烧瓶:TypeError:参数'%s'的预期Ptr <cv :: UMat>

时间:2019-10-31 10:08:27

标签: 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)
  

工作方式如何?

  1. 我不使用python3 upload_file.py --listofargs命令而直接以flask run的形式运行flask应用程序。

  2. 当我单击colorize时,出现上述错误 Internal server error

如何解决该错误,以便可以通过img正确读取上载的html并通过烧瓶显示回opencv img

1 个答案:

答案 0 :(得分:0)

在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渲染[我不是专家,但是如果其他人可以提供帮助,我会将其合并到此答案中。]

让我知道这是否对您有用