1

我使用 Flask 框架编写了一个用 python编写的网站。我试图在网站上显示实时视频流,但似乎只有一个客户端可以流式传输视频,甚至该流在 30 秒后失败......

该网站部署在树莓派上,有 3 名工作人员,所以我知道他们不可能同时从相机读取帧。这就是为什么我使用了我在网上找到的同步方法,尽管结果如上所述。

这是我的代码的相关部分:

from flask import Flask, render_template, url_for, request, escape, Response
import picamera 
from camera import CameraStream
import cv2

app = Flask(__name__)

cap = CameraStream().start()

def gen_frame():
"""Video streaming generator function."""
    while cap:
        frame = cap.read()
        convert = cv2.imencode('.jpg', frame)[1].tobytes()
        yield (b'--frame\r\n'
           b'Content-Type: image/jpeg\r\n\r\n' + convert + b'\r\n') # concate frame one by one and show result


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

相机模块是我提到的同步方法的副本(链接

让多个客户端连接并尝试流式传输我的相机后我得到的日志:

Jan 03 17:46:41 raspberrypi gunicorn[1567]:   File "/usr/lib/python2.7/dist-
packages/werkzeug/wrappers.py", line 82, in _iter_encoded
Jan 03 17:46:41 raspberrypi gunicorn[1567]:     for item in iterable:
Jan 03 17:46:41 raspberrypi gunicorn[1567]:   File "/home/pi/Desktop/python_scripts/internetdisplay/app.py", line 23, in gen_frame
Jan 03 17:46:41 raspberrypi gunicorn[1567]:     frame = cap.read()
Jan 03 17:46:41 raspberrypi gunicorn[1567]:   File "/home/pi/Desktop/python_scripts/internetdisplay/camera.py", line 31, in read
Jan 03 17:46:41 raspberrypi gunicorn[1567]:     frame = self.frame.copy()
Jan 03 17:46:41 raspberrypi gunicorn[1567]: AttributeError: 'NoneType' object has no attribute 'copy'
Jan 03 17:46:54 raspberrypi gunicorn[1567]: [2020-01-03 17:46:54 +0000] [1567] [CRITICAL] WORKER TIMEOUT (pid:1576)
Jan 03 17:46:54 raspberrypi gunicorn[1567]: [2020-01-03 17:46:54 +0000] [1576] [INFO] Worker exiting (pid: 1576)
Jan 03 17:46:55 raspberrypi gunicorn[1567]: [2020-01-03 17:46:55 +0000] [1593] [INFO] Booting worker with pid: 1593

我尝试从模块调整读取方法,如果框架为无,则返回无。但结果是相似的。

4

0 回答 0