0% found this document useful (0 votes)
17 views1 page

Eye and Face Dectection

The document contains a Python script that uses OpenCV to perform real-time face and eye detection using pre-trained Haar Cascade classifiers. It captures video from the webcam, detects faces and eyes in each frame, and draws rectangles around them. The process continues until the user presses 'q' to exit.

Uploaded by

merintijaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Eye and Face Dectection

The document contains a Python script that uses OpenCV to perform real-time face and eye detection using pre-trained Haar Cascade classifiers. It captures video from the webcam, detects faces and eyes in each frame, and draws rectangles around them. The process continues until the user presses 'q' to exit.

Uploaded by

merintijaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import cv2

# Load the pre-trained Haar Cascade classifiers for face and eye detection
face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')
eye_cascade = [Link]([Link] + 'haarcascade_eye.xml')

# Initialize the webcam


cap = [Link](0)

while True:
# Capture frame-by-frame from the webcam
ret, frame = [Link]()

if not ret:
break

# Convert the frame to grayscale as the detector works on grayscale images


gray = [Link](frame, cv2.COLOR_BGR2GRAY)

# Detect faces in the grayscale frame


faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)

# Loop over the detected faces


for (x, y, w, h) in faces:
# Draw a rectangle around each face
[Link](frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Region of interest for the face in the grayscale image


roi_gray = gray[y:y + h, x:x + w]
# Region of interest for the face in the color image
roi_color = frame[y:y + h, x:x + w]

# Detect eyes within the detected face region


eyes = eye_cascade.detectMultiScale(roi_gray, scaleFactor=1.1,
minNeighbors=10)

# Loop over the detected eyes


for (ex, ey, ew, eh) in eyes:
# Draw a rectangle around each eye
[Link](roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

# Display the resulting frame with the rectangles drawn around faces and eyes
[Link]('Face and Eye Detection', frame)

# Press 'q' to exit the loop


if [Link](1) & 0xFF == ord('q'):
break

# Release the webcam and close all OpenCV windows


[Link]()
[Link]()

You might also like