From charlesreid1

No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Main|OpenCV}}
This page covers how to do facial detection using OpenCV's built-in feature detection algorithms. Note that these require OpenCV >= 2.4.


This page covers how to do facial detection using OpenCV's built-in feature detection algorithms. Note that these require OpenCV > 2.4.
=Documentation=


==Basic Template==
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:
 
<pre>
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)
</pre>
 
The result:
 
[[Image:DetectedFaces.jpg|200px]]


Gist with simple OpenCV facial detection algorithm script: https://gist.github.com/charlesreid1/381bb27eea325d24eeb3a11dbb113dbf
Gist with simple OpenCV facial detection algorithm script: https://gist.github.com/charlesreid1/381bb27eea325d24eeb3a11dbb113dbf


=Related=


* [[OpenCV]]


 
* [[RaspberryPi/USB Camera]]


=Flags=
=Flags=

Latest revision as of 08:31, 27 October 2017

This page covers how to do facial detection using OpenCV's built-in feature detection algorithms. Note that these require OpenCV >= 2.4.

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:

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:

DetectedFaces.jpg

Gist with simple OpenCV facial detection algorithm script: https://gist.github.com/charlesreid1/381bb27eea325d24eeb3a11dbb113dbf

Related

Flags