简体   繁体   English

Flask:使用 openCV 时无法进行重定向

[英]Flask: can't get redirection to work when using openCV

I am using opencv and pyzbar to make a barcode scanner that can work in a flask app.我正在使用 opencv 和 pyzbar 制作可以在烧瓶应用程序中工作的条形码扫描仪。 There is a while True loop which constantly outputs the current camera frame to 127.0.0.1:5000/scanner, whilst simultaneously attempting to decode a barcode from the image.有一个 while True 循环不断地将当前相机帧输出到 127.0.0.1:5000/scanner,同时尝试从图像中解码条形码。 If it decodes a barcode, the loop breaks and the program redirects to 127.0.0.1:5000/output/, where the barcode number is displayed - except it doesn't for some reason.如果它解码条形码,循环中断,程序重定向到 127.0.0.1:5000/output/,显示条形码编号的位置 - 但由于某种原因没有显示。 127.0.0.1:5000/output/ displays correctly if it is entered manually. 127.0.0.1:5000/output/ 如果手动输入则显示正确。

Python: Python:

from flask import Flask, render_template, Response, redirect, url_for
import io
import cv2
import sys
from pyzbar import pyzbar

app = Flask(__name__)

@app.route('/output/')
def output():
    return str(output)

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

def gen():
    vc = cv2.VideoCapture(0)
    while True:
        read_return_code, frame = vc.read()
        og = frame
        cv2.rectangle(frame,(200,170),(450,330),(0,255,100),7)
        frame = cv2.flip(frame, 1)
        decode = pyzbar.decode(og)
        if len(str(decode)) > 10:
            global output
            output = str(decode).split("'")
            output = output[1]
            vc.release()
            return redirect('/output/')   #THE PROBLEM 
            break
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
      
        encode_return_code, image_buffer = cv2.imencode('.jpg', frame)
        io_buf = io.BytesIO(image_buffer)
        print("a", file=sys.stderr)
        yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + io_buf.read() + b'\r\n')

        
@app.route('/video_feed')
def video_feed():
    return Response(
        gen(),
        mimetype='multipart/x-mixed-replace; boundary=frame'
    )


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, threaded=False) 

HTML: HTML:

<html>
<head>
<style>
body{
font-family: Verdana, sans-serif;
} 
</style>
<title>Scanner</title>
</head>
<body>
<h1>Scanner</h1>
<img src="{{ url_for('video_feed') }}">
</body>
</html>

  1. you return redirect without parameters, this is equal to just requesting it via URL.您返回不带参数的重定向,这等于仅通过 URL 请求它。

  2. your 'output' route doesn't take any input params.您的“输出”路线不接受任何输入参数。 You should change your code to something like this, where 'data' is the output of your cv:您应该将代码更改为类似这样的内容,其中“数据”是您的简历的输出:

    app.route('/output/') def output(data): return str(data) app.route('/output/') def output(data): 返回 str(data)

    app.route('/scanner') def index(): data='whatever' return redirect(url_for('output', data=data)) app.route('/scanner') def index(): data='whatever' return redirect(url_for('output', data=data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM