#!/usr/bin/python
"""
Created on Fri Dec 7 20:27:44 2018
@author: 10748
"""
import os
import filetype
import sys
import tensorflow as tf
def train(file):
a_list = list()
# change this as you see fit
image_path = file
# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("output_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("output_graph.pb", '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')
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]
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
a_list.append(human_string+'_score = '+str(score))
return a_list
def file_type(file):
try:
b = filetype.guess(file)
c = b.extension
if c == 'jpg':
return file
except:
return
file_list = list()
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
file_list.append(os.path.join(name))
for i in range(len(file_list)):
file_name = file_type(file_list[i])
if file_name == None:
print('')
else:
a =train(file_name)
for q in range (len(a)):
print(a[q])
input('按下回车键退出')
终端上显示:
Traceback (most recent call last):
File "D:\Anaconda3\envs\tensorflow-gpu\flow\data\train\TS.py", line 35, in
for q in range (len(a)):
NameError: name 'a' is not defined