我是靠谱客的博主 稳重花卷,这篇文章主要介绍opencv人脸识别 C++(附视频链接),现在分享给大家,希望可以做个参考。

之前想做一个好玩的人脸识别。就学习opencv这个开源工具,下面的是具体的代码实现。

贴一个哔哩哔哩演示的视频opencv人脸识别演示


faceidentify.hpp

复制代码
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
// // faceidentify.hpp // abilitytpratice // // Created by Mr_zhou on 24.5.21. // #ifndef faceidentify_hpp #define faceidentify_hpp #include <stdio.h> #include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/face.hpp> #include <fstream> #include <string> #include <vector> #include <algorithm> using namespace cv; using namespace cv::face; using namespace std; class faceidentify { public: void saveimage(); public: void identify(); private: // 级联器 cv::CascadeClassifier faceCascade; cv::CascadeClassifier eyeCascade; string facexml = "/opt/homebrew/Cellar/opencv/4.5.1_3/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml"; string eyexml = "/opt/homebrew/Cellar/opencv/4.5.1_3/share/opencv4/haarcascades/haarcascade_eye_tree_eyeglasses.xml"; string savefacexml = "/opt/homebrew/Cellar/opencv/4.5.1_3/share/opencv4/haarcascades/haarcascade_frontalface_alt_tree.xml"; }; #endif /* faceidentify_hpp */

faceidentify.cpp

复制代码
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// // faceidentify.cpp // abilitytpratice // // Created by Mr_zhou on 24.5.21. // #include "faceidentify.hpp" void faceidentify::saveimage() { // open camera VideoCapture capture(0); if(!capture.isOpened()) { cout << "camera open failed" << endl; return; } cout << "camera open success!" << endl; // 加载检测人脸的xml文件 if(!faceCascade.load(savefacexml)) { cout << "savefacexml load error!" << endl; return; } cout << "savefacexml load success!" << endl; Mat frame,img; //显示实际大小 vector<Rect> faces; int count = 0; while(capture.read(frame)) { // 防止左右颠倒 flip(frame, frame, 1); // 检测人脸 faceCascade.detectMultiScale(frame,faces,1.1,3,0,Size(50,50),Size(500,500)); for(int i = 0;i<faces.size();i++) { Mat dst; resize(frame(faces[i]), dst, Size(100,100)); if(count % 2 == 0) { imwrite(format("/Users/mr_zhou/Desktop/tupian/face_%d.JPG",count),frame(faces[i])); } rectangle(frame, faces[i], Scalar(0,0,255), 2,8); }// end for循环 imshow("camera-demo", frame); char c = waitKey(10); if(c == 27) { break; } count++; }// end while // 释放空间 capture.release(); } void faceidentify::identify() { ifstream file; file.open("/Users/mr_zhou/Desktop/picture/myimage.txt",ifstream::in); if(!file.is_open()) { cout << "open file filed!" << endl; } // 读取文件并保存 string line; string path; string classlabel; char separator = ';'; vector<cv::Mat> images; vector<int> labels; Mat tmpimage; while(getline(file,line)) { stringstream liness(line); getline(liness, path, separator); getline(liness,classlabel); // 二者都不为空 if(!path.empty() && !classlabel.empty()) { resize(cv::imread(path,0), tmpimage, Size(294,294)); images.push_back(tmpimage); labels.push_back(atoi(classlabel.c_str())); } } // end while if(images.size() < 1 || labels.size() < 1) { cout << "invalid path" << endl; return ; } int height = images[0].rows; int width = images[0].cols; cout << "height:" << height << "width:" << width << endl; cv::Mat testSample = images[images.size()-1]; int testLable = labels[labels.size()-1]; images.pop_back(); labels.pop_back(); // 使用EigenFace获取特征脸 Ptr<FisherFaceRecognizer> model = FisherFaceRecognizer::create(); model->train(images, labels); // 获取特征脸 int predictLabel = model->predict(testSample); // 返回识别到的 cout << "actual label:" << testLable << "predict label:" << predictLabel << endl; if(!faceCascade.load(savefacexml)) { cout << "load savefacexml filed!" << endl; } // 打开摄像头 VideoCapture cap(0); if(!cap.isOpened()) { cout << "camera open failed!" << endl; return; } cout << "camera open failed!" << endl; Mat frame; vector<Rect> faces; Mat dst; while(cap.read(frame)) { // 颠倒图像 flip(frame,frame,1); // 设置显示视频中的信息尺寸 faceCascade.detectMultiScale(frame, faces,1.1,3,0,Size(80,80),Size(500,500)); for(int i = 0; i < faces.size();i++) { Mat roi = frame(faces[i]); // 设置灰度图像,防止颜色不一致 cvtColor(roi, dst, COLOR_BGR2GRAY); // 设置大小,与窗口一致 resize(dst,testSample,testSample.size()); // 保存识别的结果 int label = model->predict(testSample); // 使用红色的框框起来 rectangle(frame, faces[i], Scalar(0,0,255), 3,8,0); string text; if(label == 17) { text = "wenliang"; } else if(label == 18) { text = "shengju"; } else if(label == 19) { text = "qifei"; } else { text = "no know!"; } putText(frame, text, Point(faces[i].x,faces[i].y-10),FONT_HERSHEY_PLAIN, 2.0,Scalar(255,0,0),3); } imshow("face-recognition", frame); if(waitKey(50) == 27) { break; } } // end while }

main.cpp

复制代码
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
// // main.cpp // abilitytpratice // // Created by Mr_zhou on 24.5.21. // #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; #include "faceidentify.hpp" // 主函数 int main(int argc, const char * argv[]) { // insert code here... faceidentify fi; // 保存人脸图像 fi.saveimage(); // 人脸识别 fi.identify(); waitKey(0); std::cout << "Hello, World!n"; return 0; }

1.按照自己安装opencv的路径填写face_xml

2.保存图片的路径按照自己保存的文件名

最后

以上就是稳重花卷最近收集整理的关于opencv人脸识别 C++(附视频链接)的全部内容,更多相关opencv人脸识别内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部