def run_inference_on_image(image):
"""Runs inference on an image.
Args:
image: Image file name.
Returns:
Nothing
"""
#image_data = tf.gfile.FastGFile(image, 'rb').read()
image_data = image
# Creates graph from saved GraphDef.
#create_graph()
with tf.Session() as sess:
# Some useful tensors:
# 'softmax:0': A tensor containing the normalized prediction across
# 1000 labels.
# 'pool_3:0': A tensor containing the next-to-last layer containing 2048
# float description of the image.
# 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
# encoding of the image.
# Runs the softmax tensor by feeding the image_data as input to the graph.
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
predictions = sess.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data.tostring()})
predictions = np.squeeze(predictions)
sess.close()
# Creates node ID --> English string lookup.
node_lookup = NodeLookup()
top_k = predictions.argsort()[1:][::-1]
human_string = node_lookup.id_to_string(top_k[0])
score = predictions[top_k[0]]
return {
'autocategory': human_string,
'autocategoryconfidence': str(score)
}
#print('%s (score = %.5f)' % (human_string, score))
评论列表
文章目录