Python下应用opencv 实现人脸检测功能


Posted in Python onOctober 24, 2019

使用OpenCV's Haar cascades作为人脸检测,因为他做好了库,我们只管使用。

代码简单,除去注释,总共有效代码只有10多行。

所谓库就是一个检测人脸的xml 文件,可以网上查找,下面是一个地址:

https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml

如何构造这个库,学习完本文后可以参考:

http://note.sonots.com/SciSoftware/haartraining.html

https://www.instructables.com/id/Create-OpenCV-Image-Classifiers-Using-Python/

知道构造库,就可以检测各种你想要检测的东西了。

人脸检测不是人脸识别,但是人脸识别的前提。

运行效果如下:

Python下应用opencv 实现人脸检测功能

前提:

这个原始代码来自 https://www.pyimagesearch.com/2016/11/21/raspbian-opencv-pre-configured-and-pre-installed/ 的一个教学讲稿。

你需要下载haarcascade_frontalface_default.xml 以及准备你要检测的文件,我这里是family.jpg,放在python 文件detect_faces.py 所在目录(工作目录)的子目录images下。haarcascade_frontalface_default.xml是放在工作目录。

如果加上摄像头连接程序,也可实时检测,另文介绍。

代码1介绍

导入库,并做命令行参数处理。你在命令行可以输入如下:

python detect_faces.py --image image/family.jpg  --detector haarcascade_frontalface_default.xml

我在程序中都有缺省参数处理,你如果集成测试或命令行不输参数的话,就要修改好你的缺省值。

这样命令行就是python detect_faces.py ,同时也可以输入命令行输入参数。

# USAGE 使用方法是:
# python detect_faces.py --image images/family.jpg \
# --detector haarcascade_frontalface_default.xml
# import the necessary packages 输入包
# import imutils 
import argparse
import cv2
# construct the argument parser and parse the arguments //构造命令行参数分析
# 为了集成测试,或者命令行输入的简单,这里都有缺省参数
#image 是 images/family.jpg
#detector 是 haarcascade_frontalface_default.xml
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default='images/family.jpg',
 help="path to the input image")
ap.add_argument("-d", "--detector", default='haarcascade_frontalface_default.xml',
 help="path to Haar cacscade face detector")
args = vars(ap.parse_args())
 导入图形文件,并灰度处理
# load our image and convert it to grayscale 导入图形文件,并灰度化
image = cv2.imread(args["image"])
#image =imutils.resize(image,width=800)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
导入检测文件,检测图中人脸,显示检测到的人脸数
# load the face detector and detect faces in the image
# 导入脸部检测文件
detector = cv2.CascadeClassifier(args["detector"])
#检测图形中的脸部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#显示检测到的人脸数目
print("[INFO] detected {} faces".format(len(rects)))
 循环,绘图每个检测到的人脸框,并图形显示
# load the face detector and detect faces in the image
# 导入脸部检测
detector = cv2.CascadeClassifier(args["detector"])
#检测图形中的脸部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#显示检测到的人脸数目
print("[INFO] detected {} faces".format(len(rects)))

最后串接所有代码如下:

# USAGE 使用方法是:
# python detect_faces.py --image images/family.jpg \
# --detector haarcascade_frontalface_default.xml
# import the necessary packages 输入包
# import imutils 如果需要成比例缩放图形才需要,这里不需要
import argparse
import cv2
# construct the argument parser and parse the arguments //构造命令行参数分析
# 为了集成测试,或者命令行输入的简单,这里都有缺省参数
#image 是 images/family.jpg
#detector 是 haarcascade_frontalface_default.xml
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default='images/family.jpg',
 help="path to the input image")
ap.add_argument("-d", "--detector", default='haarcascade_frontalface_default.xml',
 help="path to Haar cacscade face detector")
args = vars(ap.parse_args())
# load our image and convert it to grayscale 导入图形文件,并灰度化
image = cv2.imread(args["image"])
#image =imutils.resize(image,width=800)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# load the face detector and detect faces in the image
# 导入脸部检测文件
detector = cv2.CascadeClassifier(args["detector"])
#检测图形中的脸部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#显示检测到的人脸数目
print("[INFO] detected {} faces".format(len(rects)))
# loop over the bounding boxes and draw a rectangle around each face
# 循环rects,绘图每个检测到的人脸框
for (x, y, w, h) in rects:
 cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show the detected faces
cv2.imshow("Faces", image)
cv2.waitKey(0)

总结

以上所述是小编给大家介绍的Python下应用opencv 实现人脸检测功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
Python转换HTML到Text纯文本的方法
Jan 15 Python
简单讲解Python中的数字类型及基本的数学计算
Mar 11 Python
python中星号变量的几种特殊用法
Sep 07 Python
Python实现Pig Latin小游戏实例代码
Feb 02 Python
python绘制评估优化算法性能的测试函数
Jun 25 Python
Apache,wsgi,django 程序部署配置方法详解
Jul 01 Python
Django admin model 汉化显示文字的实现方法
Aug 12 Python
python禁用键鼠与提权代码实例
Aug 16 Python
使用bandit对目标python代码进行安全函数扫描的案例分析
Jan 27 Python
Python绘制分类图的方法
Apr 20 Python
python基础学习之递归函数知识总结
May 26 Python
教你用Python爬取英雄联盟皮肤原画
Jun 13 Python
Python迭代器iterator生成器generator使用解析
Oct 24 #Python
Python 取numpy数组的某几行某几列方法
Oct 24 #Python
Django和Flask框架优缺点对比
Oct 24 #Python
python命令 -u参数用法解析
Oct 24 #Python
使用python制作游戏下载进度条的代码(程序说明见注释)
Oct 24 #Python
用Python解数独的方法示例
Oct 24 #Python
Python3 sys.argv[ ]用法详解
Oct 24 #Python
You might like
YII2框架中excel表格导出的方法详解
2017/07/21 PHP
PHP连接sftp并下载文件的方法教程
2018/08/26 PHP
Web开发之JavaScript
2012/03/29 Javascript
javascript面向对象特性代码实例
2014/06/12 Javascript
分享一个自己写的简单的javascript分页组件
2015/02/15 Javascript
javascript中一些util方法汇总
2015/06/10 Javascript
JS仿淘宝实现的简单滑动门效果代码
2015/10/14 Javascript
JavaScript Math.round() 方法
2015/12/18 Javascript
老生常谈遮罩层 滚动条的问题
2016/04/29 Javascript
AngularJS基础 ng-keypress 指令简单示例
2016/08/02 Javascript
javascript的函数劫持浅析
2016/09/26 Javascript
AngularJS中指令的四种基本形式实例分析
2016/11/22 Javascript
JavaScript实现拖拽元素对齐到网格(每次移动固定距离)
2016/11/30 Javascript
NodeJS收发GET和POST请求的示例代码
2017/08/25 NodeJs
提升页面加载速度的插件InstantClick
2017/09/12 Javascript
JQ图片文件上传之前预览功能的简单实例(分享)
2017/11/12 Javascript
关于react中组件通信的几种方式详解
2017/12/10 Javascript
微信小程序实现的点击按钮 弹出底部上拉菜单功能示例
2018/12/20 Javascript
详解VUE里子组件如何获取父组件动态变化的值
2018/12/26 Javascript
使用Vue.js 和Chart.js制作绚丽多彩的图表
2019/06/15 Javascript
微信小程序转发事件实现解析
2019/10/22 Javascript
深入浅析golang zap 日志库使用(含文件切割、分级别存储和全局使用等)
2020/02/19 Javascript
vue使用自定义事件的表单输入组件用法详解【日期组件与货币组件】
2020/06/01 Javascript
从零学python系列之教你如何根据图片生成字符画
2014/05/23 Python
python扫描proxy并获取可用代理ip的实例
2017/08/07 Python
简单了解python的内存管理机制
2019/07/08 Python
python使用opencv resize图像不进行插值的操作
2020/07/05 Python
实例讲解CSS3中Transform的perspective属性的用法
2016/04/22 HTML / CSS
MAC Cosmetics巴西官方网站:M·A·C彩妆
2019/04/18 全球购物
英国绿色商店:Natural Collection
2019/05/03 全球购物
运动会闭幕词
2015/01/28 职场文书
英语辞职信怎么写
2015/02/28 职场文书
2015年初三班主任工作总结
2015/05/21 职场文书
团组织推荐意见
2015/06/05 职场文书
幼儿园春季开学通知
2015/07/16 职场文书
Redis字典实现、Hash键冲突及渐进式rehash详解
2021/09/04 Redis