CoolFace
Apppublic

YUNABI/FACE_RECOG

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
face_detection.py35 linesDownload Raw Back to root
1import cv2
2
3# Load Haar Cascade file
4face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
5
6# Start video capture (0 selects the default camera)
7cap = cv2.VideoCapture(0)
8
9while True:
10    # Read a frame from the camera
11    ret, frame = cap.read()
12    if not ret:
13        break
14
15    # Convert to grayscale (Haar Cascade works on grayscale images)
16    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
17
18    # Detect faces
19    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
20
21    # Draw rectangles around detected faces
22    for (x, y, w, h) in faces:
23        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
24
25    # Display the frame with detected faces
26    cv2.imshow('Face Detection', frame)
27
28    # Break the loop if 'q' is pressed
29    if cv2.waitKey(1) & 0xFF == ord('q'):
30        break
31
32# Release the video capture and close windows
33cap.release()
34cv2.destroyAllWindows()
35