Program :
# Eye detection in video
import cv2
# trained dataset for face (from github)
face_cascade = cv2.CascadeClassifier("face detection/haarcascade_frontalface_default.xml")
# trained dataset for eyes (from github)
eye_cascade = cv2.CascadeClassifier("face detection/haarcascade_eye_tree_eyeglasses.xml")
# for detecting eye in video pass the location of the video as a argument
video = cv2.VideoCapture(0) # 0 is for accessing webcam
while True:
success, frame = video.read()
if success == True:
# converting color frame to grayscale frame
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# getting values of x-axis, y-axis, width, height
faces = face_cascade.detectMultiScale(gray_frame)
""" print(faces) -> return values of x-axis, y-axis, width, height """
# creating rectangle around the face
for x, y, w, h in faces:
# cv2.rectangle(frame, (x, y), ((x + w), (y + h)), (0, 0, 255), 2)
# roi means region of interest
roi_gray = gray_frame[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
# getting values of x-axis, y-axis, width, height for eyes
eyes = eye_cascade.detectMultiScale(roi_gray)
""" print(eyes) -> return values of x-axis, y-axis, width, height """
# creating rectangle around the eyes
for ex, ey, ew, eh in eyes:
cv2.rectangle(roi_color, (ex, ey), ((ex + ew), (ey + eh)), (255, 0, 0), 2)
# Showing the eye detected video
cv2.imshow("Detecting...", frame)
key = cv2.waitKey(1)
if key == 13:
break
else:
break
Comments