我正在尝试使用Flask包装器将图像上传到TF-Inception模型,但这是我在通过邮递员测试时遇到的错误。我已经尝试了很多谷歌搜索/但没有找到一种方法来弄清楚如何解决最初的image_data部分
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
但是我已经改变它以使用烧瓶的请求模块接受图像数据,并且这一直是空的
image_data = request.data
但我想传递我正在上传的图片文件的数据。
错误:
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()
答案 0 :(得分:1)
(完全重写答案,谢谢澄清)
所以,据我所知,你的代码在直接使用文件名时效果很好,但是一旦你尝试从 POST 中读取文件就失败了。
在您的代码中,您可以像这样检索文件:
image_data = request.data
Looking around the web我发现你应该得到这样的数据:
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']