I recently started django and I wanted to see the video from the laptop camera to Django 2.2 based web app. I was successful in viewing the cam video by directly sending response to web using function display_livefeed. Following is my code of views.py of app 'camerafeed'
class mycamera(object):
def __init__(self):
self.frames = cv2.VideoCapture(0)
def __del__(self):
self.frames.release()
def get_jpg_frame(self):
is_captured, frame = self.frames.read()
retval, jframe = cv2.imencode('.jpg', frame)
return jframe.tobytes()
def livefeed():
camera_object = mycamera()
while True:
jframe_bytes = camera_object.get_jpg_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + jframe_bytes + b'\r\n\r\n')
@condition(etag_func=None)
def display_livefeed(self):
return StreamingHttpResponse(
livefeed(),
content_type='multipart/x-mixed-replace; boundary=frame'
)
I used path('monitor/', display_livefeed, name='monitor'), to see the video streaming on http://127.0.0.1:8000/monitor/ and it works perfectly >>Image from the streaming video<<
Now I wanted to display on the html template just line it is done here: https://www.pyimagesearch.com/2019/09/02/opencv-stream-video-to-web-browser-html-page/ but this was done by using Flask. But I wanted to do the same using Django and got stuck. Here is the html file from the above link.
<html>
<head>
<title>Pi Video Surveillance</title>
</head>
<body>
<h1>Pi Video Surveillance</h1>
<img src="{{ url_for('video_feed') }}">
</body>
I tried to do like this using by making this function:
def video_feed(request):
return StreamingHttpResponse(
livefeed(), # Calling livefeed() function
content_type='multipart/x-mixed-replace; boundary=frame'
)
But it able to see the video on html page using path('', homePageView.as_view(), name='home'), and placing code below in views.py
class homePageView(TemplateView):
template_name = 'home.html'
I seen and tried following:
But may be due to the fact that i am new to all these thing including web development, I might not able to get it done. Kindly help explaining me how to do this.
I am using python 3.7 and django 2.2