利用基于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 counter
fps.update() # show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop
if 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()

Object Detection: Face Detection using Haar Cascades的更多相关文章

  1. 【计算机视觉】Opencv中的Face Detection using Haar Cascades

    [计算机视觉]Opencv中的Face Detection using Haar Cascades 标签(空格分隔): [图像处理] 声明:引用请注明出处http://blog.csdn.net/lg ...

  2. [Object Tracking] Contour Detection through OpenCV

    利用OpenCV检测图像中的长方形画布或纸张并提取图像内容 - 阅读笔记 相对来说,如下链接是此文的高阶方案版本,做对比是极好的. [Object Tracking] Contour Detectio ...

  3. [Object Tracking] Contour Detection through Tensorflow running on smartphone

    From: 手机端运行卷积神经网络的一次实践 -- 基于 TensorFlow 和 OpenCV 实现文档检测功能 貌似不错的东西:移动端视觉识别模型:MobileNets Holistically- ...

  4. 通过haar Cascades检测器来实现面部检测

    在OpenCV中已经封装的很好只需要使用cv::CascadeClassifier类就可以很容易的实现面部的检测, 三大步: 1.训练好的特征分类器配置文件haarcascade_frontalfac ...

  5. Object Detection with 10 lines of code - Image AI

    To perform object detection using ImageAI, all you need to do is Install Python on your computer sys ...

  6. (转)Awesome Object Detection

    Awesome Object Detection 2018-08-10 09:30:40 This blog is copied from: https://github.com/amusi/awes ...

  7. YOLO object detection with OpenCV

    Click here to download the source code to this post. In this tutorial, you’ll learn how to use the Y ...

  8. 行人检测(Pedestrian Detection)资源整合

    一.纸 评论文章分类: [1] D. Geronimo, and A. M.Lopez. Vision-based Pedestrian Protection Systems for Intellig ...

  9. Clash Detection

    Clash Detection eryar@163.com Abstract. Clash detection is used for the model collision check. The p ...

随机推荐

  1. 正值表达式匹配html标签的属性值

    今天由于工作的需求,需要获取html标签的属性值,立即想到了正则表达式,标签如下: <circle id="ap_test" cx="200" cy=&q ...

  2. vijos1264:神秘的咒语

    描述 身为拜月教的高级间谍,你的任务总是逼迫你出生入死.比如这一次,拜月教主就派你跟踪赵灵儿一行,潜入试炼窟底. 据说试炼窟底藏着五行法术的最高法术:风神,雷神,雪妖,火神,山神的咒语.为了习得这些法 ...

  3. MySQL/InnoDB tips & tricks

    本文讨论的是 MySQL 5.7. 1.混用 * 和列名会产生解析错误,要避免这个错误,要使用 tbl_name.* 的格式. 2.where 子句中不能使用 select 中定义的别名,因为 SQL ...

  4. extern关键字祥解

    1 基本解释:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.此外extern也可用来进行链接指定. 也就是说extern ...

  5. java代码I/O类

    总结:流类无法理解啊—————— package com.aini; import java.io.*; //流类 //使用FileInputStream读取文件信息 public class ffg ...

  6. RCE、exp、Exploit、Exploit Pack、exp-gui、Payload、MetaSploit都是啥

    对于走在安全路上的小菜来说,这几个exp.Exploit.Exploit Pack.exp-gui.Payload.MetaSploit名词着实把人转的不轻,下面给大家解释下: RCE,remote ...

  7. 转:三思!大规模MySQL运维陷阱之基于MyCat的伪分布式架构

    在微信公众号看到一篇关于mycat的文章,觉得分析的很不错,给大家分享一下 三思!大规模MySQL运维陷阱之基于MyCat的伪分布式架构 原文链接:https://mp.weixin.qq.com/s ...

  8. 关于java中getClass()和getSuperClass()的讲解

    为了讲解这个问题,我们先来看一下下面的代码: package com.yonyou.test; import java.util.Date; class Test extends Date{ priv ...

  9. UE4材质初探

    转自:http://www.unrealchina.net/portal.php?mod=view&aid=233 UE4的材质表面上看起来很简单,可是到了用的时候却总是没有办法实现好的效果. ...

  10. FreeType 管理字形

    转自:http://blog.csdn.net/hgl868/article/details/7254687 1.字形度量 顾名思义,字形度量是对应每一个字形的特定距离,以此描述如何对文本排版.    ...