如何转换:尝试将不支持类型(<class'werkzeug.datastructures.FileStorage'>)的值('image/jpeg')>)转换为张量?

2024-09-10 15:16:00 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是烧瓶和导入的求值函数,错误如下 ValueError:尝试将类型不受支持的值(<;class'werkzeug.datastructures.FileStorage')转换为张量

import io
from flask import Flask, render_template, request, send_file
from flask_uploads import UploadSet, configure_uploads, IMAGES
import os 
import random 
import string 
import numpy as np 
import cv2

[...]

@app.route('/upload', methods=['GET', 'POST'])
    def upload():
        global files
        if request.method == 'POST' and 'photo' in request.files:
            filename_raw = request.files['photo'].filename
            if len(files) > 100:
                files = {}
            fn, ext = os.path.splitext(filename_raw)
            filename = ''.join([random.choice(string.hexdigits) for v in xrange(20)]) + ext 
            files[filename] = request.files['photo'].read()
            test_res,a = evaluate([request.files['photo']])
            #filename = photos.save(request.files['photo'])
            print request.files['photo']
            img_path = '/uploads/' + filename 
            return '<html><head></head><body>' + ("<img width=400 src='%s'/><br>" % img_path) + '<br>'.join(test_res) + '</body></html>'
        return render_template('upload.html')



def evaluate(image):
    attention_plot = np.zeros((max_length, attention_features_shape))
    hidden = decoder.reset_state(batch_size=1)
    temp_input = tf.expand_dims(load_image(image)[0], 0)
    img_tensor_val = image_features_extract_model(temp_input)
    img_tensor_val = tf.reshape(img_tensor_val, (img_tensor_val.shape[0], -1, img_tensor_val.shape[3]))
    features = encoder(img_tensor_val)
    dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0)
    result = []
    for i in range(max_length):
      predictions, hidden, attention_weights = decoder(dec_input, features, hidden)
      attention_plot[i] = tf.reshape(attention_weights, (-1, )).numpy()
      predicted_id = tf.argmax(predictions[0]).numpy()
      result.append(index_word[predicted_id])
      if index_word[predicted_id] == '<end>':
        return result, attention_plot
      dec_input = tf.expand_dims([predicted_id], 0)
    attention_plot = attention_plot[:len(result), :]
    return result, attention_plot

Tags: imageimportimginputreturnplotrequesttf