I hope I find you well.
I am really struggling with a personal project and would be really grateful if someone could please set me straight.
I wish to stream video from a picamera in django. I saw some cv2 implementations, had some trouble with cv2 and decided to try an alternate route. Some of the code here is lifted from the picamera documentation and some is from some of the cv2 implementations I've come across.
When I hit the django url, the camera begins to stream however the webpage is grey with a small box at the centre with no visible stream.
python3 -m django --version: 3.0.6
Full code posted below. This is what I see
Is anyone able to point me in the right direction?
Thank you, Darren
from django.shortcuts import render
from django.http import HttpResponse
from django.http import StreamingHttpResponse
from doron.models import Storage
from django.views import generic
import picamera
import io
from threading import Condition
from time import sleep
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# New frame, copy the existing buffer's content and notify all
# clients it's available
self.buffer.truncate()
with self.condition:
self.frame = self.buffer.getvalue()
self.condition.notify_all()
self.buffer.seek(0)
return self.buffer.write(buf)
def livefe(request):
def cam():
with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
output = StreamingOutput()
camera.start_recording(output, format='mjpeg')
try:
while True:
with output.condition:
output.condition.wait()
frame = output.frame
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
finally:
camera.stop_recording()
try:
return StreamingHttpResponse(cam(), content_type="multipart/x-mixed-replace;boundary=frame")
except:
pass