In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import math
import cv2
A reference of detectMultiScale parameter
http://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-detectmultiscale
In [2]:
def faceDetect(imagePath, cascadeClassifier="haarcascade_frontalface_alt.xml", scaleFactor = 1.05,
minNeighbors=3, minSize = (60,60)):
faceCascade = cv2.CascadeClassifier(cascadeClassifier)
# Read image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = faceCascade.detectMultiScale(image=gray, scaleFactor=scaleFactor, minNeighbors=minNeighbors, minSize=minSize)
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Show image
plt.imshow(image)
In [3]:
faceDetect('b2.jpg', "haarcascade_frontalface_alt.xml", 1.05)
In [4]:
# Miss detection at scaleFactor = 1.1
faceDetect('b2.jpg', "haarcascade_frontalface_alt.xml", 1.1)
In [5]:
# Miss detection at minNeighbors = 5
faceDetect('b2.jpg', "haarcascade_frontalface_alt.xml", 1.05, 5)
In [6]:
# Miss detection with haarcascade_frontalface_alt2.xml
faceDetect('b2.jpg', "haarcascade_frontalface_alt2.xml", 1.05)
In [7]:
# Miss detection with haarcascade_frontalface_alt_tree.xml
faceDetect('b2.jpg', "haarcascade_frontalface_alt_tree.xml", 1.05)
In [8]:
# Error occur with haarcascade_frontalface_default.xml
faceDetect('b2.jpg', "haarcascade_frontalface_default.xml", 1.05)