「计算机视觉C++」C++实现人脸识别

1.介绍

人脸识别是图像处理非常重要的应用之一。本文这是一个简单的人脸识别程序,运用OpenCV3中自带的模型:(需要模型文件的私信我喔!)

<code>haarcascade_frontalface_alt.xml
haarcascade_eye_tree_eyeglasses.xml/<code>
「计算机视觉C++」C++实现人脸识别

2.几句牢骚话

人脸识别技术的衡量维度太多,但从技术比较,比如图像比对级的1:1,1:N,N:N。衡量的标准和维度都不同。比如算法精确度上,国内国外的人脸识别技术大多数在开源OpenCV等开源库上进行新规则添加(深度学习进行叠层运算),公司之间的识别正确率差异仅仅在小数点上,99.6%-99.7%提升意义不大,如果说就此称王称霸就说达到世界一流,就要被内行笑话了。基本上国内每家公司都会说自己的人脸识别算法牛,真正表达的是缺乏对底层技术的敬畏。

「计算机视觉C++」C++实现人脸识别

给你个表情,你自己体会

3.C++的人脸识别实现

<code>//--------------------------------------【程序说明】-------------------------------------------
//\t\t程序描述:人脸识别
//\t\t测试所用操作系统: Windows 10 64bit
//\t\t测试所用IDE版本:Visual Studio 2017
//\t\t测试所用OpenCV版本:\t3.4
//\t\t2020年3月 Revised by @DL小宝
//------------------------------------------------------------------------------------------------

/**
* @file ObjectDetection.cpp
* @author A. Huaman ( based in the classic facedetect.cpp in samples/c )
* @brief A simplified version of facedetect.cpp, show how to load a cascade classifier and how to find objects (Face + eyes) in a video stream
*/

//---------------------------------【头文件、命名空间包含部分】----------------------------
//\t\t描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#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;

void detectAndDisplay(Mat frame);

//--------------------------------【全局变量声明】----------------------------------------------
//\t\t描述:声明全局变量
//-------------------------------------------------------------------------------------------------
//注意,需要把"haarcascade_frontalface_alt.xml"和"haarcascade_eye_tree_eyeglasses.xml"这两个文件复制到工程路径下

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(12345);

//--------------------------------【help( )函数】----------------------------------------------
//\t\t描述:输出帮助信息
//-------------------------------------------------------------------------------------------------
static void ShowHelpText()
{
\t//输出欢迎信息和OpenCV版本
\tcout << "\\n\\n\\t\\t\\t 当前使用的OpenCV版本为:" << CV_VERSION
\t\t<< "\\n\\n ----------------------------------------------------------------------------";
}

//-----------------------------------【main( )函数】--------------------------------------------
//\t\t描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main(void)
{
\tVideoCapture capture;
\tMat frame;

\t//-- 1. 加载级联(cascades)
\tif (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading\\n"); return -1; };
\tif (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading\\n"); return -1; };

\t//-- 2. 读取视频
\tcapture.open(0);
\tShowHelpText();
\tif (capture.isOpened())
\t{
\t\tfor (;;)
\t\t{
\t\t\tcapture >> frame;

\t\t\t//-- 3. 对当前帧使用分类器(Apply the classifier to the frame)
\t\t\tif (!frame.empty())
\t\t\t{
\t\t\t\tdetectAndDisplay(frame);
\t\t\t}

\t\t\telse
\t\t\t{
\t\t\t\tprintf(" --(!) No captured frame -- Break!"); break;
\t\t\t}

\t\t\tint c = waitKey(10);
\t\t\tif ((char)c == 'c') { break; }
\t\t}
\t}
\treturn 0;
}

void detectAndDisplay(Mat frame)
{
\tstd::vector<rect> faces;
\tMat frame_gray;

\tcvtColor(frame, frame_gray, COLOR_BGR2GRAY);
\tequalizeHist(frame_gray, frame_gray);

\t//-- 人脸检测
\t//此句代码的OpenCV2版为:
//face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
\t//此句代码的OpenCV3版为:
\tface_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

\tfor (size_t i = 0; i < faces.size(); i++)
\t{
\t\tPoint center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
\t\tellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 2, 8, 0);

\t\tMat faceROI = frame_gray(faces[i]);
\t\tstd::vector<rect> eyes;

\t\t//-- 在脸中检测眼睛
\t\t//此句代码的OpenCV2版为:
\t // eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
\t\t//此句代码的OpenCV3版为:
\t\teyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

\t\tfor (size_t j = 0; j < eyes.size(); j++)
\t\t{
\t\t\tPoint eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
\t\t\tint radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
\t\t\tcircle(frame, eye_center, radius, Scalar(255, 0, 0), 3, 8, 0);
\t\t}

\t}
\t//-- 显示最终效果图
\timshow(window_name, frame);
}/<rect>/<rect>/<stdio.h>/<iostream>/<code>


分享到:


相關文章: