From charlesreid1

 
(4 intermediate revisions by the same user not shown)
Line 15: Line 15:
* Main opencv repository (version 2.4): https://github.com/opencv/opencv/tree/2.4/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
* opencv_contributions repository: https://github.com/opencv/opencv_contrib/tree/master/modules/face/data/cascades
This page contains an example of how to use the Haar cascade to detect multiple faces and eyes in a photograph: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html#face-detection


=Code=
=Code=


==Basic Face Detection==
==Basic Face Detection Example==
 


Here is a very simple face detection example:


<pre>
<pre>
Line 31: Line 29:
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')


img = cv2.imread('face.jpg')
img = cv2.imread('images/bush.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


faces = face_cascade.detectMultiScale(gray, 1.3, 5)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
for (x,y,w,h) in faces:
     cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
     cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
     roi_gray = gray[y:y+h, x:x+w]
     roi_gray = gray[y:y+h, x:x+w]
     roi_color = img[y:y+h, x:x+w]
     roi_color = img[y:y+h, x:x+w]
     eyes = eye_cascade.detectMultiScale(roi_gray)
    # 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:
     for (ex,ey,ew,eh) in eyes:
         cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
         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.imshow('img',img)
cv2.waitKey(0)
cv2.waitKey(0)
</pre>
</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
Line 53: Line 55:
=Related=
=Related=


[[OpenCV]]
* [[OpenCV]]


[[RaspberryPi/USB Camera]]
* [[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