-
Notifications
You must be signed in to change notification settings - Fork 0
/
HumanEyeDetector.py
43 lines (32 loc) · 1.57 KB
/
HumanEyeDetector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import cv2
# Load the pre-trained Haar Cascade Classifier for face and eye detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
# Initialize the camera (you can specify the camera index, e.g., 0 for the default camera)
cap = cv2.VideoCapture(0)
while True:
# Capture a frame from the camera
ret, frame = cap.read()
# Convert the frame to grayscale for better performance
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
# Draw a rectangle around the detected face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Extract the region of interest (ROI) in the face
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
# Detect eyes in the ROI
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
# Draw a rectangle around the detected eyes
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 0, 255), 2)
# Display the frame with detected faces and eyes
cv2.imshow('Eye Detection', frame)
# Exit the loop if the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera and close all OpenCV windows
cap.release()
# cv2.destroyAllWindows()