Reputation: 1932
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/">
Upvotes: 0
Views: 565
Reputation: 121
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
Upvotes: 1