我正在尝试基于旧博客文章(松散地)创建一个服务器,以使用 Quart 流式传输视频。
要将视频流式传输到客户端,似乎我需要做的就是有一个返回帧生成器的路由。但是,实际上这样做会导致不断重复的消息socket.send() raised exception
,并在客户端上显示损坏的图像。之后,服务器似乎不再响应进一步的请求。
使用来自原始帖子的更多灵感,我尝试返回一个Response
(使用return Response(generator, mimetype="multipart/x-mixed-replace; boundary=frame")
.)这确实会在客户端上显示视频,但是一旦他们断开连接(关闭选项卡,导航到另一个页面等),服务器就会socket.send() raised exception
再次开始发送垃圾邮件并且确实不回应进一步的请求。
我的代码如下。
# in app.py
from camera_opencv import Camera
import os
from quart import (
Quart,
render_template,
Response,
send_from_directory,
)
app = Quart(__name__)
async def gen(c: Camera):
for frame in c.frames():
# d_frame = cv_processing.draw_debugs_jpegs(c.get_frame()[1])
yield (b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + frame[0] + b"\r\n")
c_gen = gen(Camera(0))
@app.route("/video_feed")
async def feed():
"""Streaming route (img src)"""
# return c_gen
return Response(c_gen, mimetype="multipart/x-mixed-replace; boundary=frame")
# in camera_opencv.py
from asyncio import Event
import cv2
class Camera:
last_frame = []
def __init__(self, source: int):
self.video_source = source
self.cv2_cam = cv2.VideoCapture(self.video_source)
self.event = Event()
def set_video_source(self, source):
self.video_source = source
self.cv2_cam = cv2.VideoCapture(self.video_source)
async def get_frame(self):
await self.event.wait()
self.event.clear()
return Camera.last_frame
def frames(self):
if not self.cv2_cam.isOpened():
raise RuntimeError("Could not start camera.")
while True:
# read current frame
_, img = self.cv2_cam.read()
# encode as a jpeg image and return it
Camera.last_frame = [cv2.imencode(".jpg", img)[1].tobytes(), img]
self.event.set()
yield Camera.last_frame
self.cv2_cam.release()