我试图使用一个烧瓶包装器将一个图像上传到tfinception模型,但是在通过postman测试时,我遇到了一个错误。我尝试了很多google搜索/SO,但是没有找到一种方法来解决最初的image_data部分的地址
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
但是我已经用flask的请求模块改变了它来接受图像数据,而这个总是空的
^{pr2}$但我想把我上传的图像文件的数据传过来。在
错误:
InvalidArgumentError (see above for traceback): Invalid JPEG data, size 0
[[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_DecodeJpeg/contents_0)]]
代码:
from flask import Flask, request
import tensorflow as tf
import sys
app = Flask(__name__)
@app.route("/classify", methods=["POST"])
def classify():
image_data = request.data
#loads label file, strips off carriage return
label_lines = [line.strip() for line in tf.gfile.GFile("/tmp/output_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("/tmp/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 an 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]
print('%s (score = %.2f)' % (human_string, score))
if __name__ == '__main__':
app.run()
(完整重写答案,感谢您的澄清)
因此,我所理解的是,当直接使用文件名时,您的代码运行得很好,但是一旦您尝试从POST读取该文件,就会失败。在
在代码中检索文件如下所示:
Looking around the web我发现您应该像这样获取数据:
^{pr2}$相关问题 更多 >
编程相关推荐