【发布时间】:2021-08-05 00:19:57
【问题描述】:
我是 ML 的初学者,我正在研究一些发布在 github 上的研究论文,如下所示:
https://github.com/aksh-ai/neuralBlack
为了我的学习目的,我正在尝试执行这个项目。当我使用 powershell 运行脚本 deploy.py 时,服务器会执行,当我尝试上传图像并单击分类时,它会刷新页面并且不显示预测的布局。
这就是错误所说的:
127.0.0.1 - - [05/Aug/2021 04:54:13] “POST /predict HTTP/1.1”404 - 异常
预期值:第 1 行第 1 列(字符 0)
我认为可能存在 url 名称问题,因为 predict.html 的类具有不同的名称,所以我确实将 url 从 (http://localhost:3000/predict) 更改为 (http://localhost:3000/ pred_page) 如下:
来自
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'rb') as img:
predicted = requests.post("http://localhost:3000/predict", files={"file": img}).json()
到
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'rb') as img:
predicted = requests.post("http://localhost:3000/pred_page", files={"file": img}).json()
它显示了这个错误:
127.0.0.1 - - [05/Aug/2021 05:04:41] "POST /pred_page HTTP/1.1" 405 - Exception
Expecting value: line 1 column 1 (char 0)
这是整个 deploy.py 代码:
import os
import requests
import json
from flask import Flask, flash, request, redirect, url_for, render_template, session
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = './static/images'
ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg']
app = Flask(__name__, template_folder='template')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "m4xpl0it"
@app.route('/empty_page')
def empty_page():
filename = session.get('filename', None)
os.remove(os.path.join(UPLOAD_FOLDER, filename))
return redirect(url_for('index'))
@app.route('/pred_page')
def pred_page():
pred = session.get('pred_label', None)
f_name = session.get('filename', None)
return render_template('pred.html', pred=pred, f_name=f_name)
@app.route('/', methods=['POST', 'GET'])
def index():
try:
if request.method == 'POST':
f = request.files['bt_image']
filename = str(f.filename)
if filename!='':
ext = filename.split(".")
if ext[1] in ALLOWED_EXTENSIONS:
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'rb') as img:
predicted = requests.post("http://localhost:3000/pred_page", files={"file": img}).json()
session['pred_label'] = predicted['class_name']
session['filename'] = filename
return redirect(url_for('pred_page'))
except Exception as e:
print("Exception\n")
print(e, '\n')
return render_template('index.html')
if __name__=="__main__":
app.run(port=3000)
【问题讨论】:
-
看看你是如何设置
index()接受 POST 的?如果你要发帖,你需要对pred_page做同样的事情。
标签: python machine-learning flask