From charlesreid1

No edit summary
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.
 
=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
 
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=
 
==Basic Face Detection==
 
 
 
<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('face.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)
</pre>


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


==Basic Template==


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




==Raspberry Pi==
=Related=
 
[[OpenCV]]


Related page: [RaspberryPi/USB Camera]]
[[RaspberryPi/USB Camera]]


=Flags=
=Flags=

Revision as of 07:17, 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:

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

Basic Face Detection

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

img = cv2.imread('face.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)


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


Related

OpenCV

RaspberryPi/USB Camera

Flags