Search code examples
pythonopencvframeworkspyramid

Pyramid Framework : Stream Camera using Opencv


I have been trying to stream my webcam using the pyramid framework. The thing is the object generated is a generator object, and when I'm passing it over views, I do get the video stream at route (/video_feed) but it lags a lot. anyone?

here's the block of code for it

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")

Help!


Solution

  • It's probable that your WSGI server or any other upstream proxy is buffering the streaming responses. If this is waitress I think you can set send_bytes to something lower than the default.

    https://docs.pylonsproject.org/projects/waitress/en/latest/arguments.html