繁体   English   中英

带有图像上传的Tensorflow Inception

[英]Tensorflow Inception with image upload

我正在尝试使用Flask包装器将图像上传到TF-Inception模型,但这是我在通过邮递员进行测试时遇到的错误。 我已经尝试了很多谷歌搜索/ SO,但没有找到一种方法来弄清楚如何解决原来是

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

但是我已经更改了它以使用flask的请求模块接受图像数据,并且这种情况一直都是空的

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()

(完全重写答案,感谢您的澄清)

因此,我的理解是,当直接使用文件名时,您的代码运行良好,但是一旦尝试从POST读取文件,代码就会失败。

在您的代码中,您可以像这样检索文件:

image_data = request.data

环顾网络,我发现您应该这样获取数据:

# 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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM