Django + OpenCV : open webcam take pictures and save them

7,086 views
Skip to first unread message

Samara Muñoz

unread,
Aug 12, 2020, 5:06:46 PM8/12/20
to Django users
Hi, I am doing a project where I am trying to open a camera (webcam and/or IP cam), then I would like to have a bottom, and every time I click it the frames/images are saved. 
I followed this tutorial by Sanket Sawardekar:


where in my Django project named video stream :

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('streamapp.urls'))
]


settings.py I added the 'streamapp'

---------------------------------------------------------------------------

Then in my Django application named streamapp :

urls.py

#display both cameras
    path('', views.index, name='index'),

    #access the laptop camera
    path('video_feed', views.video_feed, name='video_feed'),

    #access the phone camera
    path('webcam_feed', views.webcam_feed, name='webcam_feed'),


views:
#Display the 2 videos
def index(request):
    return render(request, 'streamapp/home.html')

#Every time you call the phone and laptop camera method gets frame
#More info found in camera.py
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

#Method for laptop camera
def video_feed(request):
return StreamingHttpResponse(gen(VideoCamera()),
                    #video type
content_type='multipart/x-mixed-replace; boundary=frame')

#Method for phone camera
def webcam_feed(request):
return StreamingHttpResponse(gen(IPWebCam()),
                    #video type
content_type='multipart/x-mixed-replace; boundary=frame')



Finally a file was created camera.py where cv2 and urllib where imported:



from imutils.video import VideoStream
import imutils
import cv2
import os
import urllib.request
import numpy as np
from django.conf import settings





class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)

    def __del__(self):
        self.video.release()


    #This function is used in views
    def get_frame(self):

        success, image = self.video.read()
        frame_flip = cv2.flip(image, 1)
        ret, jpeg = cv2.imencode('.jpg', frame_flip)
        return jpeg.tobytes()



class IPWebCam(object):
    def __init__(self):
        self.url = "http://192.168.1.178:8080/shot.jpg"


    def __del__(self):
        cv2.destroyAllWindows()

    def get_frame(self):
        imgResp = urllib.request.urlopen(self.url)
        imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
        img = cv2.imdecode(imgNp, -1)
        img =cv2.resize(img, (640, 480))
        frame_flip = cv2.flip(img, 1)
        ret, jpeg = cv2.imencode('.jpg', frame_flip)
        return jpeg.tobytes()


----------------------------------------------------------------------------------------------------------
for the html:

<!DOCTYPE html>
<html>
  <head>
    <title> Video Live Stream </title>
  </head>
  <body>
    <h1> Video Live Stream </h1>
    <!-- Creating 2 image tags -->
    <img src = "{% url 'video_feed' %}">
    <img src = "{% url 'webcam_feed' %}">
  </body>
</html>



Thanks for your help


Reply all
Reply to author
Forward
0 new messages