Integrating opencv with django for a simple face detection program

Hey everyone , im trying to make a program that detects faces with haar classifier ; i found out how to use opencv for the webcam but i didnt know how to add the detection part to it
Any help would be appreciated
here’s my code :

def get_frame(self): 
    image = self.frame
    _, jpeg = cv2.imencode('.jpg', image)
    return jpeg.tobytes()

def update(self): 
    while True:
        (self.grabbed, self.frame) = 

def gen(camera): 
 while True:
    frame = camera.get_frame()
          b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') 

def detection(camera): 
    while True:
     _, img = gen(camera)

     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

     faces = face_cascade.detectMultiScale(gray, 1.1, 4)

     for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)


    k = cv2.waitKey(30) & 0xff
    if k==27:

return img

this is how i tried to combine both

def detectme(request):
  cam = VideoCamera() 
  return StreamingHttpResponse(detect(cam), content_type="multipart/x-mixed- 

this is the home.html incase i messed something up in there

{% load static %}
{%load widget_tweaks %}
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">

{% block body %}
        <td width="50%">
            <img src="" style="width:1200px;" />
        <td width="50%">

{% endblock %}


What exactly are you trying to accomplish here? What’s the underlying objective?

From what I’m seeing of the code, I’m almost getting the idea that you’re trying to access a webcam on the user’s computer - which can’t be done from Django. If you’re trying to take a picture or get the datastream (video) from a web cam, it needs to be done from JavaScript.

thank you for your comment ,
the main purpose is to identify faces through a webcam video which was opened with opencv
the problem is i couldnt apply the detection methods

If you’re trying to access the user’s webcam, you can’t do it from Django. It can only be done with JavaScript. (Django isn’t running in the browser, it’s running in the server. The JavaScript runs in the browser.)

So the question is still - what webcam are you trying to access?