python调用opencv实现猫脸检测功能


Posted in Python onJanuary 15, 2019

Python 小猫检测,通过调用opencv自带的猫脸检测的分类器进行检测。

分类器有两个:haarcascade_frontalcatface.xml和
haarcascade_frontalcatface_extended.xml。可以在opencv的安装目录下找到

D:\Program Files\OPENCV320\opencv\sources\data\haarcascades

小猫检测代码为:

1. 直接读取图片调用

import cv2

image = cv2.imread("cat_04.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# load the cat detector Haar cascade, then detect cat faces
# in the input image
detector = cv2.CascadeClassifier("haarcascade_frontalcatface.xml")
#haarcascade_frontalcatface_extended.xml
rects = detector.detectMultiScale(gray, scaleFactor=1.1,
 minNeighbors=10, minSize=(100, 100))
# loop over the cat faces and draw a rectangle surrounding each

print (enumerate(rects))

for (i, (x, y, w, h)) in enumerate(rects):
 cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
 cv2.putText(image, "Cat #{}".format(i + 1), (x, y - 10),
 cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)
 print (i, x,y,w,h)
# show the detected cat faces
cv2.imshow("Cat Faces", image)
cv2.waitKey(1)

检测效果:

python调用opencv实现猫脸检测功能

2. 通过命令控制符调用

也可以通过调用argparse库,进行整体调用

新建cat_detect.py文件

# import the necessary packages
import argparse
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,

 help="path to the input image")
ap.add_argument("-c", "--cascade", default="haarcascade_frontalcatface_extended.xml", 
 help="path to cat detector haar cascade")

args = vars(ap.parse_args())
#"haarcascade_frontalcatface_extended.xml",

# load the input image and convert it to grayscale
#image = cv2.imread(args["image"])
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# load the cat detector Haar cascade, then detect cat faces

# in the input image
detector = cv2.CascadeClassifier(args["cascade"])
rects = detector.detectMultiScale(gray, scaleFactor=1.1,

 minNeighbors=10, minSize=(120, 120)) # cat good

# loop over the cat faces and draw a rectangle surrounding each
print (enumerate(rects))
for (i, (x, y, w, h)) in enumerate(rects):

 cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
 cv2.putText(image, "cat #{}".format(i + 1), (x, y - 10),
 cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)
# show the detected cat faces
cv2.imshow("Cat Faces", image)
cv2.waitKey(0)

通过“命令控制符”调用

cmd
cd E:\WORK\py\detectCat
E:\WORK\py\detectCat>python cat_detector.py --image cat_07.png

python调用opencv实现猫脸检测功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
学习python处理python编码问题
Mar 13 Python
python简单读取大文件的方法
Jul 01 Python
Python实现GUI学生信息管理系统
Apr 05 Python
python flask实现分页的示例代码
Aug 02 Python
python web自制框架之接受url传递过来的参数实例
Dec 17 Python
对python读取CT医学图像的实例详解
Jan 24 Python
python远程邮件控制电脑升级版
May 23 Python
Django框架使用内置方法实现登录功能详解
Jun 12 Python
python字典改变value值方法总结
Jun 21 Python
深入浅析Python 中的sklearn模型选择
Oct 12 Python
python 消除 futureWarning问题的解决
Dec 25 Python
从Pytorch模型pth文件中读取参数成numpy矩阵的操作
Mar 04 Python
python可视化实现代码
Jan 15 #Python
Python饼状图的绘制实例
Jan 15 #Python
Python设计模式之状态模式原理与用法详解
Jan 15 #Python
Python设计模式之适配器模式原理与用法详解
Jan 15 #Python
Python设计模式之备忘录模式原理与用法详解
Jan 15 #Python
matplotlib.pyplot绘图显示控制方法
Jan 15 #Python
python实现彩色图转换成灰度图
Jan 15 #Python
You might like
PHP.MVC的模板标签系统(三)
2006/09/05 PHP
php下检测字符串是否是utf8编码的代码
2008/06/28 PHP
php异常处理技术,顶级异常处理器
2012/06/13 PHP
php过滤html标记属性类用法实例
2014/09/23 PHP
PHP+JS实现大规模数据提交的方法
2015/07/02 PHP
学习php设计模式 php实现模板方法模式
2015/12/08 PHP
php 与 nginx 的处理方式及nginx与php-fpm通信的两种方式
2018/09/28 PHP
jquery HotKeys轻松搞定键盘事件代码
2008/08/30 Javascript
js弹出层包含flash 不能关闭隐藏的2种处理方法
2013/06/17 Javascript
JS实现的论坛Ajax打分效果完整实例
2015/10/31 Javascript
jQuery实现摸拟alert提示框
2016/05/22 Javascript
jqueryMobile 动态添加元素,展示刷新视图的实现方法
2016/05/28 Javascript
Angular学习笔记之angular的$filter服务浅析
2016/11/12 Javascript
JQuery学习总结【一】
2016/12/01 Javascript
JS组件系列之JS组件封装过程详解
2017/04/28 Javascript
在iframe中使bootstrap的模态框在父页面弹出问题
2017/08/07 Javascript
JS+Canvas绘制动态时钟效果
2017/11/10 Javascript
Bootstrap实现的表格合并单元格示例
2018/02/06 Javascript
详解基于electron制作一个node压缩图片的桌面应用
2019/01/29 Javascript
深入剖析Python的爬虫框架Scrapy的结构与运作流程
2016/01/20 Python
Python 网络爬虫--关于简单的模拟登录实例讲解
2018/06/01 Python
Python编程学习之如何判断3个数的大小
2019/08/07 Python
PyTorch之图像和Tensor填充的实例
2019/08/18 Python
pytorch中tensor.expand()和tensor.expand_as()函数详解
2019/12/27 Python
python基于opencv检测程序运行效率
2019/12/28 Python
Python爬虫headers处理及网络超时问题解决方案
2020/06/19 Python
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
2020/07/23 Python
python的flask框架难学吗
2020/07/31 Python
WoolOvers澳洲官方网站:英国针织服装公司
2018/05/13 全球购物
大学新生入学教育方案
2014/05/16 职场文书
企业领导对照检查材料
2014/08/20 职场文书
2015年社区服务活动总结
2015/03/25 职场文书
2019学生会干事辞职信
2019/06/27 职场文书
曾国藩励志经典名言37句,蕴含哲理
2019/10/14 职场文书
超详细Python解释器新手安装教程
2021/05/10 Python
【TED出品】天梯非主流开心游1700 划水骑士
2022/03/31 魔兽争霸