OpenCV/Face Detection
From charlesreid1
This page covers how to do facial detection using OpenCV's built-in feature detection algorithms. Note that these require OpenCV >= 2.4.
Contents
Documentation
Face detection can be done using Haar cascade algorithms (general concept) and cascading classifiers (OpenCV class). The main idea is to break down images into smaller components, and look for patterns in brightness variation that match what faces normally look like.
Haar Cascades
There are two approaches to doing face detection via Haar cascades:
- Train your own Haar cascade on images that you want to use (this is useful if you're doing object detection, or if you are looking for a particular component of a face, or a particular shape/color/pattern)
- Use pre-packaged, pre-trained Haar cascades (this is useful if you just want to use face detection as a component of a project)
There are several pre-trained Haar cascades available:
- Main opencv repository (version 3): https://github.com/opencv/opencv/tree/master/data/haarcascades
- Main opencv repository (version 2.4): https://github.com/opencv/opencv/tree/2.4/data/haarcascades
- opencv_contributions repository: https://github.com/opencv/opencv_contrib/tree/master/modules/face/data/cascades
Code
Basic Face Detection Example
Here is a very simple face detection example:
import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') img = cv2.imread('images/bush.jpg') gray = cv2.cvtColor(img, 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),(0,255,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w] # If the 1.07 parameter becomes 1.08 we only detect right eye (???) eyes = eye_cascade.detectMultiScale(roi_gray, 1.07, 1) for (ex,ey,ew,eh) in eyes: cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,0,255),2) cv2.imwrite("detected_faces.jpg", img) cv2.imshow('img',img) cv2.waitKey(0)
The result:
Gist with simple OpenCV facial detection algorithm script: https://gist.github.com/charlesreid1/381bb27eea325d24eeb3a11dbb113dbf