opencv之人脸检测之cv2.CascadeClassifier

import cv2

import os.path

def detect(filename, cascade_file = "haarcascade_frontalface_alt.xml"):
    if not os.path.isfile(cascade_file):
        raise RuntimeError("%s: not found" % cascade_file)

    cascade = cv2.CascadeClassifier(cascade_file)
    image = cv2.imread(filename)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)

    faces = cascade.detectMultiScale(gray,
                                     # detector options
                                     scaleFactor = 1.1,
                                     minNeighbors = 5,
                                     minSize = (24, 24))
    i=0
    for (x, y, w, h) in faces:
        i+=1
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
        temp=image[y:y+h,x:x+w,:]
        cv2.imwrite('%s_%d.jpg'%(os.path.basename(filename).split('.')[0],i),temp)
    cv2.imshow("AnimeFaceDetect", image)
    cv2.waitKey(0)
    cv2.imwrite("out.png", image)



detect('face.png')

opencv安装好之后,在对应的文件夹里搜索haarcascade_frontalface_alt.xml文件,放到这个程序的同级目录下,直接运行即可。

运行效果:
opencv之人脸检测之cv2.CascadeClassifier_第1张图片

你可能感兴趣的:(opencv)