Opencv has published some haarcascade models over the years for object detection from images. These models includes face, eyes, person and some other objects. Those models can be downloaded from OpenCV github repository or also we can retrain our models on custom dataset. For list of models, visit this page and you can download any model.
https://github.com/opencv/opencv/tree/master/data/haarcascades
We will use haarcascade frontal face and eye detection for this tutorial. You can also use other haarcascades from this repository.
Face Detection
There are different haarcascades for face available on given url above. We are using haarcascade_frontalface_default.xml
file for this example and you can download it from this url.
https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalcatface.xml
Once, file is downloaded, we can load cascade file using opencv methods.
import cv2
# load haarcascade file
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
Now we can use this to input an image and get bbox coordinates for detections in image. We can either read a single image file or use frames from a video. First we will load an image and apply haarcascade to detect faces from image. We will use detectMultiScale method to detect objects in image which by default only requires an image but we can specify other params also to get required detections. For example, we can specify min and max size of an object detection, number of neighbors for each candidate rectangle to obtain it and scale factor. You can get more details at this url.
https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html
image = cv2.imread("images/group2.jpg")
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray) # returns a list of coordinates
Now we can iterate over face list and draw rectangle using points on images.
# points are in x, y, width, height format.
for (x,y,w,h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (127, 0, 255), 2)
# show image
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Eyes Detection
We can also detect eyes using haarcascades in opencv. We will use haarcascade provided by opencv named haarcascade_eye.xml
from haarcascades list and can be downloaded from this url.
https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
Once downloaded, we can load this using CascadeClassifier
method in opencv.
# load eyes haarcascade file
face_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
Now we can either pass full image to this method or can use face detected from face detection script. When we input only face detected from image, we can avoid false detections that may occur in image. So, we modify previous script as follows.
for (x,y,w,h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (127, 0, 255), 2)
# crop part of image containing face
sub_image = image[y:y+h, x:x+w]
eyes = haarcascade_eye.detectMultiScale(sub_image)
# iterate over detections and draw rect on given sub image
for (x, y, w, h) in eyes:
cv2.rectangle(sub_image, (x, y), (x + w, y + h), (0, 0, 255), 2)
For more details on opencv haarcascades, visit these urls for documentation and tutorials.
Comments (0)