目录


  利用基于Haar特征的级联分类器实现人脸检测;官方教程


目标

  • 学习基于Haar特征的级联分类器(Cascade Callifiers)实现人脸检测;
  • 扩展到人眼检测;

基础知识

Paul Viola、Michael Jones: Rapid Object Detection using a Boosted Cascade of Simple Features

  OpenCV中提供了训练和检测两个部分;下面的代码主要是检测部分,也就是说利用OpenCV提供的训练好的模型进行检测;OpenCV提供了不少训练好的分类器模型,如人脸、眼睛、笑容,分类器文件位于GitHub;

  有关训练的部分会涉及到上面那篇发表在2001年的CVPR上的文章;Haar特征,积分图,还有级联分类器等概念;

OpenCV实现人脸检测

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# @Time    : 19-4-21 下午1:08
# @Author  : chen"""
基于Haar特征的级联分类器用于人脸检测
https://docs.opencv.org/4.0.0/d7/d8b/tutorial_py_face_detection.html"""
import cv2 # 下面两个文件下载地址
# https://github.com/opencv/opencv/tree/master/data/haarcascades
print("[INFO] 加载.xml文件")
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')# 读取图片,并转换成灰度图像
print("[INFO] 转换成灰度图像")
img = cv2.imread('face_2.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 在灰度图像下检测人脸
print("[INFO] 人脸检测")
faces = face_cascade.detectMultiScale(gray, 1.3, 5)count = 0
for (x, y, w, h) in faces:print("[INFO] 检测到第{}张人脸图像".format(count))count += 1# 画矩形圈出人脸cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)# 获取人脸灰度图像和彩色图像roi_gray = gray[y:y+h, x:x+w]roi_color = img[y:y+h, x:x+w]# 在人脸灰度图像上检测眼睛位置eyes = eye_cascade.detectMultiScale(roi_gray)for (ex, ey, ew, eh) in eyes:cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

从视频流中检测人脸

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# @Time    : 19-4-21 下午1:56
# @Author  : chen"""
从视频流中检测人脸位置,眼睛位置
"""
# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import time
import cv2# 加载.xml文件
print("[INFO] 加载.xml文件")
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')# 初始化视频流,唤醒摄像头
print("[INFO] 开启摄像头")
vs = VideoStream(src=0).start()
time.sleep(2.0)# start the FPS throughput estimator
fps = FPS().start()# loop over frames from the video file stream
while True:# 捕获视频帧frame = vs.read()# 读取图片,并转换成灰度图像# img = cv2.imread(img)gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# 在灰度图像下检测人脸faces = face_cascade.detectMultiScale(gray, 1.3, 5)for (x, y, w, h) in faces:# 画矩形圈出人脸cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)# 获取人脸灰度图像和彩色图像roi_gray = gray[y:y + h, x:x + w]roi_color = frame[y:y + h, x:x + w]# 在人脸灰度图像上检测眼睛位置eyes = eye_cascade.detectMultiScale(roi_gray)for (ex, ey, ew, eh) in eyes:cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)# update the FPS counterfps.update()# show the output framecv2.imshow("Frame", frame)key = cv2.waitKey(1) & 0xFF# if the `q` key was pressed, break from the loopif key == ord("q"):break# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

转载于:https://www.cnblogs.com/chenzhen0530/p/10746731.html

Object Detection: Face Detection using Haar Cascades相关推荐

  1. Revisiting Salient Object Detection: Simultaneous Detection, Ranking, and Subitizing of Multiple Sal

    问题: 作者认为,显著性目标检测领域迄今为止的工作解决的是一个相当病态的问题.即不同的人对于什么是显著性目标没有一个普遍的一致意见.这意味着一些目标会比另一些目标更加显著,并且不同的显著性目标中存在着 ...

  2. 《Revisiting Salient Object Detection! Simultaneous Detection, Ranking, and Subitizing of Multiple Sa

    <Revisiting Salient Object Detection! Simultaneous Detection, Ranking, and Subitizing of Multiple ...

  3. 【opencv】Face Detection using Haar Cascades

    转自:http://docs.opencv.org/trunk/d7/d8b/tutorial_py_face_detection.html Goal In this session, We will ...

  4. Face Detection using Haar Cascades

    本翻译有点拗口,有很多地方翻译不对. haar 级联分类器 是一个有效的目标检测方法,2001年Paul Viola 和 Michael Jones 提出.也是基于机器学习的方法,由大量正反图片训练的 ...

  5. opencv-python 采用Haar Cascades进行人脸检测

    目标 使用基于Haar特征的级联分类器进行人脸检测 把它扩展到眼睛检测等领域 基础 基于Haar特征的级联分类器进行目标检测是Paul Viola和Michael Jones在2001年发表的论文&l ...

  6. 异常检测(Out-of-distribution detection\ anomaly detection)相关论文阅读

    Learning Confidence for Out-of-Distribution detection in Neural Network 作者 Terrance DeVries .Graham ...

  7. OpenCV利用Haar Cascades进行人脸检测

    开发十年,就只剩下这套Java开发体系了 >>>    目标 在这届会议上, 我们将使用基于Haar特征的级联分类器来了解人脸检测的基础知识 我们将延长相同的眼睛检测等. 基础 使用 ...

  8. 机器学习工程师 — Udacity 基于CNN和迁移学习创建狗品种分类器

    卷积神经网络(Convolutional Neural Network, CNN) 项目:实现一个狗品种识别算法App 推荐你阅读以下材料来加深对 CNN和Transfer Learning的理解: ...

  9. object detection

    原地址:https://handong1587.github.io/deep_learning/2015/10/09/object-detection.html Object Detection Pu ...

最新文章

  1. 今晚直播 | 深入浅出理解A3C强化学习
  2. python list循环语句的使用方法
  3. C++的精髓——虚函数
  4. jQuery表单验证的几种方法
  5. 企业实战_09_MyCat 搭建Mysql 一主三从复制环境
  6. win10 安装docker流程_Windows10下安装Docker的步骤图文教程
  7. 华为无盘服务器,无盘服务器操作系统
  8. 那些很重要,但是不常用的技术,websocket
  9. 史上最全计算机毕业设计题目4(10万套,大部分全国唯一)
  10. 鸿蒙系统全屋定制,华为推出鸿蒙 1+2+N 全屋智能、智慧屏 V 系列,还有一款陪伴机器人小艺精灵...
  11. 研华工控台式计算机选型,工控机选型手册.pdf
  12. 作为iOS开发者,你不可错过的资源
  13. win10——microsoft同步用户主题桌面背景的本地位置、默认背景位置、双屏双背景图设置
  14. Rust 错误处理(二)
  15. 使用easypoi导出注解添加序号,无须重写ExcelExportUtil类
  16. 交流电机是如何工作的
  17. How to use ros_control on your robot-ros_control_usage-ros_control-ros
  18. 区块链——软分叉和硬分叉
  19. ImageJ+OriginLab简单实现区域信号强度统计
  20. 信息学 计算机奥林匹克竞赛辅导,信息学奥林匹克竞赛辅导心得(转载)

热门文章

  1. mysql自动提交的概念_MySQL入门之事务概念
  2. day05 Python 元组
  3. 扫雷游戏(NOIP2015 普及组第二题)
  4. 试解释如下两个概念:CLR和CTS
  5. [NOIP2002]矩形覆盖
  6. Android风格与主题
  7. vue导出Excel(二)
  8. 基于SpringMVC+EasyPoi,采用Excel模板方式实现Excel在线预览和导出(2021版)
  9. WebService之初体验
  10. python des加密文件_Python DES加密解密方法 pyDes库 兼容中文