0

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

1 Answers1

0

I'm literally working on the same topic at the moment and it seems we used almost the same templates online. I had this problem and, if I remember well, I solved it by adding a little sleep between start of recording and the streaming start.

In the end, I moved the whole init block into another function that I can then call with a button on the page or other condition later (later I'll turn it on at first client login, turn it off at last client logout):

camera = picamera.PiCamera(resolution='640x480', framerate=12)
output = StreamingOutput()
camera.start_recording(output, format='mjpeg')

Hope it helps.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Hey thanks for taking the time to reply! I abandoned DJango and managed to get this working in Flask instead. Will post the Flask code soon. – user2519486 Feb 06 '22 at 16:22