Потоковое видео на фляге без ошибок, но работает только приложение, которое не отображается в браузере - PullRequest
0 голосов
/ 01 ноября 2019

Я хочу транслировать видео о работе с фляжкой. Я изначально пытаюсь стримить видео с камеры. Там нет ошибок, но это не работает. На самом деле он не транслируется на html-странице. Он загружает html-страницу, но не отображает видео, которое мне нужно.

app.py:

import cv2
import numpy as np
from flask import Flask, render_template, Response

app = Flask(__name__)

@app.route('/')
def index():
    # return the rendered template
    return render_template("view.html")

def gen():
    while True:
        capture = cv2.VideoCapture(0)
        ret, frame = capture.read()
        if ret == False:
            continue
        # encode the frame in JPEG format
        encodedImage = cv2.imencode(".jpg", frame)
        yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + bytearray(np.array(encodedImage)) + 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(debug = True)

Это мой HTML-код.

view.html:

<html>
  <head>
    <title>Pi Video Surveillance</title>
  </head>
  <body>
    <h1>Pi Video Surveillance</h1>
    <img src="{{ url_for('video_feed') }}">
  </body>
</html>

1 Ответ

0 голосов
/ 01 ноября 2019

Просто напишите capture = cv2.VideoCapture(0) снаружи while True: в def gen():.

Ниже приведена измененная функция def gen():

def gen():
    capture = cv2.VideoCapture(0)
    while True:
        ret, frame = capture.read()
        if ret == False:
            continue
        # encode the frame in JPEG format
        encodedImage = cv2.imencode(".jpg", frame)
        yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + bytearray(np.array(encodedImage)) + b'\r\n')
...