python - Flask 无法读取从 html 上传的图像
问题描述
我正在尝试制作一个简单的 html 网页,用户可以在其中上传图片。该图像通过一种形式传递给flask API,其中图像被传递给图像分类模型。但是,上传文件后,我无法在 Flask API 中接收图像。我收到一条错误消息,
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'uploaded_img'
有人可以指导我哪里出错了吗?
我的html代码:
<body class="center-screen">
<div id="content">
<form id="url-form" action="/classify_image" method="post" enctype="multipart/form-data">
<h1>Upload Image</h1>
<div class="formcontainer">
<hr/>
<div class="container">
<label for="img">Upload image:</label>
<input type="file" id="img" name="uploaded_img" accept="image/*">
</div>
<button type="submit">
<strong>Classify Image</strong>
</button>
</div>
</form>
</div>
</body>
我的烧瓶端点:
@app.route('/classify_image', methods=['POST'])
def classify_image():
if request.method == 'POST':
print("Running")
f = request.files['uploaded_img']
model.predict(f)
解决方案
推荐阅读
- vue.js - Vuetify 复选框非常大
- python-3.x - 在创建 Google Compute Engine VM 实例时添加环境变量
- python - 将嵌套列表扩展到行、创建标题并映射回原始列
- azure - ASP.NET Core 2.2 站点预热
- datatables - ? 让带有固定列的 datatables.js 水平滚动在移动设备上工作?
- css - 更改 textInput 闪亮小部件的占位符颜色
- bash - grep 到远程主机上的文件和 scp 到本地
- brightway - 在 Brightway 中计算不同影响评估方法的蒙特卡洛结果的有效方法
- java - 如何提高我的 Android Studio 应用程序性能
- ssas - SSAS MDX 脚本查看针对不同日期的新计算度量