openbr.cpp
14.4 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright 2012 The MITRE Corporation *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <openbr/openbr_plugin.h>
#include "core/bee.h"
#include "core/cluster.h"
#include "core/eval.h"
#include "core/fuse.h"
#include "core/plot.h"
#include "core/qtutils.h"
#include "plugins/openbr_internal.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
using namespace br;
static int partialCopy(const QString &string, char *buffer, int buffer_length)
{
QByteArray byteArray = string.toLocal8Bit();
int copyLength = std::min(buffer_length-1, byteArray.size());
if (copyLength < 0)
return byteArray.size() + 1;
memcpy(buffer, byteArray.data(), copyLength);
buffer[copyLength] = '\0';
return byteArray.size() + 1;
}
const char *br_about()
{
static QMutex aboutLock;
QMutexLocker lock(&aboutLock);
static QByteArray about = Context::about().toLocal8Bit();
return about.data();
}
void br_cat(int num_input_galleries, const char *input_galleries[], const char *output_gallery)
{
Cat(QtUtils::toStringList(num_input_galleries, input_galleries), output_gallery);
}
void br_cluster(int num_simmats, const char *simmats[], float aggressiveness, const char *csv)
{
ClusterSimmat(QtUtils::toStringList(num_simmats, simmats), aggressiveness, csv);
}
void br_combine_masks(int num_input_masks, const char *input_masks[], const char *output_mask, const char *method)
{
BEE::combineMasks(QtUtils::toStringList(num_input_masks, input_masks), output_mask, method);
}
void br_compare(const char *target_gallery, const char *query_gallery, const char *output)
{
Compare(File(target_gallery), File(query_gallery), File(output));
}
void br_compare_n(int num_targets, const char *target_galleries[], const char *query_gallery, const char *output)
{
if (num_targets > 1) Compare(QtUtils::toStringList(num_targets, target_galleries).join(";")+"(separator=;)", File(query_gallery), File(output));
else Compare(File(target_galleries[0]), File(query_gallery), File(output));
}
void br_pairwise_compare(const char *target_gallery, const char *query_gallery, const char *output)
{
PairwiseCompare(File(target_gallery), File(query_gallery), File(output));
}
void br_convert(const char *file_type, const char *input_file, const char *output_file)
{
Convert(File(file_type), File(input_file), File(output_file));
}
void br_enroll(const char *input, const char *gallery)
{
Enroll(File(input), File(gallery));
}
void br_enroll_n(int num_inputs, const char *inputs[], const char *gallery)
{
if (num_inputs > 1) Enroll(QtUtils::toStringList(num_inputs, inputs).join(";")+"(separator=;)", File(gallery));
else Enroll(File(inputs[0]), gallery);
}
void br_project(const char *input, const char *gallery)
{
Project(File(input), File(gallery));
}
float br_eval(const char *simmat, const char *mask, const char *csv, int matches)
{
return Evaluate(simmat, mask, csv, matches);
}
void br_assert_eval(const char *simmat, const char *mask, const float accuracy)
{
assertEval(simmat, mask, accuracy);
}
float br_inplace_eval(const char *simmat, const char *target, const char *query, const char *csv)
{
return InplaceEval(simmat, target, query, csv);
}
void br_eval_classification(const char *predicted_gallery, const char *truth_gallery, const char *predicted_property, const char *truth_property)
{
EvalClassification(predicted_gallery, truth_gallery, predicted_property, truth_property);
}
void br_eval_clustering(const char *csv, const char *gallery, const char *truth_property)
{
EvalClustering(csv, gallery, truth_property);
}
float br_eval_detection(const char *predicted_gallery, const char *truth_gallery, const char *csv, bool normalize, int minSize, int maxSize)
{
return EvalDetection(predicted_gallery, truth_gallery, csv, normalize, minSize, maxSize);
}
float br_eval_landmarking(const char *predicted_gallery, const char *truth_gallery, const char *csv, int normalization_index_a, int normalization_index_b, int sample_index, int total_examples)
{
return EvalLandmarking(predicted_gallery, truth_gallery, csv, normalization_index_a, normalization_index_b, sample_index, total_examples);
}
void br_eval_regression(const char *predicted_gallery, const char *truth_gallery, const char *predicted_property, const char *truth_property)
{
EvalRegression(predicted_gallery, truth_gallery, predicted_property, truth_property);
}
void br_finalize()
{
Context::finalize();
}
void br_fuse(int num_input_simmats, const char *input_simmats[],
const char *normalization, const char *fusion, const char *output_simmat)
{
Fuse(QtUtils::toStringList(num_input_simmats, input_simmats), normalization, fusion, output_simmat);
}
void br_initialize(int &argc, char *argv[], const char *sdk_path, bool use_gui)
{
Context::initialize(argc, argv, sdk_path, use_gui);
}
void br_initialize_default()
{
int argc = 1;
char app[] = "br";
char *argv[1] = {app};
Context::initialize(argc, argv, "");
}
bool br_is_classifier(const char *algorithm)
{
return IsClassifier(algorithm);
}
void br_make_mask(const char *target_input, const char *query_input, const char *mask)
{
BEE::makeMask(target_input, query_input, mask);
}
void br_make_pairwise_mask(const char *target_input, const char *query_input, const char *mask)
{
BEE::makePairwiseMask(target_input, query_input, mask);
}
int br_most_recent_message(char *buffer, int buffer_length)
{
return partialCopy(Globals->mostRecentMessage, buffer, buffer_length);
}
int br_objects(char *buffer, int buffer_length, const char *abstractions, const char *implementations, bool parameters)
{
return partialCopy(br::Context::objects(abstractions, implementations, parameters).join('\n'), buffer, buffer_length);
}
bool br_plot(int num_files, const char *files[], const char *destination, bool show)
{
return Plot(QtUtils::toStringList(num_files, files), destination, show);
}
bool br_plot_detection(int num_files, const char *files[], const char *destination, bool show)
{
return PlotDetection(QtUtils::toStringList(num_files, files), destination, show);
}
bool br_plot_landmarking(int num_files, const char *files[], const char *destination, bool show)
{
return PlotLandmarking(QtUtils::toStringList(num_files, files), destination, show);
}
bool br_plot_metadata(int num_files, const char *files[], const char *columns, bool show)
{
return PlotMetadata(QtUtils::toStringList(num_files, files), columns, show);
}
float br_progress()
{
return Globals->progress();
}
void br_read_pipe(const char *pipe, int *argc, char ***argv)
{
static QList<QByteArray> byteArrayList;
static QVector<char*> rawCharArrayList;
QFile file(pipe);
file.open(QFile::ReadOnly);
QTextStream stream(&file);
QStringList args;
while (args.isEmpty()) {
args = QtUtils::parse(stream.readAll(), ' ');
if (args.isEmpty()) QThread::sleep(100);
}
file.close();
byteArrayList.clear(); rawCharArrayList.clear();
foreach (const QString &string, args) {
byteArrayList.append(string.toLocal8Bit());
rawCharArrayList.append(byteArrayList.last().data());
}
*argc = byteArrayList.size();
*argv = rawCharArrayList.data();
}
int br_scratch_path(char *buffer, int buffer_length)
{
return partialCopy(Context::scratchPath(), buffer, buffer_length);
}
const char *br_sdk_path()
{
static QMutex sdkLock;
QMutexLocker lock(&sdkLock);
static QByteArray sdkPath = QDir(Globals->sdkPath).absolutePath().toLocal8Bit();
return sdkPath.data();
}
void br_get_header(const char *matrix, const char **target_gallery, const char **query_gallery)
{
static QByteArray targetGalleryData, queryGalleryData;
QString targetGalleryString, queryGalleryString;
BEE::readMatrixHeader(matrix, &targetGalleryString, &queryGalleryString);
targetGalleryData = targetGalleryString.toLatin1();
queryGalleryData = queryGalleryString.toLatin1();
*target_gallery = targetGalleryData.data();
*query_gallery = queryGalleryData.data();
}
void br_set_header(const char *matrix, const char *target_gallery, const char *query_gallery)
{
BEE::writeMatrixHeader(matrix, target_gallery, query_gallery);
}
void br_set_property(const char *key, const char *value)
{
Globals->setProperty(key, value);
}
int br_time_remaining()
{
return Globals->timeRemaining();
}
void br_train(const char *input, const char *model)
{
Train(input, model);
}
void br_train_n(int num_inputs, const char *inputs[], const char *model)
{
if (num_inputs > 1) Train(QtUtils::toStringList(num_inputs, inputs).join(";")+"(separator=;)", File(model));
else Train(File(inputs[0]), model);
}
const char *br_version()
{
static QMutex versionLock;
QMutexLocker lock(&versionLock);
static QByteArray version = Context::version().toLocal8Bit();
return version.data();
}
void br_slave_process(const char *baseName)
{
WorkerProcess *worker = new WorkerProcess;
worker->transform = Globals->algorithm;
worker->baseName = baseName;
worker->mainLoop();
delete worker;
}
br_template br_load_img(const char *data, int len)
{
std::vector<char> buf(data, data+len);
cv::Mat img = cv::imdecode(cv::Mat(buf), CV_LOAD_IMAGE_COLOR);
Template *tmpl = new Template(img);
return (br_template)tmpl;
}
unsigned char *br_unload_img(br_template tmpl)
{
Template *t = reinterpret_cast<Template*>(tmpl);
return t->m().data;
}
br_template_list br_template_list_from_buffer(const char *buf, int len)
{
QByteArray arr(buf, len);
TemplateList *tl = new TemplateList();
*tl = TemplateList::fromBuffer(arr);
return (br_template_list)tl;
}
void br_free_template(br_template tmpl)
{
Template *t = reinterpret_cast<Template*>(tmpl);
delete t;
}
void br_free_template_list(br_template_list tl)
{
TemplateList *realTL = reinterpret_cast<TemplateList*>(tl);
delete realTL;
}
void br_free_output(br_matrix_output output)
{
MatrixOutput *matOut = reinterpret_cast<MatrixOutput*>(output);
delete matOut;
}
int br_img_rows(br_template tmpl)
{
Template *t = reinterpret_cast<Template*>(tmpl);
return t->m().rows;
}
int br_img_cols(br_template tmpl)
{
Template *t = reinterpret_cast<Template*>(tmpl);
return t->m().cols;
}
int br_img_channels(br_template tmpl)
{
Template *t = reinterpret_cast<Template*>(tmpl);
return t->m().channels();
}
bool br_img_is_empty(br_template tmpl)
{
Template *t = reinterpret_cast<Template*>(tmpl);
return t->m().empty();
}
int br_get_filename(char *buffer, int buffer_length, br_template tmpl)
{
return partialCopy(reinterpret_cast<Template*>(tmpl)->file.name, buffer, buffer_length);
}
void br_set_filename(br_template tmpl, const char *filename)
{
Template *t = reinterpret_cast<Template*>(tmpl);
t->file.name = filename;
}
int br_get_metadata_string(char *buffer, int buffer_length, br_template tmpl, const char *key)
{
Template *t = reinterpret_cast<Template*>(tmpl);
QVariant qvar = t->file.value(key);
return partialCopy(QtUtils::toString(qvar), buffer, buffer_length);
}
br_template_list br_enroll_template(br_template tmpl)
{
Template *t = reinterpret_cast<Template*>(tmpl);
TemplateList *tl = new TemplateList();
tl->append(*t);
Enroll(*tl);
return (br_template_list)tl;
}
void br_enroll_template_list(br_template_list tl)
{
TemplateList *realTL = reinterpret_cast<TemplateList*>(tl);
Enroll(*realTL);
}
br_matrix_output br_compare_template_lists(br_template_list target, br_template_list query)
{
TemplateList *targetTL = reinterpret_cast<TemplateList*>(target);
TemplateList *queryTL = reinterpret_cast<TemplateList*>(query);
MatrixOutput *output = MatrixOutput::make(targetTL->files(), queryTL->files());
CompareTemplateLists(*targetTL, *queryTL, output);
return (br_matrix_output)output;
}
float br_get_matrix_output_at(br_matrix_output output, int row, int col)
{
MatrixOutput *matOut = reinterpret_cast<MatrixOutput*>(output);
return matOut->data.at<float>(row, col);
}
br_template br_get_template(br_template_list tl, int index)
{
TemplateList *realTL = reinterpret_cast<TemplateList*>(tl);
return (br_template)&realTL->at(index);
}
int br_num_templates(br_template_list tl)
{
TemplateList *realTL = reinterpret_cast<TemplateList*>(tl);
return realTL->size();
}
br_gallery br_make_gallery(const char *gallery)
{
Gallery *gal = Gallery::make(File(gallery));
return (br_gallery)gal;
}
br_template_list br_load_from_gallery(br_gallery gallery)
{
Gallery *gal = reinterpret_cast<Gallery*>(gallery);
TemplateList *tl = new TemplateList();
*tl = gal->read();
return (br_template_list)tl;
}
void br_add_template_to_gallery(br_gallery gallery, br_template tmpl)
{
Gallery *gal = reinterpret_cast<Gallery*>(gallery);
Template *t = reinterpret_cast<Template*>(tmpl);
gal->write(*t);
}
void br_add_template_list_to_gallery(br_gallery gallery, br_template_list tl)
{
Gallery *gal = reinterpret_cast<Gallery*>(gallery);
TemplateList *realTL = reinterpret_cast<TemplateList*>(tl);
gal->writeBlock(*realTL);
}
void br_close_gallery(br_gallery gallery)
{
Gallery *gal = reinterpret_cast<Gallery*>(gallery);
delete gal;
}
void br_deduplicate(const char *input_gallery, const char *output_gallery, const char *threshold)
{
br::Deduplicate(input_gallery, output_gallery, threshold);
}