python检测猫脸

本人喜欢猫,今天用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()

效果展示:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I'm 程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值