Python下应用opencv 实现人脸检测功能
作者:leon_zeng0 发布时间:2022-09-18 00:25:44
使用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 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 实现人脸检测功能,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/leon_zeng0/article/details/102691739
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 01、正则表达式学习正则表达式操作字符串,re模块是用C语言写的没匹配速度非常快,其中compile函数根据一个模式字符串和可选的标志参数生
- 在做项目时发现,很多场合都可能用到Input但又想让它具有select的特性,研究了一下,似乎可以实现,下面的代码可以大概说明我的意图,但实
- 需求描述:平台中需要编写接口供第三方调用,需要控制调用频率,需求为5s内调用一次后不得再次调用。解决思路1.Django官方插件库中有个dj
- 一、property() 函数讲解了解 @property 装饰器之前,我们首先要了解内置函数的 property()。class prop
- 本文实例讲述了Sanic框架请求与响应。分享给大家供大家参考,具体如下:前面介绍了Sanic框架的路由,这里接着介绍Sanic框架的请求与响
- #!/usr/bin/python#coding:utf-8#write:JACK#info:ftp exampleimport ftpli
- 1. 线程的概念:线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元。一个标准的线程由线程
- 一直以来,jQuery是我最喜欢的框架之一。1月14号 1.4 Released版本发布后,就迫不及待去了解了一下1.4版本对比以往1.3.
- 介绍方法之前,我们先说说Python的解释器,由于Python是动态编译的语言,和C/C++、Java或者Kotlin等静态语言不同,它是在
- 在XHTML标签中有一些标签的作用是相似的,当然这里的相似是指语义相似,以至于很多人都不清楚这些相似的标签如何使用,那么今天的主题就是分解相
- 大量的多行段落本身就会降低可读性,同时空行分段也比空格分段有更高的可适应性...前文讨论的热烈程度远超我预期,正好还有之前查阅资料拍的几张实
- 比如有一个a = {‘a': 1}希望变为a = {‘b' :1}即:在保留value不变的情况下,替换key值目前能想到的
- 如何获取实时股票信息股票信息的接口有很多,之前大家常用的是新浪的,但在年初的时候,新浪的接口突然不能使用,给大家造成了很大的困扰,为此网上也
- HTML 5 和 XHTML 2规范草稿公布以来,一直存在很大的争议。HTML 5是由包括Google、Mirosoft、Mozilla、O
- 本文实例讲述了python双向链表原理与实现方法。分享给大家供大家参考,具体如下:双向链表一种更复杂的链表是“双向链表”或“双面链表”。每个
- python的多进程性能要明显优于多线程,因为cpython的GIL对性能做了约束。Python是运行在解释器中的语言,查找资料知道,pyt
- 主题介绍pyecharts里面有很多的主题可以供我们选择,我们可以根据自己的需要完成主题的配置,这样就告别了软件的限制,可以随意的发挥自己的
- import osimport sysimport ftplibimport socket#########################
- 有些时候我们需要得到刚刚插入数据库中的条目id,用于相关系统的更改,在以前我是使用select+top 1+desc 这种笨方法实现的,在没
- 前言python下的setuptools带有一个easy_install的工具,在安装python的每三方模块、工具时很有用,也很方便。安装