Easy Face Detection using OpenCV and Python

A lot of applications and websites incorporate face detection features , specially social network sites and photo/image databases. Using OpenCV, it is very easy to detect faces within images or even live video or a webcam.

OpenCV has a component called Haar Cascade Classifier which does all the heavy lifting.  For most simple purposes, the classifier works out of the box without requiring any training or modification. There are predefined classifier data for detection of faces, eyes, smiles, upper body, lower body etc.

The only drawback is that only frontal faces are detected and faces which are at an angle or turned sideways are difficult to detect using the built-in data. To overcome that limitation to some extent, the sample code below uses a second classifier for faces in profile as well. So basically, the target image is scanned for frontal faces and faces in profile and both the results are independently stored. All identified faces are highlighted in blue rectangles.

The sample code also contains eyes detection, which if found in an image, is highlighted with green rectangles. Example outputs are given below:

facedetect

 

 

 

 

 

 

 

 

 

facedetect

 

 

 

 

 

 

 

facedetect

 

 

 

 

 

 

 

 

 

 

 

 

The two outputs below are obtained from the same image – one detected frontal faces, the other detected faces in profile.

facedetect facedetectprofile

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The sample code is given below. You have to change the paths as required. The paths for the Haar classifiers need to be absolute and not relative.

import numpy as np
import cv2
import copy

face_cascade = cv2.CascadeClassifier('/var/projects/opencv-3.0.0/data/haarcascades/haarcascade_frontalface_default.xml')
alt_face_cascade = cv2.CascadeClassifier('/var/projects/opencv-3.0.0/data/haarcascades/haarcascade_profileface.xml')
eye_cascade = cv2.CascadeClassifier('/var/projects/opencv-3.0.0/data/haarcascades/haarcascade_eye.xml')
img = cv2.imread('/home/amit/Downloads/faces.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = copy.copy(img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
     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.imwrite("/home/amit/Downloads/facedetect.png", img)

faces = alt_face_cascade.detectMultiScale(gray2, 1.3, 5)
for (x,y,w,h) in faces:
     cv2.rectangle(img2,(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.imwrite("/home/amit/Downloads/facedetectprofile.png", img2)

Be the first to comment

Leave a Reply

Your email address will not be published.


*