-
Notifications
You must be signed in to change notification settings - Fork 58
/
multi-faces.cc
148 lines (122 loc) · 4.68 KB
/
multi-faces.cc
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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020-2021 Intel Corporation. All Rights Reserved.
// Sample of how to correlate detected face rectangle with authenticated user
#include "RealSenseID/FaceAuthenticator.h"
#include "RealSenseID/Preview.h"
#include <iostream>
#include <vector>
RealSenseID::DeviceConfig device_config;
class PreviewRender : public RealSenseID::PreviewImageReadyCallback
{
std::vector<unsigned int> _images_ts;
public:
void OnPreviewImageReady(const RealSenseID::Image image)
{
std::cout << image.metadata.timestamp << std::endl;
_images_ts.push_back(image.metadata.timestamp); // save timestamps for matching to onFaceDetected
// image can be saved to file with timestamp in name
}
const std::vector<unsigned int>& GetImagesTimeStamps()
{
return _images_ts;
}
};
class MyAuthClbk : public RealSenseID::AuthenticationCallback
{
std::vector<RealSenseID::FaceRect> _faces;
size_t _result_idx = 0;
unsigned int _ts =0;
public:
void OnResult(const RealSenseID::AuthenticateStatus status, const char* user_id) override
{
std::cout << "\n******* OnResult #" << _result_idx << "*******" << std::endl;
std::cout << "Status: " << status << std::endl;
if (status == RealSenseID::AuthenticateStatus::Success)
{
if (device_config.algo_flow == RealSenseID::DeviceConfig::AlgoFlow::SpoofOnly)
{
std::cout << " Real face" << std::endl;
}
else
{
std::cout << "UserId: " << user_id << std::endl;
}
}
else if (status == RealSenseID::AuthenticateStatus::Forbidden)
{
std::cout << " User is not authenticated" << std::endl;
}
else if (status == RealSenseID::AuthenticateStatus::Spoof)
{
std::cout << " Spoof" << std::endl;
}
// print the corresponding face coords (that was received in OnFaceDetected)
if (_faces.size() > _result_idx)
{
auto& face = _faces[_result_idx];
std::cout << "Face: " << face.x << "," << face.y << " " << face.w << "x" << face.h << std::endl;
}
_result_idx++;
std::cout << std::endl;
}
void OnHint(const RealSenseID::AuthenticateStatus hint) override
{
std::cout << "OnHint " << hint << std::endl;
}
void OnFaceDetected(const std::vector<RealSenseID::FaceRect>& faces, const unsigned int ts) override
{
_faces = faces;
_ts = ts;
}
unsigned int GetLastTimeStamp()
{
return _ts;
}
};
int main()
{
RealSenseID::FaceAuthenticator authenticator;
RealSenseID::PreviewConfig preview_config;
RealSenseID::Preview preview(preview_config);
#ifdef _WIN32
auto status = authenticator.Connect({"COM25"});
#elif LINUX
auto status = authenticator.Connect({"/dev/ttyACM0"});
#endif
if (status != RealSenseID::Status::Ok)
{
std::cout << "Failed connecting with status " << status << std::endl;
return 1;
}
// uncomment below line to configure FW to run Spoof detection only, by default all algo(s) are running
// device_config.algo_flow = RealSenseID::DeviceConfig::AlgoFlow::SpoofOnly;
// configure FW to authenticate or detect spooofs all detected faces
device_config.face_selection_policy = RealSenseID::DeviceConfig::FaceSelectionPolicy::All;
// apply configuration
status = authenticator.SetDeviceConfig(device_config);
PreviewRender image_clbk;
MyAuthClbk auth_clbk;
preview.StartPreview(image_clbk);
authenticator.Authenticate(auth_clbk);
preview.StopPreview();
// get timestamps saved in callbacks
auto face_detection_ts = auth_clbk.GetLastTimeStamp();
auto images_ts = image_clbk.GetImagesTimeStamps();
if (face_detection_ts != 0)
{
// looking for the preview image that is closest to face detection by timestamp
for (auto it = images_ts.begin(); it != images_ts.end(); ++it)
{
if (*it >= face_detection_ts)
{
auto most_close =
(*it - face_detection_ts < face_detection_ts - *(it -1)) ? it : (it-1); // chose closest
std::cout << "The image matched to OnFaceDetected is the #" << most_close - images_ts.begin()
<< " image accquired." << std::endl;
std::cout << "Face detection timestamp: " << face_detection_ts << " milliseconds." << std::endl;
std::cout << "Image timestamp: " << *most_close << " milliseconds." << std::endl;
break;
}
}
}
}