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

yipeiwu_com5年前Python基础

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)

检测效果:

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设计】。

相关文章

对python中的argv和argc使用详解

主要问题 为什么argv中第一个,即index=0的内容就是文件名? python中argc是用什么实现的? 概念解释 argc:argument counter,命令行参数个数 ar...

python 生成器协程运算实例

一、yield运行方式 我们定义一个如下的生成器: def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) wh...

python 普通克里金(Kriging)法的实现

python 普通克里金(Kriging)法的实现

克里金法时一种用于空间插值的地学统计方法。 克里金法用半变异测定空间要素,要素即自相关要素。 半变异公式为: 其中γ(h) 是已知点 xi 和 xj 的半变异,***h***表示...

在Python中利用Into包整洁地进行数据迁移的教程

在Python中利用Into包整洁地进行数据迁移的教程

动机 我们花费大量的时间将数据从普通的交换格式(比如CSV),迁移到像数组、数据库或者二进制存储等高效的计算格式。更糟糕的是,许多人没有将数据迁移到高效的格式,因为他们不知道怎么(或者不...

如何基于Python制作有道翻译小工具

如何基于Python制作有道翻译小工具

这篇文章主要介绍了如何基于Python制作有道翻译小工具,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 该工具主要是利用了爬虫,爬取w...