CV:利用cv2自带两步法haarcascade_frontalcatface.xml实现对猫脸检测

CV:利用cv2自带两步法haarcascade_frontalcatface.xml实现对猫脸检测


输出结果

实现代码

@author: niu
'''
import cv2

catPath = "haarcascade_frontalcatface.xml"
faceCascade = cv2.CascadeClassifier(catPath)

img = cv2.imread("F:\File_Python\Resources\cat.jpg")  

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor= 1.02,
    minNeighbors=3,
    minSize=(150, 150),
    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)
    cv2.putText(img,'Persian cat',(x,y-7), 3, 1.2, (0, 255, 0), 2, cv2.LINE_AA)

cv2.imshow('Jason niu', img)
cv2.imwrite("cat.jpg",img)
c = cv2.waitKey(0)

参考文献
CV:利用OpenCV的CascadeClassifier级联分类器加载猫脸检测文件haarcascade_frontalcatface.xml实现对猫脸检测并加文本cat

(0)

相关推荐