Saturday, August 1, 2020

Eye Detection

Eye detection is another fascinating application of computer vision which makes it more realistic as well as futuristic. OpenCV has a built-in facility to perform eye detection. We are going to use the Haar cascade classifier for eye detection.

Example

The following example gives the Python code using Haar Cascade to detect the face of Amitabh Bachan given in the following image:


Import OpenCV package as shown:

import cv2
import numpy as np


Now, use the HaarCascadeClassifier for detecting face:

eye_cascade = cv2.CascadeClassifier('D:/ProgramData/cascadeclassifier/haarcascade_eye.xml')

Now, for reading a particular image, use the imread() function:

img = cv2.imread('AB_Eye.jpg')

Now, convert it into grayscale because it would accept grey images:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Now with the help of eye_cascade.detectMultiScale, perform actual face detection:

eyes = eye_cascade.detectMultiScale(gray, 1.03, 5)

Now, draw a rectangle around the whole face:

for (ex,ey,ew,eh) in eyes:
    img = cv2.rectangle(img,(ex,ey),(ex+ew, ey+eh),(0,255,0),2)
cv2.imwrite('Eye_AB.jpg',img)

This Python program will create an image named Eye_AB.jpg with eye detection as shown:


In last few posts we saw a few applications of CV in brief. Our next topic of discussion would be Deep learning.
Share:

0 comments:

Post a Comment