当前位置: 首页 > 新闻动态 > 软件编程

浅理解C++ 人脸识别系统的实现

作者:用户投稿 浏览: 发布日期:2026-01-12
[导读]:这篇文章主要介绍了浅理解C++ 人脸识别系统的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

机器学习

  • 机器学习的目的是把数据转换成信息。
  • 机器学习通过从数据里提取规则或模式来把数据转成信息。

人脸识别

  • 人脸识别通过级联分类器对特征的分级筛选来确定是否是人脸。
  • 每个节点的正确识别率很高,但正确拒绝率很低。
  • 任一节点判断没有人脸特征则结束运算,宣布不是人脸。
  • 全部节点通过,则宣布是人脸。

工业上,常用人脸识别技术来识别物体。

基于深度学习的人脸识别系统,一共用到5个开源库:OpenCV(计算机视觉库)、Caffe(深度学习库)、Dlib(机器学习库)、libfacedetection(人脸检测库)、cudnn(gpu加速器)。

用到一个开源的深度学习模型:VGG model。

#include "opencv2/core/core.hpp"
#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;

string face_cascade_name = "haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
string window_name = "人脸识别";

void detectAndDisplay( Mat frame );

int main( int argc, char** argv ){
  Mat image;
  image = imread( argv[1]);

  if( argc != 2 || !image.data ){
    printf("[error] 没有图片\n");
    return -1;
  }

  if( !face_cascade.load( face_cascade_name ) ){ 
    printf("[error] 无法加载级联分类器文件!\n");
    return -1; 
  }

  detectAndDisplay(image);

  waitKey(0);  
}

void detectAndDisplay( Mat frame ){
  std::vector<Rect> faces;
  Mat frame_gray;

  cvtColor( frame, frame_gray, CV_BGR2GRAY );
  equalizeHist( frame_gray, frame_gray );

  face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

  for( int i = 0; 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), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
  }

  imshow( window_name, frame );
}

参考文章:https://www.cnblogs.com/justany/archive/2012/11/22/2781552.html

免责声明:转载请注明出处:http://shjed.com/news/751742.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!