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) = self.video.read()
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')
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:
break
return img
this is how i tried to combine both
@gzip.gzip_page
def detectme(request):
cam = VideoCamera()
return StreamingHttpResponse(detect(cam), content_type="multipart/x-mixed-
replace;boundary=frame")
this is the home.html incase i messed something up in there
{% load static %}
{%load widget_tweaks %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DJAGO_OPENCV</title>
</head>
<body>
{% block body %}
<h1></h1>
<table>
<tr>
<td width="50%">
<img src="http://127.0.0.1:8000/detectme" style="width:1200px;" />
</td>
<td width="50%">
</td>
</tr>
</table>
{% endblock %}
</body>
</html>