In [21]:
#adapted from http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html
import numpy as np
import cv2
from matplotlib import pyplot as plt
%matplotlib inline

#change to your haar cascade directory
face_cascade = cv2.CascadeClassifier('/Users/andrewjtimmons/anaconda/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/Users/andrewjtimmons/anaconda/share/OpenCV/haarcascades/haarcascade_eye.xml')
mouth_cascade = cv2.CascadeClassifier('/Users/andrewjtimmons/anaconda/share/OpenCV/haarcascades/haarcascade_mcs_mouth.xml')
nose_cascade = cv2.CascadeClassifier('/Users/andrewjtimmons/anaconda/share/OpenCV/haarcascades/haarcascade_mcs_nose.xml')
smile_cascade = cv2.CascadeClassifier('/Users/andrewjtimmons/anaconda/share/OpenCV/haarcascades/haarcascade_smile.xml')

In [28]:
#Load the image
img = cv2.imread("skull.jpg")

In [29]:
#convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print gray


[[159 158 157 ..., 191 192 192]
 [161 160 158 ..., 192 193 194]
 [158 157 156 ..., 192 193 194]
 ..., 
 [110 102 107 ...,  91  92  95]
 [111 104 110 ...,  93  95  98]
 [114 108 114 ...,  95  98  99]]

Faces


In [36]:
#show faces

# repasting these so you can draw on a fresh image and play
# with scaleFactor and minNeighbors
img = cv2.imread("skull.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#settings for detection
scaleFactor = 1.05
minNeighbors = 3

#detects the faces
faces = face_cascade.detectMultiScale(img, scaleFactor = scaleFactor, minNeighbors = minNeighbors)

#draws boxes around possible faces
for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

#shows the image
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Eyes


In [41]:
#add in eyes

# repasting these so you can draw on a fresh image and play
# with scaleFactor and minNeighbors
img = cv2.imread("skull.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

scaleFactor = 1.05
minNeighbors = 6

faces = face_cascade.detectMultiScale(img, scaleFactor = scaleFactor, minNeighbors = minNeighbors)

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, scaleFactor = scaleFactor, minNeighbors = minNeighbors)
    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)
cv2.destroyAllWindows()

Mouths


In [40]:
#add mouths and eyes

# repasting these so you can draw on a fresh image and play
# with scaleFactor and minNeighbors
img = cv2.imread("skull.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

scaleFactor = 1.05
minNeighbors = 6
faces = face_cascade.detectMultiScale(img, scaleFactor = scaleFactor, minNeighbors = minNeighbors)

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, scaleFactor = scaleFactor, minNeighbors = minNeighbors)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    mouth = mouth_cascade.detectMultiScale(roi_gray, scaleFactor = scaleFactor, minNeighbors = minNeighbors)
    for (sx,sy,sw,sh) in mouth:
        cv2.rectangle(roi_color, (sx,sy), (sx+sw, sy+sh), (255,255,255),2)
        
    smile = smile_cascade.detectMultiScale(roi_gray, scaleFactor = scaleFactor, minNeighbors = minNeighbors)
    for (sx,sy,sw,sh) in smile:
        cv2.rectangle(roi_color, (sx,sy), (sx+sw, sy+sh), (255,0,255),2)
     
        
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In [7]:
###Cake

In [43]:
#That seems good out of the box.  What happens if there are no faces?

# repasting these so you can draw on a fresh image and play
# with scaleFactor and minNeighbors
img = cv2.imread("dessert.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

scaleFactor = 1.05
minNeighbors = 6
faces = face_cascade.detectMultiScale(img, scaleFactor = scaleFactor, minNeighbors = minNeighbors)

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, scaleFactor = scaleFactor, minNeighbors = minNeighbors)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    mouth = mouth_cascade.detectMultiScale(roi_gray, scaleFactor = scaleFactor, minNeighbors = minNeighbors)
    for (sx,sy,sw,sh) in mouth:
        cv2.rectangle(roi_color, (sx,sy), (sx+sw, sy+sh), (255,255,255),2)
        
    smile = smile_cascade.detectMultiScale(roi_gray, scaleFactor = scaleFactor, minNeighbors = minNeighbors)
    for (sx,sy,sw,sh) in smile:
        cv2.rectangle(roi_color, (sx,sy), (sx+sw, sy+sh), (255,0,255),2)
     
        
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()