EYE DETECTION IN IMAGE FILE USING OPENCV AND PYTHON

Program :

# Eye Detection in Python
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")

# read image
img = cv2.imread("images/elonmusk.jpg")

# convert colour image to grayscale image
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# getting values of x-axis, y-axis, width, height for face
faces = face_cascade.detectMultiScale(gray_img)
""" 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(img, (x, y), ((x + w), (y + h)), (0, 0, 255), 2)"""

# roi means region of interest
roi_gray = gray_img[y:y+h, x:x+w]
roi_color = img[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)

# show image
cv2.imshow("Detecting...", img)
key = cv2.waitKey()
if key == 13:
exit()

Output :


Comments

Popular posts from this blog

MOTION DETECTION AND TRACKING USING OPENCV AND PYTHON

COLOR DETECTION USING OPENCV AND PYTHON

DETECTING ACCURACY LEVEL OF THE KNOWN AND TEST FACES USING PYTHON