python - Flask:TypeError:参数 '%s'的预期Ptr <cv::UMat>

标签 python image opencv flask upload

我正在通过 html 上传img,并在打开的简历中处理它并显示图像。我在arg parsers中有upload_file.py。所以我直接以python3 upload_file.py --listofargs的形式运行应用程序

template.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>

upload_file.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)

How it is working ?


  • 我不使用python3 upload_file.py --listofargs命令直接将 flask 应用程序作为flask run运行。
  • 当我单击colorize时,出现上述错误 Internal server error

  • 如何解决错误,以便可以通过img正确读取上载的html并通过 flask 显示回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渲染[我不是专家,但是如果有人可以提供帮助,我会将其合并到此答案中。]

    让我知道这是否适合您

    关于python - Flask:TypeError:参数 '%s'的预期Ptr <cv::UMat>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58640945/

    相关文章:

    java - 屏幕上有图像 "wrap around"。 JavaFX

    Javascript - 等待图像加载

    opencv - DJI Phantom 4 相机本征矩阵

    python - 谷歌应用引擎: empty property in datastore

    python - pymongo DuplicateKeyError - 在 upsert 期间

    python - 将新数据附加到数据框

    python - BS4 如何在不使用 .text 的情况下获取文本?

    c# - 如何在 ASP.NET 中使用 C# 从数据库中检索二进制图像

    python - 高级方形检测(带连接区域)

    python - TypeError:labels不是numpy数组,也不是标量