人脸识别代码(基于OpenCV)

官方源代码

Posted by HD on February 19, 2018

`

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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);

  //——————————–【全局变量声明】———————————————-
// 描述:声明全局变量
//————————————————————————————————-
//注意,需要把”haarcascade_frontalface_alt.xml”和”haarcascade_eye_tree_eyeglasses.xml”这两个文件复制到工程路径下
String face_cascade_name = “C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml”;
String eyes_cascade_name = “C:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml”;
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = “Capture – Face detection”;
RNG rng(12345);

//——————————–【help( )函数】———————————————-
// 描述:输出帮助信息
//————————————————————————————————-

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

//– 1. 加载级联(cascades)
if (!face_cascade.load(face_cascade_name)) { printf(“–(!)Error loading\n”); return -1; };
if (!eyes_cascade.load(eyes_cascade_name)) { printf(“–(!)Error loading\n”); return -1; };

//– 2. 读取视频
capture.open(1);
if (capture.isOpened())
{
for (;;)
{
capture >> frame;

//– 3. 对当前帧使用分类器(Apply the classifier to the frame)
if (!frame.empty())
{
detectAndDisplay(frame);
}
else
{
printf(” –(!) No captured frame — Break!”); break;
}

int c = waitKey(10);
if ((char)c == ‘c’) { break; }

}
}
return 0;
}

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

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

//– 人脸检测
//此句代码的OpenCV2版为:
//face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|C

`


☛小礼物走一走,来Github关注我☚