-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
77 lines (64 loc) · 2.57 KB
/
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
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
#include <iostream>
#include "FaceRecognition.h"
using namespace cv;
int main(int argc, char *argv[])
{
float recogn_thresh=0.2;
const int rfactor = 5;
Size img_shape={160, 120}, display_shape=img_shape*rfactor, features_model_shpae={112, 112};
String detection_model = "./models/yunet_120x160.onnx";
String recognize_model = "./models/mobilefacenet.onnx";
String data_path = "./data/";
Detector detector(detection_model, img_shape);
FeatureExtractor extractor(recognize_model, features_model_shpae);
FeatureDatabase<CosSimilarity> database(data_path, detector,extractor,
display_shape, rfactor, recogn_thresh);
cv::VideoCapture cap;
cv::Mat im;
cv::TickMeter cvtm;
std::vector<cv::String> output_names = {"loc", "conf", "iou"};
std::vector<cv::Mat> faces, features;
String title = "FaceRecognition@eipi10";
if (isdigit(argv[1][0])) {
cap.open(argv[1][0] - '0');
if (!cap.isOpened()) {
std::cerr << "Cannot open the camera." << std::endl;
return 0;
}
}
if (cap.isOpened()) {
while (true) {
cap >> im;
cvtm.start();
if (features_from_img(im, detector, extractor, features,
display_shape, rfactor)) {
// std::cout << detector.size() << " faces found.\n";
int i=0;
float max_similar;
std::string max_name;
for (const auto& feature : features) {
if (database.select_max_similarity(feature, max_similar,
max_name)) {
} else {
max_name = std::string("Unknown");
}
cv::putText(im, max_name, detector.faces()[i].bbox.top_left*rfactor,
cv::FONT_HERSHEY_SIMPLEX, 0.5,
cv::Scalar(0, 255, 0), 1);
++i;
}
} else {
std::cout << "No faces found." << std::endl;
}
cvtm.stop();
std::string timeLabel = cv::format("Inference time: %.2f ms", cvtm.getTimeMilli());
cv::putText(im, timeLabel, cv::Point(0, 25), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 1);
draw(im, detector.faces() , rfactor);
cvtm.reset();
cv::imshow(title, im);
if ((cv::waitKey(1) & 0xFF) == 27)
break;
}
}
return 0;
}