We can use object detection in images using the Haar feature-based cascade classifiers. This object detection method was proposed by Paul Viola and Michael Jones and is a machine learning based approach where the system is trained with many positive and negative images.
Detecting Faces, Cats and Licence plates |
Theory
In this example we detect faces. In order to train the classifier, the algorithm requires a good deal of positive images of faces and negative images without faces.
The system then extracts features from these using the gray scale version of the image. Each feature becomes a single value determined by subtracting the sum of pixels under white rectangle from sum of pixels under black rectangle:
We thus classify into: edge features; line features; and four-square features. David Cameron's hair line looks very much like an edge feature, while his mouth is more like a line feature. To find features we analyse for the changes in the images, such as from dark to light. For eyes we see that the eyes are often darker that the bridge of the nose, so we can look for dark-> light -> dark:
There are a whole lot of filters that have been used to classify objects:
Source code
The following outlines the Python code used:
import numpy as np import cv2 import matplotlib.pyplot as plot import sys imfile = 'http://img.sxsw.com/2015/films/F52361.jpg' imfile = 'F52361.jpg' file='111.jpg' if (len(sys.argv)>1): imfile=str(sys.argv[1]) if (len(sys.argv)>2): file=str(sys.argv[2]) face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') img = cv2.imread( imfile) 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),(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) file1 = "filename" cv2.imwrite(file1,img)
and the smile part is:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') smile_cascade = cv2.CascadeClassifier('smile.xml') img = cv2.imread( imfile) 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),(255,0,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w] smile = smile_cascade.detectMultiScale( roi_gray, scaleFactor= 1.7, minNeighbors=22, minSize=(25, 25) ) for (ex,ey,ew,eh) in smile: cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,0,255),2)