Last active
February 12, 2024 19:13
-
-
Save kumarvipu1/4f46b8c47c7dce5f31c940dc9a172eed to your computer and use it in GitHub Desktop.
Face Detect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
# Enable camera | |
cap = cv2.VideoCapture(0) | |
cap.set(3, 640) | |
cap.set(4, 420) | |
# import cascade file for facial recognition | |
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
''' | |
# if you want to detect any object for example eyes, use one more layer of classifier as below: | |
eyeCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye_tree_eyeglasses.xml") | |
''' | |
while True: | |
success, img = cap.read() | |
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
# Getting corners around the face | |
faces = faceCascade.detectMultiScale(imgGray, 1.3, 5) # 1.3 = scale factor, 5 = minimum neighbor | |
# drawing bounding box around face | |
for (x, y, w, h) in faces: | |
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3) | |
''' | |
# detecting eyes | |
eyes = eyeCascade.detectMultiScale(imgGray) | |
# drawing bounding box for eyes | |
for (ex, ey, ew, eh) in eyes: | |
img = cv2.rectangle(img, (ex, ey), (ex+ew, ey+eh), (255, 0, 0), 3) | |
''' | |
cv2.imshow('face_detect', img) | |
if cv2.waitKey(10) & 0xFF == ord('q'): | |
break | |
cap.release() | |
cv2.destroyWindow('face_detect') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment