tf.gfile.FastGFile(file,'rb')。read()两次读取图像

时间:2017-08-17 17:50:05

标签: python tensorflow

如果我使用tf.gfile.FastGFile(file, 'rb').read()两次,每次都在循环中,从两个不同的文件夹中读取图像,就像这样

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

....

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

然后是python,在第二个循环中开始从第一个文件夹读取图像,它已经读过,在完成第一个文件夹后,它开始读取第二个文件夹的图像。

如何解决这个问题?

CODE:

import os,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

1 个答案:

答案 0 :(得分:0)

您已使用files=[]从两个不同的文件夹中读取文件。

使用两个不同的files=[]files1=[]代替一个files=[]