本人喜欢猫,今天用python完成了对猫的检测 代码:
"""
Created on Wed Oct 16 09:23:38 2019
@author: Administrator
"""
import cv2
filepath = "E:\\the_data\\opencv\\cat5.jpg"
img = cv2.imread(filepath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
classifier = cv2.CascadeClassifier( "E:\\the_data\\opencv\\xml\\lbpcascade_frontalcatface.xml" )
color = (0, 255, 0)
faceRects = classifier.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=3, minSize=(5, 5))
if len(faceRects):
for faceRect in faceRects:
x, y, w, h = faceRect
cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
cv2.imshow("image", img)
c = cv2.waitKey(10)
cv2.waitKey(0)
cv2.destroyAllWindows()
效果展示:
|