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中lambda函数 list comprehension 和 zip函数使用指南
Sep 28 Python
浅谈python中的__init__、__new__和__call__方法
Jul 18 Python
Python使用pip安装报错:is not a supported wheel on this platform的解决方法
Jan 23 Python
使用Python更换外网IP的方法
Jul 09 Python
python 读取dicom文件,生成info.txt和raw文件的方法
Jan 24 Python
python实现抽奖小程序
Apr 15 Python
Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】
Jun 18 Python
PyQt5根据控件Id获取控件对象的方法
Jun 25 Python
python3.6+django2.0+mysql搭建网站过程详解
Jul 24 Python
解决Django migrate不能发现app.models的表问题
Aug 31 Python
简单了解python中的与或非运算
Sep 18 Python
200行python代码实现贪吃蛇游戏
Apr 24 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实现的汉字拼音转换和公历农历转换类及使用示例
2014/07/01 PHP
PHP编程基本语法快速入门手册
2016/01/07 PHP
phpMyAdmin无法登陆的解决方法
2017/04/27 PHP
PHP实现字符串翻转功能的方法【递归与循环算法】
2017/11/03 PHP
让GoogleCode的SVN下的HTML文件在FireFox下正常显示.
2009/05/25 Javascript
google地图的路线实现代码
2009/08/20 Javascript
jQuery渐变发光导航菜单的实例代码
2013/03/27 Javascript
javascript中字符串的定义示例代码
2013/12/19 Javascript
JavaScript使用function定义对象并调用的方法
2015/03/23 Javascript
DEDECMS如何为文章添加HOT NEW标志图片
2015/08/14 Javascript
使用jQuery制作基础的Web图片轮播效果
2016/04/22 Javascript
DIV+CSS+jQ实现省市联动可扩展
2016/06/22 Javascript
Angular懒加载机制刷新后无法回退的快速解决方法
2016/08/30 Javascript
JS实现页面打印功能
2017/03/16 Javascript
完美实现js拖拽效果 return false用法详解
2017/07/28 Javascript
使用Node.js实现ORM的一种思路详解(图文)
2017/10/24 Javascript
vue 根据数组中某一项的值进行排序的方法
2018/08/30 Javascript
Vue使用NPM方式搭建项目
2018/10/25 Javascript
vue 指令和过滤器的基本使用(品牌管理案例)
2019/11/04 Javascript
Vue 封装防刷新考试倒计时组件的实现
2020/06/05 Javascript
JS实现canvas简单小画板功能
2020/06/23 Javascript
js实现贪吃蛇游戏(简易版)
2020/09/29 Javascript
python解析模块(ConfigParser)使用方法
2013/12/10 Python
浅谈Python中的作用域规则和闭包
2018/03/20 Python
Python提取特定时间段内数据的方法实例
2019/04/01 Python
Python networkx包的实现
2020/02/14 Python
在PyCharm中遇到pip安装 失败问题及解决方案(pip失效时的解决方案)
2020/03/10 Python
Python爬取YY评级分数并保存数据实现过程解析
2020/06/01 Python
HTML5表格_动力节点Java学院整理
2017/07/11 HTML / CSS
Currentbody法国:健康与美容高科技产品
2020/08/16 全球购物
2015年学生管理工作总结
2015/05/26 职场文书
办公室日常管理制度
2015/08/04 职场文书
2016中秋节晚会开场白
2015/11/26 职场文书
公务员廉洁从政心得体会
2016/01/19 职场文书
python 模拟在天空中放风筝的示例代码
2021/04/21 Python
MySQL 外键约束和表关系相关总结
2021/06/20 MySQL