本人喜欢猫,今天用python完成了对猫的检测
代码:
# -*- coding: utf-8 -*-
"""
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) # 转换灰色
# OpenCV猫脸识别分类器
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))
#faceRects = classifier.detectMultiScale( gray, scaleFactor=1.3, minNeighbors=3, minSize=(20, 20))
if len(faceRects): # 大于0则检测到猫脸
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()
效果展示:



