Face and Eye Detection — using OpenCV

Soumya Patil
3 min readOct 13, 2019

--

OpenCV uses machine learning algorithms to search for faces within a picture. Because faces are so complicated, there isn’t one simple test that will tell you if it found a face or not.

We are going to use Haar Cascade. A Haar Cascade is basically a classifier which is used to detect the object for which it has been trained for, from the source.

The methodology of face detection can be applied to landmark (e.g., eyes, nose tip, and mouth) localization, which can then be utilized to face geometrical normalization.

Environment Setup required :

  1. Jupyter Notebook
  2. Python — OpenCV, numpy
  3. Insert haarcascade_eye.xml & haarcascade_frontalface_default.xml files in the same folder.

Features:

Recognize and locate facial features: Get the coordinates of the eyes, ears, cheeks, nose, and mouth of every face detected.

Get the contours of facial features: Get the contours of detected faces and their eyes, eyebrows, lips, and nose.

Recognize facial expressions: Determine whether a person is smiling or has their eyes closed.

Track faces across video frames: Get an identifier for each individual person’s face that is detected. This identifier is consistent across invocations, so you can, for example, perform image manipulation on a particular person in a video stream.

Process video frames in real-time: Face detection is performed on the device, and is fast enough to be used in real-time applications, such as video manipulation.

Algorithm:

The haar-like algorithm is also used for feature selection or feature extraction for an object in an image, with the help of edge detection, line detection, centre detection for detecting eyes, nose, mouth, etc. in the picture. It is used to select the essential features in an image and extract these features for face detection.

import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
#save the image(i) in the same directoryimg = cv2.imread("friends.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

Summary :

There is as such no restriction to the number of faces this algorithm can detect. The code can detect faces, but it would still require verification from the user. This is therefore not a fully intelligent system since it requires interaction from the user.

Can the Haar Cascades detect animal face and eyes ??

Well, go try it out ; )

--

--