Enrollment 28.2.0
Loading...
Searching...
No Matches
ICSVerifyMatcher.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "enrollment/Export.h"
19#include <array>
20#include <memory>
21#include <mutex>
22#include <vector>
23
24namespace inno
25{
26 class Logger;
31 */
32 class ICSVerifyMatcher : public ICSMatcher
33 {
34 public:
38 */
39 class Artifact
40 {
41 private:
49 virtual void ConfigureMatchingParameters(const ICSTemplate& probe,
50 const ICSTemplate& gallery,
51 inno_ICSVerifyMatcher_params& params) = 0;
55 virtual void OnCreated() = 0;
61 virtual void CheckCompatibility(inno_ICSVerifyMatcher_params& params) = 0;
62
63 public:
64 Artifact() = default;
65 Artifact(const Artifact&) = delete;
66 Artifact(Artifact&&) = delete;
67 Artifact& operator=(const Artifact&) = delete;
68 Artifact& operator=(Artifact&& c) = delete;
69 virtual ~Artifact() = default;
70
71 friend class ICSVerifyMatcher;
72 };
73
76 */
77 class Artifacts : public Artifact
78 {
79 public:
87 */
88 void Add(Artifact& artifact)
89 {
90 const std::lock_guard lock(artifactsMux);
91
92 if (isMatchingInProgress)
93 {
94 throw EnrollmentRuntimeException("cannot add artifact during matching");
95 }
96
97 inno_ICSVerifyMatcher_params param = {};
98
99 for (auto* a : artifacts)
100 {
101 a->CheckCompatibility(param);
102 }
103
104 // when not compatible then it will throw
105 artifact.CheckCompatibility(param);
106
107 artifacts.emplace_back(&artifact);
108 }
109
110 private:
111 void ConfigureMatchingParameters(const ICSTemplate& probe,
112 const ICSTemplate& gallery,
113 inno_ICSVerifyMatcher_params& params) final
114 {
115 const std::lock_guard lock(artifactsMux);
116
117 for (auto* a : artifacts)
118 {
119 a->ConfigureMatchingParameters(probe, gallery, params);
120 }
121
122 isMatchingInProgress = true;
123 }
124
125 void OnCreated() final
126 {
127 const std::lock_guard lock(artifactsMux);
128
129 for (auto* a : artifacts)
130 {
131 a->OnCreated();
132 }
133
134 isMatchingInProgress = false;
135 }
136
137 void CheckCompatibility(inno_ICSVerifyMatcher_params& params) final
138 {
139 const std::lock_guard lock(artifactsMux);
140
141 for (auto* a : artifacts)
142 {
143 a->CheckCompatibility(params);
144 }
145 }
146
147 std::vector<Artifact*> artifacts;
148 std::mutex artifactsMux;
149 bool isMatchingInProgress = false;
150
151 public:
152 Artifacts() = default;
153 Artifacts(const Artifacts&) = delete;
154 Artifacts(Artifacts&&) = delete;
155 Artifacts& operator=(const Artifacts&) = delete;
156 Artifacts& operator=(Artifacts&& c) = delete;
157 virtual ~Artifacts() = default;
158 };
159
164 */
165 class MatchingMinutiae : public Artifact
166 {
167 public:
172 */
173 std::vector<std::shared_ptr<Minutia>> GetProbeMinutiae() const
174 {
175 const std::scoped_lock lock(dataMux);
176
177 if (isMatchingInProgress)
178 {
179 throw EnrollmentRuntimeException("matching in progress, cannot get probe minutiae");
180 }
181
182 return probe;
183 }
188 */
189 std::vector<std::shared_ptr<Minutia>> GetGalleryMinutiae() const
190 {
191 const std::scoped_lock lock(dataMux);
192
193 if (isMatchingInProgress)
194 {
195 throw EnrollmentRuntimeException("matching in progress, cannot get gallery minutiae");
196 }
197
198 return gallery;
199 }
200
201 MatchingMinutiae() = default;
202 MatchingMinutiae(const MatchingMinutiae&) = delete;
204 MatchingMinutiae& operator=(const MatchingMinutiae&) = delete;
205 MatchingMinutiae& operator=(MatchingMinutiae&& c) = delete;
206 virtual ~MatchingMinutiae() = default;
207
208 private:
209 std::vector<std::shared_ptr<Minutia>> probe;
210 std::vector<uint16_t> matchingProbeIndices;
211 std::vector<std::shared_ptr<Minutia>> gallery;
212 std::vector<uint16_t> matchingGalleryIndices;
213 int maxMatchingMinutiae = 0;
214 mutable std::mutex dataMux;
215 bool isMatchingInProgress = false;
216
217 void ConfigureMatchingParameters(const ICSTemplate& probeTemplate,
218 const ICSTemplate& galleryTemplate,
219 inno_ICSVerifyMatcher_params& params) final
220 {
221 const std::scoped_lock lock(dataMux);
222
223 if (isMatchingInProgress)
224 {
225 throw EnrollmentRuntimeException("matching minutiae already used in matching");
226 }
227
228 isMatchingInProgress = true;
229
230 probe = probeTemplate.GetMinutiae();
231 gallery = galleryTemplate.GetMinutiae();
232 // It is not possible to have more matching minutiae than number of minutiae in probe or gallery
233 maxMatchingMinutiae = static_cast<int>(std::min(probe.size(), gallery.size()));
234 matchingProbeIndices.resize(maxMatchingMinutiae);
235 matchingGalleryIndices.resize(maxMatchingMinutiae);
236
237 params.assocGalleryMinutiae = matchingGalleryIndices.data();
238 params.assocProbeMinutiae = matchingProbeIndices.data();
239 params.associationCount = &maxMatchingMinutiae;
240 }
241
242 void OnCreated() final
243 {
244 const std::scoped_lock lock(dataMux);
245
246 // reorder according to matching indices
247 std::vector<std::shared_ptr<Minutia>> reorderedMinutiae;
248 // maxMatchingMinutiae is modified by IEngine during matching and contains actual (not max) number of
249 // matching minutiae
250 reorderedMinutiae.reserve(maxMatchingMinutiae);
251 for (int i = 0; i < maxMatchingMinutiae; ++i)
252 {
253 reorderedMinutiae.emplace_back(probe[matchingProbeIndices[i]]);
254 }
255 std::swap(probe, reorderedMinutiae);
256
257 reorderedMinutiae.clear();
258 reorderedMinutiae.reserve(maxMatchingMinutiae);
259 for (int i = 0; i < maxMatchingMinutiae; ++i)
260 {
261 reorderedMinutiae.emplace_back(gallery[matchingGalleryIndices[i]]);
262 }
263 std::swap(gallery, reorderedMinutiae);
264
265 isMatchingInProgress = false;
266 }
267
268 void CheckCompatibility(inno_ICSVerifyMatcher_params& params) final
269 {
270 if (params.assocGalleryMinutiae != nullptr || params.associationCount != nullptr ||
271 params.assocProbeMinutiae != nullptr)
272 {
273 throw EnrollmentRuntimeException("matching minutiae artifact is already enabled");
274 }
275
276 static uint16_t c = 0;
277 static int d = 0;
278
279 params.assocGalleryMinutiae = &c;
280 params.assocProbeMinutiae = &c;
281 params.associationCount = &d;
282 }
283 };
284
287 */
288 class Config : public Serializable
289 {
290 public:
302 */
303 void SetMaxRotation(unsigned int value)
304 {
305 static constexpr unsigned int MaxRotation = 180;
306 if (value > MaxRotation)
307 {
308 throw EnrollmentInvalidArgumentException("Max rotation MUST be in range <0,180>");
309 }
310 maxRotation = value;
311 }
316 */
317 [[nodiscard]] unsigned int GetMaxRotation() const
318 {
319 return maxRotation;
320 }
324 */
325 void WriteTo(Writer& writer) const final
326 {
327 writer.Write("r", maxRotation);
328 }
329
335 */
336 static Config Default()
337 {
338 static const Config Cfg;
339 return Cfg;
340 }
341
346 Config() = default;
347 Config(const Config&) = default;
348 Config(Config&&) = default;
349 Config& operator=(const Config&) = default;
350 Config& operator=(Config&&) = default;
351 virtual ~Config() = default;
352
353 private:
354 static constexpr unsigned int DefaultMaxRotation = 180;
355 unsigned int maxRotation = DefaultMaxRotation;
356 };
357
363 */
364 [[nodiscard]] PrintSimilarityScore Match(const ICSTemplate& probe, const ICSTemplate& gallery) const final
365 {
366 auto nullArtifact = NullArtifact();
367 return Match(probe, gallery, nullArtifact);
368 }
369
376 */
377 [[nodiscard]] PrintSimilarityScore Match(const ICSTemplate& probe,
378 const ICSTemplate& gallery,
379 Artifact& artifact) const
380 {
381 log.LogInfo("Matching prints probe %v and gallery %v with config %v", probe, gallery, cfg);
382 const CallbackLogger slog(log.GetLogger());
383 inno_ICSVerifyMatcher_params params = {};
384 params.galleryData = gallery.icsTemplateData.data();
385 params.gallerySize = gallery.icsTemplateData.size();
386 params.probeData = probe.icsTemplateData.data();
387 params.probeSize = probe.icsTemplateData.size();
388 params.speed = 0;
389 params.maxRotation = static_cast<int>(cfg.GetMaxRotation());
390
391 artifact.ConfigureMatchingParameters(probe, gallery, params);
392
393 exec->MatchICS(&params, slog.GetCallbacks(), ThrowOnError{});
394
395 artifact.OnCreated();
396
397 log.LogInfo("Matching prints probe %v and gallery %v with score %d", probe, gallery, params.score);
398 return params.score;
399 }
400
404 */
406 : cfg(Config::Default())
407 , log(GlobalLogger::Get())
408 , exec(GlobalPrintExecutor::Get())
409 {
410 }
411
416 */
417 explicit ICSVerifyMatcher(Config config)
418 : cfg(std::move(config))
419 , log(GlobalLogger::Get())
420 , exec(GlobalPrintExecutor::Get())
421 {
422 }
423
429 */
430 ICSVerifyMatcher(Config config, std::shared_ptr<PrintExecutor> executor)
431 : cfg(std::move(config))
432 , log(GlobalLogger::Get())
433 , exec(std::move(executor))
434 {
435 if (exec == nullptr)
436 {
437 throw NullArgumentException("executor can not be null");
438 }
439 }
440
445 */
446 explicit ICSVerifyMatcher(std::shared_ptr<PrintExecutor> executor)
447 : cfg(Config::Default())
448 , log(GlobalLogger::Get())
449 , exec(std::move(executor))
450 {
451 if (exec == nullptr)
452 {
453 throw NullArgumentException("executor can not be null");
454 }
455 }
456
457 ICSVerifyMatcher(const ICSVerifyMatcher&) = delete;
459 ICSVerifyMatcher& operator=(const ICSVerifyMatcher&) = delete;
460 ICSVerifyMatcher& operator=(ICSVerifyMatcher&&) = delete;
461 virtual ~ICSVerifyMatcher() = default;
462
463 private:
464 Config cfg;
466 std::shared_ptr<PrintExecutor> exec;
467
468 class NullArtifact final : public Artifact
469 {
470 private:
471 void ConfigureMatchingParameters(const ICSTemplate& /* probe */,
472 const ICSTemplate& /* gallery */,
473 inno_ICSVerifyMatcher_params& /* params */) final
474 {
475 }
476 void OnCreated() final
477 {
478 }
479 void CheckCompatibility(inno_ICSVerifyMatcher_params& /*params*/) final
480 {
481 }
482
483 public:
484 NullArtifact() = default;
485 NullArtifact(const NullArtifact&) = delete;
486 NullArtifact(NullArtifact&&) = delete;
487 NullArtifact& operator=(const NullArtifact&) = delete;
488 NullArtifact& operator=(NullArtifact&& c) = delete;
489 virtual ~NullArtifact() = default;
490 };
491 };
492} // namespace inno
CallbackLogger is logger for binary part of libary due to C boundary.
Definition Logger.h:157
LoggerCallBacks GetCallbacks() const
Get the Callbacks object.
Definition Logger.h:166
Base exception for all enrollment-sdk invalid argument exceptions.
Definition EnrollmentException.h:96
Base exception for all enrollment-sdk runtime exceptions.
Definition EnrollmentException.h:84
It provides functions with format specifier to compose and log message via provided Logger.
Definition FormattingLogger.h:601
Holds instance of Logger.
Definition Logger.h:275
Holds instance of PrintExecutor.
Definition PrintExecutor.h:1300
Print template in ICS format used for biometric operations.
Definition ICSTemplate.h:41
const std::vector< uint8_t > icsTemplateData
template data
Definition ICSTemplate.h:44
Interface that generalize storing of artifacts that are produced during matching.
Definition ICSVerifyMatcher.h:39
void Add(Artifact &artifact)
Add artifact to be created during matching.
Definition ICSVerifyMatcher.h:87
ICSVerifyMatcher configuration.
Definition ICSVerifyMatcher.h:288
void WriteTo(Writer &writer) const final
Function serializes configuration via provided Writer.
Definition ICSVerifyMatcher.h:324
void SetMaxRotation(unsigned int value)
Set the Max Rotation.
Definition ICSVerifyMatcher.h:302
static Config Default()
Provides default config.
Definition ICSVerifyMatcher.h:335
Config()=default
Default configuration is.
unsigned int GetMaxRotation() const
Get the Max Rotation.
Definition ICSVerifyMatcher.h:316
It enables creation of matching minutiae during matching.
Definition ICSVerifyMatcher.h:165
std::vector< std::shared_ptr< Minutia > > GetGalleryMinutiae() const
Get gallery matching minutiae.
Definition ICSVerifyMatcher.h:188
std::vector< std::shared_ptr< Minutia > > GetProbeMinutiae() const
Get probe matching minutiae.
Definition ICSVerifyMatcher.h:172
ICSVerifyMatcher is template matcher that uses Innovatrics proprietary verification algorithm.
Definition ICSVerifyMatcher.h:32
ICSVerifyMatcher()
Constructor with default Config GlobalPrintExecutor is used to execute matching.
Definition ICSVerifyMatcher.h:404
PrintSimilarityScore Match(const ICSTemplate &probe, const ICSTemplate &gallery) const final
Calculates similarity score of probe and gallery template.
Definition ICSVerifyMatcher.h:363
ICSVerifyMatcher(std::shared_ptr< PrintExecutor > executor)
Constructor with default Config.
Definition ICSVerifyMatcher.h:445
ICSVerifyMatcher(Config config, std::shared_ptr< PrintExecutor > executor)
Constructor uses Config to setup matching algorithm.
Definition ICSVerifyMatcher.h:429
PrintSimilarityScore Match(const ICSTemplate &probe, const ICSTemplate &gallery, Artifact &artifact) const
Calculates similarity score of probe and gallery template and provide artifacts.
Definition ICSVerifyMatcher.h:376
ICSVerifyMatcher(Config config)
Constructor uses Config to setup matching algorithm.
Definition ICSVerifyMatcher.h:416
Interface for library logging.
Definition Logger.h:45
Null argument is not allowed.
Definition EnrollmentException.h:156
Provides interface for serialization of all classes.
Definition Writer.h:164
unsigned int PrintSimilarityScore
It is similarity score between print Templates.
Definition SimilarityScore.h:28