首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用图像上传的Tensorflow Inception

使用图像上传的Tensorflow Inception
EN

Stack Overflow用户
提问于 2017-05-10 20:06:50
回答 1查看 426关注 0票数 0

我尝试使用Flask包装器将图片上传到TF-Inception模型,但这是我通过postman测试时遇到的错误。我已经尝试了很多谷歌搜索,但是找不到解决image_data部分的方法,这个部分最初是

代码语言:javascript
复制
image_data = tf.gfile.FastGFile(image_path, 'rb').read()

但是我已经使用flask的request模块将其更改为接受图像数据,并且它一直都是空的

代码语言:javascript
复制
image_data = request.data

但是我想传递我上传的图像文件的数据。

错误:

代码语言:javascript
复制
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)]]

代码:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2017-05-10 22:31:57

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

因此,我所理解的是,您的代码在直接使用文件名时工作得很好,但一旦您尝试从POST读取该文件,代码就会失败。

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

代码语言:javascript
复制
image_data = request.data

Looking around the web我发现你可能应该像这样获取数据:

代码语言:javascript
复制
# 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']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43892046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档