2017-08-17 166 views
0

如果我在一個循環中使用tf.gfile.FastGFile(file, 'rb').read()兩次,每次時間,從兩個不同的文件夾中讀出的圖像,這樣tf.gfile.FastGFile(文件, 'RB')。read()讀取圖像兩次

image_data = tf.gfile.FastGFile(file, 'rb').read() 

.... 

image_dataA= tf.gfile.FastGFile(file, 'rb').read() 

然後python,在第二個循環開始從其中的第一個文件夾讀取圖像,它已經讀取,並在完成第一個文件夾後,開始讀取第二個文件夾的圖像。

如何解決這個問題?

CODE:

進口操作系統,SYS

import glob 

import tensorflow as tf 

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 

.....4 folder path's variable... 

extension = ['*.jpeg', '*.jpg'] 
files=[] 

.... 

for e in extension: 

    directory = os.path.join(image_path_face, e) 

    fileList = glob.glob(directory) 

for f in fileList: 

    files.append(f) 

#Loads label file, strips off carriage return 

label_lines = [line.rstrip() for line in tf.gfile.GFile(label_path)] 

#Unpersists graph from file 

with tf.gfile.FastGFile(model_path, 'rb') as f: 

    graph_def = tf.GraphDef() 

    graph_def.ParseFromString(f.read())tf.import_graph_def(graph_def, name='') 

with tf.Session() as sess: 
    # Feed the image_data as input to the graph and get first prediction 
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') 

    # Read in the image_data 
    for file in files: 
     image_data = tf.gfile.FastGFile(file, 'rb').read() 
     tp += 1 
     predictions = sess.run(softmax_tensor, \ 
         {'DecodeJpeg/contents:0': image_data}) 

    # Sort to show labels of first prediction in order of confidence 
     top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] 
     #print("Image Name: " + file) 
     for node_id in top_k: 
      human_string = label_lines[node_id] 
      score = predictions[0][node_id] 
      if score > 0.51: 
        p += 1 
      print('%s (score = %.5f)' % (human_string, score)) 
print("Number of correct face detection %s out of %s " % (p, tp)) 


for e in extension: 

     directory = os.path.join(image_path_nface, e) 

     fileList = glob.glob(directory) 

for f in fileList: 

    files.append(f) 

with tf.Session() as sess: 

    # Feed the image_data as input to the graph and get first prediction 
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') 

    # Read in the image_data 
    for file in files: 
     image_data = tf.gfile.FastGFile(file, 'rb').read() 
     tn += 1 
     predictions = sess.run(softmax_tensor, \ 
         {'DecodeJpeg/contents:0': image_data}) 

    # Sort to show labels of first prediction in order of confidence 
     top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] 
     # print("Image Name: " + file) 
     for node_id in top_k: 
      human_string = label_lines[node_id] 
      score = predictions[0][node_id] 
      if score > 0.51: 
        n += 1 
      print('%s (score = %.5f)' % (human_string, score)) 


print("Number of correct non-face detection %s out of %s " % (n, tn)) 

輸出是 -

f (score = 0.82634) 
nf (score = 0.17366) 
f (score = 0.99175) 
nf (score = 0.00825) 
Number of correct face detection 2 out of 2 
f (score = 0.82634) 
nf (score = 0.17366) 
f (score = 0.99175) 
nf (score = 0.00825) 
nf (score = 0.99081) 
f (score = 0.00919) 
nf (score = 0.99614) 
f (score = 0.00386) 
nf (score = 0.99388) 
f (score = 0.00612) 
nf (score = 0.99872) 
f (score = 0.00128) 
Number of correct non-face detection 6 out of 6 

回答

0

您已經使用files=[]文件從兩個不同的文件夾中讀取。

使用兩個不同的files=[]files1=[]而不是一個files=[]