侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   408

使用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/

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

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

运行效果如下:

前提:

这个原始代码来自 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 argparseimport cv2# construct the argument parser and parse the arguments //构造命令行参数分析# 为了集成测试,或者命令行输入的简单,这里都有缺省参数#image 是 images/family.jpg#detector 是 haarcascade_frontalface_default.xmlap = 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 argparseimport cv2# construct the argument parser and parse the arguments //构造命令行参数分析# 为了集成测试,或者命令行输入的简单,这里都有缺省参数#image 是 images/family.jpg#detector 是 haarcascade_frontalface_default.xmlap = 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 facescv2.imshow("Faces", image)cv2.waitKey(0)

总结

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


  • 上一条:
    Python装饰器使用你可能不知道的几种姿势
    下一条:
    Python迭代器iterator生成器generator使用解析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客