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 相关文章推荐
Django中对通过测试的用户进行限制访问的方法
Jul 23 Python
Python实现字典的key和values的交换
Aug 04 Python
Python编程入门之Hello World的三种实现方式
Nov 13 Python
python学习必备知识汇总
Sep 08 Python
解决Django模板无法使用perms变量问题的方法
Sep 10 Python
jupyter notebook引用from pyecharts.charts import Bar运行报错
Apr 23 Python
Python实现删除时保留特定文件夹和文件的示例
Apr 27 Python
解决使用pycharm提交代码时冲突之后文件丢失找回的方法
Aug 05 Python
python远程邮件控制电脑升级版
May 23 Python
python  logging日志打印过程解析
Oct 22 Python
Python concurrent.futures模块使用实例
Dec 24 Python
什么是python的必选参数
Jun 21 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
PHP4和PHP5性能测试和对比 测试代码与环境
2007/08/17 PHP
PHP+javascript制作带提示的验证码源码分享
2014/05/28 PHP
php实现字符串首字母大写和单词首字母大写的方法
2015/03/14 PHP
php mongodb操作类 带几个简单的例子
2016/08/25 PHP
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
2016/09/11 PHP
JS 显示当前日期与时间的代码
2010/03/24 Javascript
IE6-IE9不支持table.innerHTML的解决方法分享
2012/09/14 Javascript
jQuery+CSS 半开折叠效果原理及代码(自写)
2013/03/04 Javascript
jQuery实现新消息闪烁标题提示的方法
2015/03/11 Javascript
jQuery使用addClass()方法给元素添加多个class样式
2015/03/26 Javascript
Angular 输入框实现自定义验证功能
2017/02/19 Javascript
js-FCC算法-No repeats please字符串的全排列(详解)
2017/05/02 Javascript
BootStrap Table前台和后台分页对JSON格式的要求
2017/06/28 Javascript
react-native组件中NavigatorIOS和ListView结合使用的方法
2017/09/30 Javascript
d3.js实现自定义多y轴折线图的示例代码
2018/05/30 Javascript
原生JS实现轮播图效果
2018/10/12 Javascript
jQuery实现中奖播报功能(让文本滚动起来) 简单设置数值即可
2020/03/20 jQuery
[16:43]Heroes19_剃刀(完美)
2014/10/31 DOTA
快速入手Python字符编码
2016/08/03 Python
SQLite3中文编码 Python的实现
2017/01/11 Python
python 剪切移动文件的实现代码
2018/08/02 Python
Python Django view 两种return的实现方式
2020/03/16 Python
python使用opencv resize图像不进行插值的操作
2020/07/05 Python
Sublime Text3最新激活注册码分享适用2020最新版 亲测可用
2020/11/12 Python
Python环境搭建过程从安装到Hello World
2021/02/05 Python
html5新增的定时器requestAnimationFrame实现进度条功能
2018/12/13 HTML / CSS
Blue Nile台湾:钻石珠宝商,订婚首饰、结婚戒指和精品首饰
2017/11/24 全球购物
美国婴儿和儿童家具网上商店:ABaby.com
2018/07/02 全球购物
点菜员岗位职责范本
2014/02/14 职场文书
成龙洗发水广告词
2014/03/14 职场文书
房产授权委托书范本
2014/09/22 职场文书
2014银行授权委托书样本
2014/10/04 职场文书
社区党风廉政建设调研报告
2015/01/01 职场文书
大学生就业意向书
2015/05/11 职场文书
2015年物流客服工作总结
2015/07/27 职场文书
MySql分区类型及创建分区的方法
2022/04/13 MySQL