我一直在尝试使用金字塔框架来流传输我的网络摄像头。问题是生成的对象是一个生成器对象,当我在视图中传递它时,我确实在route(/video_feed)处获得了视频流,但它滞后了很多。有人知道吗?
下面是它的代码块
from pyramid.response import Response
from pyramid.view import view_config
from sqlalchemy.exc import DBAPIError
from ..models import MyModel
import cv2
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __iter__(self):
return self
def close(self):
self.video.release()
def get_frame(self):
success,image = self.video.read()
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
__next__=get_frame
@view_config(route_name='home', renderer='templates/stream.jinja2')
def my_view(request):
return {'project': 'my_project'}
def generate(camera):
while True:
frame = camera.__next__()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@view_config(route_name="video_feed")
def video(request):
return Response(
app_iter=generate(VideoCamera()),
content_type="multipart/x-mixed-replace; boundary=frame")
救命啊!
1条答案
按热度按时间i1icjdpr1#
很可能是你的WSGI服务器或者任何其他的上游代理正在缓冲流响应。如果这是waiter,我想你可以把
send_bytes
设置得比默认值低一些。https://docs.pylonsproject.org/projects/waitress/en/latest/arguments.html