使用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 实现人脸检测功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]