import cv2
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") ds_factor = 0.6 font = cv2.FONT_HERSHEY_SIMPLEX
class VideoCamera(object): def __init__(self): self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
success, image = self.video.read()
output = cv2.rectangle(image, (400, 300), (800, 600), (255, 255, 255), 2)
cv2.resize(output, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.1,
minNeighbors=5,
minSize=(200, 200),
flags=cv2.CASCADE_SCALE_IMAGE)
for (x, y, w, h) in faces:
cv2.rectangle(output, (x, y), (x + w, y + h), (255, 0, 0), 3)
cv2.putText(output, 'Number of Faces : ' + str(len(faces)), (40, 40), font, 1, (255, 0, 0), 2)
break
ret, jpeg = cv2.imencode('.jpg', output)
return jpeg.tobytes()