CV:利用cv2自带两步法haarcascade_frontalface_default.xml、_smile.xml实现对人脸、笑脸同时检测

CV:利用cv2自带两步法haarcascade_frontalface_default.xml、_smile.xml实现对人脸、笑脸同时检测


输出结果

实现代码

#CV:利用cv2.CascadeClassifier级联分类器的haarcascade_frontalface_default.xml、haarcascade_smile.xml实现对人脸、笑脸同时检测——Jason niu
import cv2

smilePath = "F:\Program Files\Python\Python36\Lib\site-packages\cv2\haarcascade_smile.xml"
smileCascade = cv2.CascadeClassifier(smilePath)

img = cv2.imread("F:\File_Python\Resources\smile.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor= 1.1,
    minNeighbors=8,
    minSize=(55, 55),
    flags=cv2.CASCADE_SCALE_IMAGE
)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]

    smile = smileCascade.detectMultiScale(
        roi_gray,
        scaleFactor= 1.16,
        minNeighbors=35,
        minSize=(25, 25),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    for (x2, y2, w2, h2) in smile:
        cv2.rectangle(roi_color, (x2, y2), (x2+w2, y2+h2), (255, 0, 0), 2)
        cv2.putText(img,'Smile',(x,y-7), 3, 1.2, (0, 255, 0), 2, cv2.LINE_AA)

cv2.imshow('Smile test', img)
#cv2.imwrite("smile.jpg",img)
c = cv2.waitKey(0)

参考文献:
CV:利用cv2.CascadeClassifier级联分类器的haarcascade_frontalface_default.xml、haarcascade_smile.xml实现对人脸、笑脸同时检测

(0)

相关推荐