ct8.cpp
10.3 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <QMap>
#include <QVariant>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <frsdk/config.h>
#include <frsdk/cptr.h>
#include <frsdk/enroll.h>
#include <frsdk/face.h>
#include <frsdk/image.h>
#include <frsdk/match.h>
#include <exception>
#include <string>
#include <vector>
#include <mm_plugin.h>
#include "model.h"
#include "resource.h"
using namespace cv;
using namespace mm;
namespace FRsdk {
struct OpenCVImageBody : public ImageBody
{
OpenCVImageBody(const Mat &m, const std::string& _name = "")
: w(m.cols), h(m.rows), b(0), rgb(0), n(_name)
{
buildRgbRepresentation(m);
buildByteRepresentation();
}
~OpenCVImageBody()
{
delete[] rgb;
delete[] b;
}
bool isColor() const { return true; }
unsigned int height() const { return h; }
unsigned int width() const { return w; }
const Byte* grayScaleRepresentation() const { return b; }
const Rgb* colorRepresentation() const { return rgb; }
std::string name() const { return n;}
void buildRgbRepresentation(const Mat &m)
{
// layout required by FRsdk::Images: [Blue Green Red NotUsed]; no padding
rgb = new Rgb[w * h * sizeof(Rgb)];
std::vector<Mat> mv;
split(m, mv);
for (unsigned int i = 0; i < h; i++) {
for (unsigned int k = 0; k < w; k++) {
rgb[i*w+k].b = mv[0 % mv.size()].at<uchar>(i, k);
rgb[i*w+k].g = mv[1 % mv.size()].at<uchar>(i, k);
rgb[i*w+k].r = mv[2 % mv.size()].at<uchar>(i, k);
rgb[i*w+k].a = 0;
}
}
}
void buildByteRepresentation()
{
b = new Byte[w*h];
Rgb* colorp = rgb;
Byte* grayp = b;
for( unsigned int i = 0; i < h; i++ ) {
for( unsigned int k = 0; k < w; k++ ) {
float f = (float) colorp->r;
f += (float) colorp->g;
f += (float) colorp->b;
f /= 3.0f;
if( f > 255.0f) f = 255.0f;
*grayp = (Byte) f;
colorp++;
grayp++;
}
}
}
private:
unsigned int w;
unsigned int h;
Byte* b;
Rgb* rgb;
std::string n;
};
struct EnrolOpenCVFeedback : public Enrollment::FeedbackBody
{
EnrolOpenCVFeedback(Mat *_m)
: m(_m), firvalid(false)
{}
EnrolOpenCVFeedback() {}
void start()
{
firvalid = false;
}
void processingImage(const FRsdk::Image& img) { (void) img; }
void eyesFound( const FRsdk::Eyes::Location& eyeLoc) { (void) eyeLoc; }
void eyesNotFound() {}
void sampleQualityTooLow() {}
void sampleQuality(const float& f) { (void) f; }
void success(const FRsdk::FIR& _fir)
{
fir = new FRsdk::FIR(_fir);
m->create(1, fir->size(), CV_8UC1);
fir->serialize((Byte*)m->data);
firvalid = true;
}
void failure() {}
void end() {}
const FRsdk::FIR& getFir() const
{
if (!firvalid) qFatal("FRsdk::EnrolOpenCVFeedback::getFIR no FIR.");
return *fir;
}
bool firValid() const
{
return firvalid;
}
private:
FRsdk::CountedPtr<FRsdk::FIR> fir;
Mat *m;
bool firvalid;
};
}
struct CT8Initialize : public Initializer
{
static FRsdk::Configuration* CT8Configuration;
void initialize() const
{
QFile file(":/3rdparty/ct8/activationkey.cfg");
file.open(QFile::ReadOnly);
QByteArray data = file.readAll();
file.close();
std::istringstream istream(QString(data).arg(Globals.SDKPath+"/models/ct8").toStdString());
try {
CT8Configuration = new FRsdk::Configuration(istream);
} catch (std::exception &e) {
qWarning("CT8Initialize Exception: %s", e.what());
CT8Configuration = NULL;
}
}
void finalize() const
{
delete CT8Configuration;
CT8Configuration = NULL;
}
};
FRsdk::Configuration* CT8Initialize::CT8Configuration = NULL;
MM_REGISTER(Initializer, CT8Initialize, false)
class CT8EnrollmentProcessorResource : public Resource<FRsdk::Enrollment::Processor>
{
QSharedPointer<FRsdk::Enrollment::Processor> make() const
{
return QSharedPointer<FRsdk::Enrollment::Processor>(new FRsdk::Enrollment::Processor(*CT8Initialize::CT8Configuration));
}
};
struct CT8Context
{
CT8Context()
{
try {
faceFinder = new FRsdk::Face::Finder(*CT8Initialize::CT8Configuration);
eyesFinder = new FRsdk::Eyes::Finder(*CT8Initialize::CT8Configuration);
firBuilder = new FRsdk::FIRBuilder(*CT8Initialize::CT8Configuration);
facialMatchingEngine = new FRsdk::FacialMatchingEngine(*CT8Initialize::CT8Configuration);
enrollmentProcessors.init();
} catch (std::exception &e) {
qFatal("CT8Context Exception: %s", e.what());
}
}
~CT8Context()
{
delete faceFinder;
delete eyesFinder;
delete firBuilder;
delete facialMatchingEngine;
}
void enroll(const FRsdk::Image &img, const FRsdk::Eyes::Location &eyes, Mat *m) const
{
try {
FRsdk::Sample sample(FRsdk::AnnotatedImage(img, eyes));
FRsdk::SampleSet sampleSet;
sampleSet.push_back(sample);
FRsdk::Enrollment::Feedback enrollmentFeedback(new FRsdk::EnrolOpenCVFeedback(m));
int index;
QSharedPointer<FRsdk::Enrollment::Processor> enrollmentProcessor = enrollmentProcessors.acquire(index);
enrollmentProcessor->process(sampleSet.begin(), sampleSet.end(), enrollmentFeedback);
enrollmentProcessors.release(index);
} catch (std::exception &e) {
qFatal("CT8Context Exception: %s", e.what());
}
}
static FRsdk::Position toPosition(const Point2f &point)
{
return FRsdk::Position(point.x, point.y);
}
protected:
FRsdk::Face::Finder *faceFinder;
FRsdk::Eyes::Finder *eyesFinder;
CT8EnrollmentProcessorResource enrollmentProcessors;
FRsdk::FIRBuilder *firBuilder;
FRsdk::FacialMatchingEngine *facialMatchingEngine;
};
struct CT8Detect : public UntrainableFeature
, public CT8Context
{
void project(const Template &src, Template &dst) const
{
try {
FRsdk::CountedPtr<FRsdk::ImageBody> i(new FRsdk::OpenCVImageBody(src));
FRsdk::Image img(i);
FRsdk::Face::LocationSet faceLocations = faceFinder->find(img, 0.01);
QList<Rect> ROIs;
QList<Point2f> landmarks;
FRsdk::Face::LocationSet::const_iterator faceLocationSetIterator = faceLocations.begin();
while (faceLocationSetIterator != faceLocations.end()) {
FRsdk::Face::Location faceLocation = *faceLocationSetIterator; faceLocationSetIterator++;
FRsdk::Eyes::LocationSet currentEyesLocations = eyesFinder->find(img, faceLocation);
if (currentEyesLocations.size() > 0) {
ROIs.append(Rect(faceLocation.pos.x(), faceLocation.pos.y(), faceLocation.width, faceLocation.width));
landmarks.append(Point2f(currentEyesLocations.front().first.x(), currentEyesLocations.front().first.y()));
landmarks.append(Point2f(currentEyesLocations.front().second.x(), currentEyesLocations.front().second.y()));
dst += src;
}
if (!Globals.EnrollAll && !dst.isEmpty()) break;
}
dst.file.setROIs(ROIs);
dst.file.setLandmarks(landmarks);
} catch (std::exception &e) {
qFatal("CT8Enroll Exception: %s", e.what());
}
if (!Globals.EnrollAll && dst.isEmpty()) dst += Mat();
}
};
MM_REGISTER(Feature, CT8Detect, false)
struct CT8Enroll : public UntrainableFeature
, public CT8Context
{
void project(const Template &src, Template &dst) const
{
try {
FRsdk::CountedPtr<FRsdk::ImageBody> i(new FRsdk::OpenCVImageBody(src));
FRsdk::Image img(i);
QList<Point2f> landmarks = src.file.landmarks();
if (landmarks.size() == 2) {
enroll(img, FRsdk::Eyes::Location(toPosition(landmarks[0]), toPosition(landmarks[1])), dst.mp());
dst.file.insert("CT8_First_Eye_X", landmarks[0].x);
dst.file.insert("CT8_First_Eye_Y", landmarks[0].y);
dst.file.insert("CT8_Second_Eye_X", landmarks[1].x);
dst.file.insert("CT8_Second_Eye_Y", landmarks[1].y);
QList<Rect> ROIs = src.file.ROIs();
if (ROIs.size() == 1) {
dst.file.insert("CT8_Face_X", ROIs.first().x);
dst.file.insert("CT8_Face_Y", ROIs.first().y);
dst.file.insert("CT8_Face_Width", ROIs.first().width);
dst.file.insert("CT8_Face_Height", ROIs.first().height);
}
} else {
dst = Mat();
}
} catch (std::exception &e) {
qFatal("CT8Enroll Exception: %s", e.what());
}
}
};
MM_REGISTER(Feature, CT8Enroll, false)
struct CT8Compare : public ComparerBase,
public CT8Context
{
float compare(const Mat &srcA, const Mat &srcB) const
{
const float DefaultNonMatchScore = 0;
if (!srcA.data || !srcB.data) return DefaultNonMatchScore;
float score = DefaultNonMatchScore;
try {
FRsdk::FIR firA = firBuilder->build((FRsdk::Byte*)srcA.data, srcA.cols);
FRsdk::FIR firB = firBuilder->build((FRsdk::Byte*)srcB.data, srcB.cols);
score = (float)facialMatchingEngine->compare(firA, firB);
} catch (std::exception &e) {
qFatal("CT8Compare Exception: %s", e.what());
}
return score;
}
};
MM_REGISTER(Comparer, CT8Compare, false)
MM_REGISTER_ALGORITHM(CT8, "Open+CT8Detect!CT8Enroll:CT8Compare")