This tutorial code’s is shown lines below. You can also download it from here . The second version (using LBP for face detection) can be found here

 #include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp" #include <iostream>
#include <stdio.h> using namespace std;
using namespace cv; /** Function Headers */
void detectAndDisplay( Mat frame ); /** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(); /** @function main */
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame; //-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -; }; //-- 2. Read the video stream
capture = cvCaptureFromCAM( - );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture ); //-- 3. Apply the classifier to the frame
if( !frame.empty() )
{ detectAndDisplay( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; } int c = waitKey();
if( (char)c == 'c' ) { break; }
}
}
return ;
} /** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray; cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray ); //-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, , |CV_HAAR_SCALE_IMAGE, Size(, ) ); for( size_t i = ; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), , , , Scalar( , , ), , , ); Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes; //-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, , |CV_HAAR_SCALE_IMAGE, Size(, ) ); for( size_t j = ; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( , , ), , , );
}
}
//-- Show what you got
imshow( window_name, frame );
}

Explanation

Result

  1. Here is the result of running the code above and using as input the video stream of a build-in webcam:

    Remember to copy the files haarcascade_frontalface_alt.xml and haarcascade_eye_tree_eyeglasses.xml in your current directory. They are located in opencv/data/haarcascades

  2. This is the result of using the file lbpcascade_frontalface.xml (LBP trained) for the face detection. For the eyes we keep using the file used in the tutorial.

  3.  
 

opencv 检测人脸、人眼的更多相关文章

  1. SmileyFace——基于OpenCV的人脸人眼检测、面部识别程序

    项目地址 https://github.com/guoyaohua/SmileyFace 开发环境 Visual Studio 2010 MFC + OpenCV 功能描述 静态图像人脸检测 视频人脸 ...

  2. OpenCV检测人脸实例代码

    下面是使用OpenCV通过在硬盘中读入图像来对其进行Haar人脸检测的代码. //包含头文件 #include <opencv2/core/core.hpp> #include " ...

  3. 用opencv检测人眼并定位瞳孔位置

    最近的研究要用到定位瞳孔的位置,所以上网搜了下相关的代码.总结如下: 1) 定位瞳孔可以直接使用opencv中的自带的分类器(haarcascade_eye_tree_eyeglasses.xml)来 ...

  4. OpenCV&Qt学习之四——OpenCV 实现人脸检测与相关知识整理

    开发配置 OpenCV的例程中已经带有了人脸检测的例程,位置在:OpenCV\samples\facedetect.cpp文件,OpenCV的安装与这个例子的测试可以参考我之前的博文Linux 下编译 ...

  5. Python学习--使用dlib、opencv进行人脸检测标注

    参考自https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/ 在原有基础上有一部分的修改(image ...

  6. OpenCV + Python 人脸检测

    必备知识 Haar-like opencv api 读取图片 灰度转换 画图 显示图像 获取人脸识别训练数据 探测人脸 处理人脸探测的结果 实例 图片素材 人脸检测代码 人脸检测结果 总结 下午的时候 ...

  7. 【转载】opencv实现人脸检测

    全文转载自CSDN的博客(不知道怎么将CSDN的博客转到博客园,应该没这功能吧,所以直接复制全文了),转载地址如下 http://blog.csdn.net/lsq2902101015/article ...

  8. Java+opencv实现人脸检测

    版本 Java1.8 opencv3.4 代码: import java.awt.Graphics; import java.awt.image.BufferedImage; import javax ...

  9. OpenCV学习系列(一) Mac下OpenCV + xcode人脸检测实现

    # OpenCV学习系列(一) Mac下OpenCV + xcode人脸检测实现 [-= 博客目录 =-] 1-学习目标 1.1-本章介绍 1.2-实践内容 1.3-相关说明 2-学习过程 2.1-环 ...

随机推荐

  1. 元类编程--__get__ __set__属性描述符

    from datetime import date, datetime import numbers class IntField: #数据描述符,实现以下任意一个,都会变为属性描述符 def __g ...

  2. SASS 和 LESS 的区别

    1.编译环境不同 SASS 的安装需要 Ruby 环境,是在服务端处理的: LESS 需要引入 less.js 来处理代码输出 CSS 到浏览器,也可以在开发环节使用 LESS,然后编译成 CSS 文 ...

  3. k8s的包管理

    1.Helm的概念和架构 每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Helm 则是 Kubernetes 上的 ...

  4. redis五中数据类型

    MySql+Memcached架构的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不断增加 ...

  5. vulkan load store and memoryless

    https://www.jendrikillner.com/article_database/ https://community.arm.com/developer/tools-software/g ...

  6. 多继承以及MRO顺序

    class A: def test(self): print("A --- test方法") def demo(self): print("A --- demo方法&qu ...

  7. Fiddler抓包工具(捕获Android数据包)

    一:获取Android的数据包必须要在同一个网络中 移动设备访问网络原理 先看看移动设备是怎么去访问网络,如图所示,可以看到,移动端的数据包是从wifi出去的. 可以看得出,移动端的数据包,都是要走w ...

  8. 微信小程序之小技能篇(一)

    1,三目运算改变class值: <view class="{{flag ? 'change' : 'change_after'}}">改变字体颜色</view&g ...

  9. native关键字

    1.native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中. 可以将native方法比作Java程序同C程序的接口

  10. Mac 升级 Python2.7 到 Python3.5

    1.去 Python 官网下载一个版本的包 https://www.python.org/downloads/mac-osx/ 2.安装之后,去  /Library/Frameworks/Python ...