0

I am trying to stream video to multiple browsers using opencv and django on a raspberry pi. In the code I share below, I am able to see my video stream on a different computer which is great, but when another computer tries to access the stream it just gets a blank screen. If I close the stream on the first computer, the second computer will now be able to access it.

So my question is, is this due to my code, or is this due to a limitation of the django development server, and I need to use gunicorn/nginix or similar production level? I am hoping I am just missing something in the code...

#views.py
class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)
    def __del__(self):
        self.video.release()

    def get_frame(self):
       ret,image = self.video.read()
       ret,jpeg = cv2.imencode('.jpg',image)
       return jpeg.tobytes()

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield(b'--frame\r\n'
        b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@gzip.gzip_page
def videoStream(request): 
    try:
        return StreamingHttpResponse(gen(VideoCamera()),content_type="multipart/x-mixed- replace;boundary=frame")
    except HttpResponseServerError as e:
        print("aborted")

Then my HTML is very simple for now:

<img id="image" src = "http://127.0.0.0:8000/videostream/">
MattG
  • 1,682
  • 5
  • 25
  • 45

1 Answers1

1

If I remember correctly, you can't capture one camera twice. Second request may have a problem capturing already captured camera.

You may try creating second process capturing video into some buffer like Redis and having django views read data from it. Something like in this answer

Mur4al
  • 111
  • 8
  • I think this is the right answer, but I have not been able to successfully implement it. I have tried to use redis but the only way I can get it to work is always having the camera on, and then when the view is accessed, it reads from the redis stream, which is not ideal but the answer is here somewhere! – MattG Mar 06 '21 at 13:30