import cv2
import os
from deepface.detectors import FaceDetector
detector_backend = 'opencv'
face_detector = FaceDetector.build_model(detector_backend)
cap = cv2.VideoCapture(0) # webcam
while True:
ret, img = cap.read()
if img is None:
break
try:
# faces store list of detected_face and region pair
faces = FaceDetector.detect_faces(face_detector, detector_backend, img, align=False)
except: # to avoid exception if no face detected
faces = []
for face, (x, y, w, h) in faces:
if w > 130: # discard small detected faces
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3) # draw rectangle to main image
cv2.imshow('result', img)
if cv2.waitKey(1) & 0xFF == ord('q'): # press q to quit
break
# kill open cv things
cap.release()
cv2.destroyAllWindows()