I'm trying to use raspberry pi capture the image from USB camera and stream it with Django framework I have tried to use StreamingHttpResponse to stream the frame from Opencv2. However, it just shows 1 frame and not replacing the image.
How can I replace the image in real time?
Here is my code.
from django.shortcuts import render
from django.http import HttpResponse,StreamingHttpResponse
import cv2
import time
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(frame)
time.sleep(1)
def index(request):
# response = HttpResponse(gen(VideoCamera())
return StreamingHttpResponse(gen(VideoCamera()),content_type="image/jpeg")
@Ritwick What I have done is by changing the gen and index function to below
I use the python generator to generate every camera frame and using the StreamingHttpResponse to replace the multipart/x-mixed-replace which the boundary tagged as frame
In django there is a gzip decorator function.
To improve the speed of Streaming. I used the django gzip decorator method to gzip the frame.