janus_io.cpp
2.74 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
#include <QtConcurrent>
#define JANUS_CUSTOM_ADD_SAMPLE
#define JANUS_CUSTOM_CREATE_GALLERY
#define JANUS_CUSTOM_CREATE_TEMPLATES
#include "janus/src/janus_io.cpp"
static void _janus_add_sample(vector<double> &samples, double sample)
{
static QMutex sampleLock;
QMutexLocker sampleLocker(&sampleLock);
samples.push_back(sample);
}
static void _janus_create_template(const char *data_path, TemplateData templateData, janus_gallery gallery, bool verbose)
{
janus_template template_;
janus_template_id templateID;
JANUS_ASSERT(TemplateIterator::create(data_path, templateData, &template_, &templateID, verbose))
static QMutex enrollLock;
QMutexLocker enrollLocker(&enrollLock);
JANUS_ASSERT(janus_enroll(template_, templateID, gallery))
}
janus_error janus_create_gallery(const char *data_path, janus_metadata metadata, janus_gallery gallery, int verbose)
{
TemplateIterator ti(metadata, true);
TemplateData templateData = ti.next();
QFutureSynchronizer<void> futures;
while (!templateData.templateIDs.empty()) {
futures.addFuture(QtConcurrent::run(_janus_create_template, data_path, templateData, gallery, verbose));
templateData = ti.next();
}
futures.waitForFinished();
return JANUS_SUCCESS;
}
typedef QPair<janus_template_id, FlatTemplate> TemplatePair;
TemplatePair _janus_create_flat_template(const char *data_path, TemplateData templateData, bool verbose)
{
janus_template template_;
janus_template_id templateID;
JANUS_ASSERT(TemplateIterator::create(data_path, templateData, &template_, &templateID, verbose))
return TemplatePair(templateID, FlatTemplate(template_));
}
janus_error janus_create_templates(const char *data_path, janus_metadata metadata, const char *gallery_file, int verbose)
{
TemplateIterator ti(metadata, true);
TemplateData templateData = ti.next();
QFutureSynchronizer<TemplatePair> futures;
while (!templateData.templateIDs.empty()) {
futures.addFuture(QtConcurrent::run(_janus_create_flat_template, data_path, templateData, verbose));
templateData = ti.next();
}
futures.waitForFinished();
QList< QFuture<TemplatePair> > flat_templates = futures.futures();
std::ofstream file;
file.open(gallery_file, std::ios::out | std::ios::binary);
foreach (const QFuture<TemplatePair> &future, flat_templates) {
janus_template_id templateID = future.result().first;
FlatTemplate flatTemplate = future.result().second;
file.write((char*)&templateID, sizeof(templateID));
file.write((char*)&flatTemplate.data->bytes, sizeof(flatTemplate.data->bytes));
file.write((char*)flatTemplate.data->flat_template, flatTemplate.data->bytes);
}
file.close();
return JANUS_SUCCESS;
}