代码之家  ›  专栏  ›  技术社区  ›  Raksha

oserror:尝试使用keras模型预测时,无法识别图像文件<\io.bytesio object at…>。

  •  1
  • Raksha  · 技术社区  · 6 年前

    我在youtube上跟随了deeplizard的“部署keras神经网络来烧瓶web服务”教程,但是被卡住了。 我发现了一些对类似问题有帮助的建议(比如 this one this one )但不知为什么他们不为我工作。或者我用错了。

    电话线打乱了 image = Image.open(io.BytesIO(decoded)) .

    这是我的代码(抱歉,这不是最小的,我不知道如何简化它而不删除可能相关的细节)。

    如果你有什么建议,请告诉我。

    非常感谢。

    app = Flask(__name__)
    
    
    def get_model():
        global model, graph
        model = load_model('model.h5')
        print(' * Model loaded!')
        graph = tf.Graph()
    
    
    def preprocess_image(image, target_size):
        if image.mode != 'RGB':
            image = image.convert('RGB')
        image = image.resize(target_size)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)
    
        return image
    
    
    print(' * Loading model...')
    get_model()
    
    
    @app.route('/predict', methods=["POST"])
    def predict():
        message = request.get_json(force=True)
        encoded = message['image']
        decoded = base64.b64decode(encoded)
        with graph.as_default():
            image = Image.open(io.BytesIO(decoded))
            preprocessed_image = preprocess_image(image, target_size(50, 50))
            prediction = model.predict(preprocessed_image).tolist()
    
            response = {
                'prediction': {
                    'food': prediction[0][0],
                    'notfood': prediction[0][1]
                }
            }
        return jsonify(response)
    

    我怀疑这可能是因为我的模型将输入作为:

    model.predict_classes(i.reshape((-1, 50, 50, 3)), batch_size=32, verbose=0)[0]

    但用户通过HTML上传的图片并没有被“重塑”…我想把它编入密码,但到目前为止运气不佳。

    0 回复  |  直到 6 年前