Opencv Live Stream From Camera In Django Webpage
I am making a project in Django. And I want to show live feed from camera on a webpage. But I am not sure on how to return live feed I get from a cam on a web page. Here's my code
Solution 1:
The solution for the above problem was something like this :
views.py
from django.views.decorators import gzip
from django.http import StreamingHttpResponse
import cv2
import threading
classVideoCamera(object):
def__init__(self):
self.video = cv2.VideoCapture(0)
(self.grabbed, self.frame) = self.video.read()
threading.Thread(target=self.update, args=()).start()
def__del__(self):
self.video.release()
defget_frame(self):
image = self.frame
_, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
defupdate(self):
whileTrue:
(self.grabbed, self.frame) = self.video.read()
defgen(camera):
whileTrue:
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_pagedeflivefe(request):
try:
cam = VideoCamera()
return StreamingHttpResponse(gen(cam), content_type="multipart/x-mixed-replace;boundary=frame")
except: # This is bad! replace it with proper handlingpass
And then in urls.py map this to a url.
Post a Comment for "Opencv Live Stream From Camera In Django Webpage"