[英]Flask: TypeError: Expected Ptr<cv::UMat> for argument '%s'
我正在通过html上传一个img
并在打开的 cv 中处理它并显示图像。 我在upload_file.py
中有arg parsers
。 所以我直接将应用程序作为 python3 upload_file.py --listofargs 运行
模板.html
<html>
<form action="{{ url_for('handle_data') }}" method="post">
<input type="file" id="imageInput" name="file" enctype="multipart/form-data">
<ul class="list-group list-group-flush">
<li class="list-group-item">
<button type="submit" class="btn btn-primary">Colorize</button>
</li>
</ul>
</form>
<img src = {{image}}>
</html>
上传文件.py
from flask import Flask, render_template, request
import cv2
from flask import jsonify
import argparse
import numpy as np
app = Flask(__name__)
@app.route('/', methods=['POST','GET'])
def index():
return render_template('template.html')
@app.route('/handle_data', methods=['POST','GET'])
def handle_data():
img = request.form['file']
image = np.array(img)
cv2.imshow("Original", image)
return render_template(template.html, image = image)
它是如何工作的?
我将 flask 应用程序直接作为python3 upload_file.py --listofargs
运行,而不使用flask run
命令。
当我单击 colorize 时,出现上述错误Internal server error
如何修复错误,以便我可以通过 html 正确读取上传的img
并通过html
显示opencv img
?
在 Python3 中,您可以这样做:
import numpy as np
import cv2
from io import BytesIO # not necessary?
def handle_data():
img = request.form['file']
image = cv2.imdecode(np.frombuffer(BytesIO(img).read(), np.uint8), cv2.IMREAD_UNCHANGED)
cv2.imshow("Original", image) # Why do you need this?
return render_template('template.html', image = image) # Don't forget the quotes?
这应该让您足够远地处理 html 呈现自己[我不是这方面的专家 - 但如果其他人可以提供帮助,我会将其纳入此答案。]
让我知道这是否适合你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.