Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回字典对象时烧瓶中出现响应错误_Javascript_Python_Flask - Fatal编程技术网

Javascript 返回字典对象时烧瓶中出现响应错误

Javascript 返回字典对象时烧瓶中出现响应错误,javascript,python,flask,Javascript,Python,Flask,我正试图从flask应用程序中打印HTML字典,但它不允许我这样做,并抛出以下错误。我能够成功地返回字符串(),但是当我更改代码以返回字典(下面的代码)时,它抛出了一个错误。我想这和我不太熟悉的js有关 这是我得到的错误 TypeError: 'list' object is not callable The view function did not return a valid response. The return type must be a string, tuple, Respon

我正试图从flask应用程序中打印HTML字典,但它不允许我这样做,并抛出以下错误。我能够成功地返回字符串(),但是当我更改代码以返回字典(下面的代码)时,它抛出了一个错误。我想这和我不太熟悉的js有关

这是我得到的错误

TypeError: 'list' object is not callable
The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a list.
下面是我的
app.py
脚本中的两个相关函数

def model_predict(img_path, model):
    img = image.load_img(img_path, target_size=(224, 224))

    # Preprocessing the image
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = x/255

    predictions = model.predict(x)
    pred_5 = np.argsort(predictions)[0][-5:]
    top_5 = {}
    labels_dict = {'Apple Scab': 0, 'Apple Black rot': 1, 'Apple Cedar rust': 2, 'Apple healthy': 3}
    for i in pred_5:
        rank = predictions[0][i]
        for kee, val in labels_dict.items():
            if i == val:
                top_5[kee] = rank

    sorted_x2 = sorted(top_5.items(), key=operator.itemgetter(1), reverse=True)
    return sorted_x2

@app.route('/predict', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        f = request.files['file']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(
            basepath, 'uploads', secure_filename(f.filename))
        f.save(file_path)

        result = model_predict(file_path, model)
        return result

    return None
这是我的js文件-

使用
[jsonify()][1]
传递数据。它将数据序列化为JSON,从而返回JSON响应。不要只返回
返回结果
,而是返回jsonify(结果)

更新代码:

def model_predict(img_path, model):
    img = image.load_img(img_path, target_size=(224, 224))

    # Preprocessing the image
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = x/255

    predictions = model.predict(x)
    pred_5 = np.argsort(predictions)[0][-5:]
    top_5 = {}
    labels_dict = {'Apple Scab': 0, 'Apple Black rot': 1, 'Apple Cedar rust': 2, 'Apple healthy': 3}
    for i in pred_5:
        rank = predictions[0][i]
        for kee, val in labels_dict.items():
            if i == val:
                top_5[kee] = rank

    sorted_x2 = sorted(top_5.items(), key=operator.itemgetter(1), reverse=True)
    return sorted_x2

@app.route('/predict', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        f = request.files['file']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(
            basepath, 'uploads', secure_filename(f.filename))
        f.save(file_path)

        result = model_predict(file_path, model)
        return jsonify(result)

    return None

它不起作用。我得到了这个错误
TypeError:type'float32'的对象不是JSON可序列化的
。你认为我还需要在这里改变什么吗?没关系。在我把浮点数转换成整数后,它就成功了。谢谢分享示例输出您可能在列表中使用numpy floats对象,如果是这种情况,请使用float(var)将所有numpy float转换为python float。哦,我明白了。我会努力的。这是输出-。目前,它是粗糙的,但我会努力使它看起来更好。