10-1. Face Detection

from imutils import *
image = imread('face.png')
show(image)

1. image: input image
2. scaleFactor = 1.1: This ratio is an image reduction per default is 1.1
3. minNeighbors =. 3: a rectangular frame around the number of matches needed for the success of each feature are matched to the region a rectangular frame, a plurality of rectangular frames exist only when it is considered a successful match, such as the face, the default is 3.
4. minSize: minimum range matching face
5. flags = 0: can take the following values: 
CASCADE_DO_CANNY_PRUNING =. 1, using the canny edge detector to exclude some of the edge with little or a lot of image regions 
CASCADE_SCALE_IMAGE = 2, the normal proportional detector 
CASCADE_FIND_BIGGEST_OBJECT = 4, only the largest detected object 
CASCADE_DO_ROUGH_SEARCH = 8 little early detection

# 级联分类器
detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=2, minSize=(10,10), flags=cv2.CASCADE_SCALE_IMAGE)

for (x,y,w,h) in rects:
    # 画矩形框
    cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)
    
show(image)

def facedetect(image):
    image = imread(image)
    # 级联分类器
    detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=2, minSize=(10,10), flags=cv2.CASCADE_SCALE_IMAGE)

    for (x,y,w,h) in rects:
        # 画矩形框
        cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)

    show(image)
facedetect('Solvay.jpg')

Guess you like

Origin www.cnblogs.com/liuwenhua/p/11891004.html