【问题标题】:Flask - Bad Request The browser sent a request that this server could not understandFlask - 错误请求浏览器发送了此服务器无法理解的请求
【发布时间】:2021-07-20 11:50:00
【问题描述】:

我正在尝试对解析器模型进行预测,使用预训练的 spacy 模型,我使用烧瓶作为 api 和 html 作为前端。但它会抛出错误请求。

@app.route('/index')
def index():
    return flask.render_template('index.html')



@app.route('/index', methods=['GET', 'POST'])


def predict():
    if request.method == 'POST':
        image_file = request.files["file_image"]
 
nlp_model = spacy.load("nlp_model")

img2 = cv2.imread(image_file)
img_rgb = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
pytesseract.pytesseract.tesseract_cmd = r'C:\Tesseract- 
OCR\tesseract.exe'
out_below_1 = pytesseract.image_to_string(img_rgb,  
config=pytesseract.pytesseract.tesseract_cmd)
tx = " ".join(out_below_1.split('\n')) 

doc = nlp_model(tx)
s = []

for ent in doc.ents:
    s.append(ent)

return  s

if __name__ == '__main__':
    app.run()

【问题讨论】:

  • 当您尝试访问/index 时是否会抛出该错误?有两个原因(实际上我不知道其中一个,但是......)首先你有两条相同的路线,所以烧瓶不知道要使用哪一条,其次装饰器之间应该没有空间和函数定义

标签: python parsing flask


【解决方案1】:

通常,此错误是由于访问了 request 对象中不存在的参数。尝试打印request.files对象的内容:print(str(request.files)),它可能不包含名为file_image的文件。

还要注意路由的定义,你有两个不同的路由,叫做index

【讨论】:

  • 实际上,我已经更改了路线的名称,但我通过更改知道它不包含图像文件,它被空传递,这就是我收到此错误的原因。那么如何解决呢?
  • 这是你如何进行http调用的问题,你肯定在那里做错了什么。打开另一个问题以找出您做错了什么,因为这是关于 Bad Request 错误
猜你喜欢
  • 2021-07-04
  • 1970-01-01
  • 2018-06-21
  • 2018-12-20
  • 2018-12-13
  • 2018-07-24
  • 2020-07-10
  • 2017-07-02
  • 1970-01-01
相关资源
最近更新 更多