如何在Flask中使用OpenCV从客户端捕获视频/图像?

时间:2019-01-25 19:40:19

标签: python opencv flask

我正在尝试创建一个Flask应用程序,该应用程序将允许用户将其网络摄像头用作运动检测器,而我在OpenCV中有一个可检测运动的代码,但现在它仅使用本地计算机的凸轮(即服务器)。我不知道如何从客户端而不是服务器捕获图像/视频,然后将其传递给OpenCV进行运动检测。

我发现这篇文章https://kirupa.com/html5/accessing_your_webcam_in_html5.htm使用HTML5和JS捕获来自客户端的提要,但没有显示将其用于OpenCV的方法。

这是烧瓶应用程序中路线的代码

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

这是上面的响应中传递的“ get_frame()”

def get_frame():
    camera_port = 0  # Assigns which webcame to detect if user has more than one webcam
    camera = cv2.VideoCapture(camera_port)  # Creates a camera object
    first_frame = None  # This variable will store the first image as a base image for comparing with images thereafter

    # Starts the while loop to stream feed
    while True:
        status = 0
        check, frame = camera.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Converts to gray scale
        gray = cv2.GaussianBlur(gray, (21, 21), 0)
        ....
        ....
        imgencode = cv2.imencode('.jpg',frame)[1]
        image_data = imgencode.tostring()
        yield (b'--frame\r\n'
             b'Content-Type: text/plain\r\n\r\n' + imageData + b'\r\n')

0 个答案:

没有答案