gallery.cpp
62.8 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 <QtCore>
#include <QtConcurrentRun>
#ifndef BR_EMBEDDED
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QXmlStreamReader>
#endif // BR_EMBEDDED
#include <opencv2/highgui/highgui.hpp>
#include "openbr_internal.h"
#include "openbr/universal_template.h"
#include "openbr/core/bee.h"
#include "openbr/core/common.h"
#include "openbr/core/opencvutils.h"
#include "openbr/core/qtutils.h"
#include <fstream>
#ifdef CVMATIO
#include "MatlabIO.hpp"
#include "MatlabIOContainer.hpp"
#endif
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif // _WIN32
namespace br
{
/*!
* \ingroup galleries
* \brief Weka ARFF file format.
* \author Josh Klontz \cite jklontz
* http://weka.wikispaces.com/ARFF+%28stable+version%29
*/
class arffGallery : public Gallery
{
Q_OBJECT
QFile arffFile;
TemplateList readBlock(bool *done)
{
(void) done;
qFatal("Not implemented.");
return TemplateList();
}
void write(const Template &t)
{
if (!arffFile.isOpen()) {
arffFile.setFileName(file.name);
arffFile.open(QFile::WriteOnly);
arffFile.write("% OpenBR templates\n"
"@RELATION OpenBR\n"
"\n");
const int dimensions = t.m().rows * t.m().cols;
for (int i=0; i<dimensions; i++)
arffFile.write(qPrintable("@ATTRIBUTE v" + QString::number(i) + " REAL\n"));
arffFile.write(qPrintable("@ATTRIBUTE class string\n"));
arffFile.write("\n@DATA\n");
}
arffFile.write(qPrintable(OpenCVUtils::matrixToStringList(t).join(',')));
arffFile.write(qPrintable(",'" + t.file.get<QString>("Label") + "'\n"));
}
void init()
{
//
}
};
BR_REGISTER(Gallery, arffGallery)
class BinaryGallery : public Gallery
{
Q_OBJECT
void init()
{
const QString baseName = file.baseName();
if (baseName == "stdin") {
#ifdef _WIN32
if(_setmode(_fileno(stdin), _O_BINARY) == -1)
qFatal("Failed to set stdin to binary mode!");
#endif // _WIN32
gallery.open(stdin, QFile::ReadOnly);
} else if (baseName == "stdout") {
#ifdef _WIN32
if(_setmode(_fileno(stdout), _O_BINARY) == -1)
qFatal("Failed to set stdout to binary mode!");
#endif // _WIN32
gallery.open(stdout, QFile::WriteOnly);
} else if (baseName == "stderr") {
#ifdef _WIN32
if(_setmode(_fileno(stderr), _O_BINARY) == -1)
qFatal("Failed to set stderr to binary mode!");
#endif // _WIN32
gallery.open(stderr, QFile::WriteOnly);
} else {
// Defer opening the file, in the general case we don't know if we
// need read or write mode yet
return;
}
stream.setDevice(&gallery);
}
void readOpen()
{
if (!gallery.isOpen()) {
gallery.setFileName(file);
if (!gallery.exists())
qFatal("File %s does not exist", qPrintable(gallery.fileName()));
QFile::OpenMode mode = QFile::ReadOnly;
if (!gallery.open(mode))
qFatal("Can't open gallery: %s for reading", qPrintable(gallery.fileName()));
stream.setDevice(&gallery);
}
}
void writeOpen()
{
if (!gallery.isOpen()) {
gallery.setFileName(file);
// Do we remove the pre-existing gallery?
if (file.get<bool>("remove"))
gallery.remove();
QtUtils::touchDir(gallery);
QFile::OpenMode mode = QFile::WriteOnly;
// Do we append?
if (file.get<bool>("append"))
mode |= QFile::Append;
if (!gallery.open(mode))
qFatal("Can't open gallery: %s for writing", qPrintable(gallery.fileName()));
stream.setDevice(&gallery);
}
}
TemplateList readBlock(bool *done)
{
readOpen();
if (gallery.atEnd())
gallery.seek(0);
TemplateList templates;
while ((templates.size() < readBlockSize) && !gallery.atEnd()) {
const Template t = readTemplate();
if (!t.isEmpty() || !t.file.isNull()) {
templates.append(t);
templates.last().file.set("progress", position());
}
// Special case for pipes where we want to process data as soon as it is available
if (gallery.isSequential())
break;
}
*done = gallery.atEnd();
return templates;
}
void write(const Template &t)
{
writeOpen();
writeTemplate(t);
if (gallery.isSequential())
gallery.flush();
}
protected:
QFile gallery;
QDataStream stream;
qint64 totalSize()
{
readOpen();
return gallery.size();
}
qint64 position()
{
return gallery.pos();
}
virtual Template readTemplate() = 0;
virtual void writeTemplate(const Template &t) = 0;
};
/*!
* \ingroup galleries
* \brief A binary gallery.
*
* Designed to be a literal translation of templates to disk.
* Compatible with TemplateList::fromBuffer.
* \author Josh Klontz \cite jklontz
*/
class galGallery : public BinaryGallery
{
Q_OBJECT
Template readTemplate()
{
Template t;
stream >> t;
return t;
}
void writeTemplate(const Template &t)
{
if (t.isEmpty() && t.file.isNull())
return;
else if (t.file.fte)
stream << Template(t.file); // only write metadata for failure to enroll
else
stream << t;
}
};
BR_REGISTER(Gallery, galGallery)
/*!
* \ingroup galleries
* \brief A contiguous array of br_universal_template.
* \author Josh Klontz \cite jklontz
*/
class utGallery : public BinaryGallery
{
Q_OBJECT
Template readTemplate()
{
Template t;
br_universal_template ut;
if (gallery.read((char*)&ut, sizeof(br_universal_template)) == sizeof(br_universal_template)) {
QByteArray data(ut.urlSize + ut.fvSize, Qt::Uninitialized);
char *dst = data.data();
qint64 bytesNeeded = ut.urlSize + ut.fvSize;
while (bytesNeeded > 0) {
qint64 bytesRead = gallery.read(dst, bytesNeeded);
if (bytesRead <= 0) {
qDebug() << gallery.errorString();
qFatal("Unexepected EOF while reading universal template data, needed: %d more of: %d bytes.", int(bytesNeeded), int(ut.urlSize + ut.fvSize));
}
bytesNeeded -= bytesRead;
dst += bytesRead;
}
t.file.set("ImageID", QVariant(QByteArray((const char*)ut.imageID, 16).toHex()));
t.file.set("AlgorithmID", ut.algorithmID);
t.file.set("URL", QString(data.data()));
char *dataStart = data.data() + ut.urlSize;
uint32_t dataSize = ut.fvSize;
if ((ut.algorithmID <= -1) && (ut.algorithmID >= -3)) {
t.file.set("FrontalFace", QRectF(ut.x, ut.y, ut.width, ut.height));
uint32_t *rightEyeX = reinterpret_cast<uint32_t*>(dataStart);
dataStart += sizeof(uint32_t);
uint32_t *rightEyeY = reinterpret_cast<uint32_t*>(dataStart);
dataStart += sizeof(uint32_t);
uint32_t *leftEyeX = reinterpret_cast<uint32_t*>(dataStart);
dataStart += sizeof(uint32_t);
uint32_t *leftEyeY = reinterpret_cast<uint32_t*>(dataStart);
dataStart += sizeof(uint32_t);
dataSize -= sizeof(uint32_t)*4;
t.file.set("First_Eye", QPointF(*rightEyeX, *rightEyeY));
t.file.set("Second_Eye", QPointF(*leftEyeX, *leftEyeY));
} else {
t.file.set("X", ut.x);
t.file.set("Y", ut.y);
t.file.set("Width", ut.width);
t.file.set("Height", ut.height);
}
t.file.set("Label", ut.label);
t.append(cv::Mat(1, dataSize, CV_8UC1, dataStart).clone() /* We don't want a shallow copy! */);
} else {
if (!gallery.atEnd())
qFatal("Failed to read universal template header!");
}
return t;
}
void writeTemplate(const Template &t)
{
const QByteArray imageID = QByteArray::fromHex(t.file.get<QByteArray>("ImageID", QByteArray(32, '0')));
if (imageID.size() != 16)
qFatal("Expected 16-byte ImageID, got: %d bytes.", imageID.size());
const int32_t algorithmID = (t.isEmpty() || t.file.fte) ? 0 : t.file.get<int32_t>("AlgorithmID");
const QByteArray url = t.file.get<QString>("URL", t.file.name).toLatin1();
uint32_t x = 0, y = 0, width = 0, height = 0;
QByteArray header;
if ((algorithmID <= -1) && (algorithmID >= -3)) {
const QRectF frontalFace = t.file.get<QRectF>("FrontalFace");
x = frontalFace.x();
y = frontalFace.y();
width = frontalFace.width();
height = frontalFace.height();
const QPointF firstEye = t.file.get<QPointF>("First_Eye");
const QPointF secondEye = t.file.get<QPointF>("Second_Eye");
const uint32_t rightEyeX = firstEye.x();
const uint32_t rightEyeY = firstEye.y();
const uint32_t leftEyeX = secondEye.x();
const uint32_t leftEyeY = secondEye.y();
header.append((const char*)&rightEyeX, sizeof(uint32_t));
header.append((const char*)&rightEyeY, sizeof(uint32_t));
header.append((const char*)&leftEyeX , sizeof(uint32_t));
header.append((const char*)&leftEyeY , sizeof(uint32_t));
} else {
x = t.file.get<uint32_t>("X", 0);
y = t.file.get<uint32_t>("Y", 0);
width = t.file.get<uint32_t>("Width", 0);
height = t.file.get<uint32_t>("Height", 0);
}
const uint32_t label = t.file.get<uint32_t>("Label", 0);
gallery.write(imageID);
gallery.write((const char*) &algorithmID, sizeof(int32_t));
gallery.write((const char*) &x , sizeof(uint32_t));
gallery.write((const char*) &y , sizeof(uint32_t));
gallery.write((const char*) &width , sizeof(uint32_t));
gallery.write((const char*) &height , sizeof(uint32_t));
gallery.write((const char*) &label , sizeof(uint32_t));
const uint32_t urlSize = url.size() + 1;
gallery.write((const char*) &urlSize, sizeof(uint32_t));
const uint32_t signatureSize = (algorithmID == 0) ? 0 : t.m().rows * t.m().cols * t.m().elemSize();
const uint32_t fvSize = header.size() + signatureSize;
gallery.write((const char*) &fvSize, sizeof(uint32_t));
gallery.write((const char*) url.data(), urlSize);
if (algorithmID != 0) {
gallery.write(header);
gallery.write((const char*) t.m().data, signatureSize);
}
}
};
BR_REGISTER(Gallery, utGallery)
/*!
* \ingroup galleries
* \brief Newline-separated URLs.
* \author Josh Klontz \cite jklontz
*/
class urlGallery : public BinaryGallery
{
Q_OBJECT
Template readTemplate()
{
Template t;
const QString url = QString::fromLocal8Bit(gallery.readLine()).simplified();
if (!url.isEmpty())
t.file.set("URL", url);
return t;
}
void writeTemplate(const Template &t)
{
const QString url = t.file.get<QString>("URL", t.file.name);
if (!url.isEmpty()) {
gallery.write(qPrintable(url));
gallery.write("\n");
}
}
};
BR_REGISTER(Gallery, urlGallery)
/*!
* \ingroup galleries
* \brief Newline-separated JSON objects.
* \author Josh Klontz \cite jklontz
*/
class jsonGallery : public BinaryGallery
{
Q_OBJECT
Template readTemplate()
{
QJsonParseError error;
const QByteArray line = gallery.readLine().simplified();
if (line.isEmpty())
return Template();
File file = QJsonDocument::fromJson(line, &error).object().toVariantMap();
if (error.error != QJsonParseError::NoError) {
qWarning("Couldn't parse: %s\n", line.data());
qFatal("%s\n", qPrintable(error.errorString()));
}
return file;
}
void writeTemplate(const Template &t)
{
const QByteArray json = QJsonDocument(QJsonObject::fromVariantMap(t.file.localMetadata())).toJson().replace('\n', "");
if (!json.isEmpty()) {
gallery.write(json);
gallery.write("\n");
}
}
};
BR_REGISTER(Gallery, jsonGallery)
/*!
* \ingroup galleries
* \brief Reads/writes templates to/from folders.
* \author Josh Klontz \cite jklontz
* \param regexp An optional regular expression to match against the files extension.
*/
class EmptyGallery : public Gallery
{
Q_OBJECT
Q_PROPERTY(QString regexp READ get_regexp WRITE set_regexp RESET reset_regexp STORED false)
BR_PROPERTY(QString, regexp, QString())
qint64 gallerySize;
void init()
{
QDir dir(file.name);
QtUtils::touchDir(dir);
gallerySize = dir.count();
}
TemplateList readBlock(bool *done)
{
TemplateList templates;
*done = true;
// Enrolling a null file is used as an idiom to initialize an algorithm
if (file.isNull()) return templates;
// Add immediate subfolders
QDir dir(file);
QList< QFuture<TemplateList> > futures;
foreach (const QString &folder, QtUtils::naturalSort(dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))) {
const QDir subdir = dir.absoluteFilePath(folder);
futures.append(QtConcurrent::run(&EmptyGallery::getTemplates, subdir));
}
foreach (const QFuture<TemplateList> &future, futures)
templates.append(future.result());
// Add root folder
foreach (const QString &fileName, QtUtils::getFiles(file.name, false))
templates.append(File(fileName, dir.dirName()));
if (!regexp.isEmpty()) {
QRegExp re(regexp);
re.setPatternSyntax(QRegExp::Wildcard);
for (int i=templates.size()-1; i>=0; i--) {
if (!re.exactMatch(templates[i].file.fileName())) {
templates.removeAt(i);
}
}
}
for (int i = 0; i < templates.size(); i++) templates[i].file.set("progress", i);
return templates;
}
void write(const Template &t)
{
static QMutex diskLock;
// Enrolling a null file is used as an idiom to initialize an algorithm
if (file.name.isEmpty()) return;
const QString newFormat = file.get<QString>("newFormat",QString());
QString destination = file.name + "/" + (file.getBool("preservePath") ? t.file.path()+"/" : QString());
destination += (newFormat.isEmpty() ? t.file.fileName() : t.file.baseName()+newFormat);
QMutexLocker diskLocker(&diskLock); // Windows prefers to crash when writing to disk in parallel
if (t.isNull()) {
QtUtils::copyFile(t.file.resolved(), destination);
} else {
QScopedPointer<Format> format(Factory<Format>::make(destination));
format->write(t);
}
}
qint64 totalSize()
{
return gallerySize;
}
static TemplateList getTemplates(const QDir &dir)
{
const QStringList files = QtUtils::getFiles(dir, true);
TemplateList templates; templates.reserve(files.size());
foreach (const QString &file, files)
templates.append(File(file, dir.dirName()));
return templates;
}
};
BR_REGISTER(Gallery, EmptyGallery)
/*!
* \ingroup galleries
* \brief Crawl a root location for image files.
* \author Josh Klontz \cite jklontz
*/
class crawlGallery : public Gallery
{
Q_OBJECT
Q_PROPERTY(bool autoRoot READ get_autoRoot WRITE set_autoRoot RESET reset_autoRoot STORED false)
Q_PROPERTY(int depth READ get_depth WRITE set_depth RESET reset_depth STORED false)
Q_PROPERTY(bool depthFirst READ get_depthFirst WRITE set_depthFirst RESET reset_depthFirst STORED false)
Q_PROPERTY(int images READ get_images WRITE set_images RESET reset_images STORED false)
Q_PROPERTY(bool json READ get_json WRITE set_json RESET reset_json STORED false)
Q_PROPERTY(int timeLimit READ get_timeLimit WRITE set_timeLimit RESET reset_timeLimit STORED false)
BR_PROPERTY(bool, autoRoot, false)
BR_PROPERTY(int, depth, INT_MAX)
BR_PROPERTY(bool, depthFirst, false)
BR_PROPERTY(int, images, INT_MAX)
BR_PROPERTY(bool, json, false)
BR_PROPERTY(int, timeLimit, INT_MAX)
QTime elapsed;
TemplateList templates;
void crawl(QFileInfo url, int currentDepth = 0)
{
if ((templates.size() >= images) || (currentDepth >= depth) || (elapsed.elapsed()/1000 >= timeLimit))
return;
if (url.filePath().startsWith("file://"))
url = QFileInfo(url.filePath().mid(7));
if (url.isDir()) {
const QDir dir(url.absoluteFilePath());
const QFileInfoList files = dir.entryInfoList(QDir::Files);
const QFileInfoList subdirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QFileInfo &first, depthFirst ? subdirs : files)
crawl(first, currentDepth + 1);
foreach (const QFileInfo &second, depthFirst ? files : subdirs)
crawl(second, currentDepth + 1);
} else if (url.isFile()) {
const QString suffix = url.suffix();
if ((suffix == "bmp") || (suffix == "jpg") || (suffix == "jpeg") || (suffix == "png") || (suffix == "tiff")) {
File f;
if (json) f.set("URL", "file://"+url.canonicalFilePath());
else f.name = "file://"+url.canonicalFilePath();
templates.append(f);
}
}
}
void init()
{
elapsed.start();
const QString root = file.name.mid(0, file.name.size()-6); // Remove .crawl suffix";
if (!root.isEmpty()) {
crawl(root);
} else {
if (autoRoot) {
foreach (const QString &path, QStandardPaths::standardLocations(QStandardPaths::HomeLocation))
crawl(path);
} else {
QFile file;
file.open(stdin, QFile::ReadOnly);
while (!file.atEnd()) {
const QString url = QString::fromLocal8Bit(file.readLine()).simplified();
if (!url.isEmpty())
crawl(url);
}
}
}
}
TemplateList readBlock(bool *done)
{
*done = true;
return templates;
}
void write(const Template &)
{
qFatal("Not supported");
}
};
BR_REGISTER(Gallery, crawlGallery)
/*!
* \ingroup galleries
* \brief Treats the gallery as a br::Format.
* \author Josh Klontz \cite jklontz
*/
class DefaultGallery : public Gallery
{
Q_OBJECT
TemplateList readBlock(bool *done)
{
*done = true;
return TemplateList() << file;
}
void write(const Template &t)
{
QScopedPointer<Format> format(Factory<Format>::make(file));
format->write(t);
}
qint64 totalSize()
{
return 1;
}
};
BR_REGISTER(Gallery, DefaultGallery)
/*!
* \ingroup galleries
* \brief Combine all templates into one large matrix and process it as a br::Format
* \author Josh Klontz \cite jklontz
*/
class matrixGallery : public Gallery
{
Q_OBJECT
Q_PROPERTY(const QString extension READ get_extension WRITE set_extension RESET reset_extension STORED false)
BR_PROPERTY(QString, extension, "mtx")
TemplateList templates;
~matrixGallery()
{
if (templates.isEmpty())
return;
QScopedPointer<Format> format(Factory<Format>::make(getFormat()));
format->write(Template(file, OpenCVUtils::toMat(templates.data())));
}
File getFormat() const
{
return file.name.left(file.name.size() - file.suffix().size()) + extension;
}
TemplateList readBlock(bool *done)
{
*done = true;
return TemplateList() << getFormat();
}
void write(const Template &t)
{
templates.append(t);
}
};
BR_REGISTER(Gallery, matrixGallery)
/*!
* \ingroup initializers
* \brief Initialization support for memGallery.
* \author Josh Klontz \cite jklontz
*/
class MemoryGalleries : public Initializer
{
Q_OBJECT
void initialize() const {}
void finalize() const
{
galleries.clear();
}
public:
static QHash<File, TemplateList> galleries; /*!< TODO */
};
QHash<File, TemplateList> MemoryGalleries::galleries;
BR_REGISTER(Initializer, MemoryGalleries)
/*!
* \ingroup galleries
* \brief A gallery held in memory.
* \author Josh Klontz \cite jklontz
*/
class memGallery : public Gallery
{
Q_OBJECT
int block;
qint64 gallerySize;
void init()
{
block = 0;
File galleryFile = file.name.mid(0, file.name.size()-4);
if ((galleryFile.suffix() == "gal") && galleryFile.exists() && !MemoryGalleries::galleries.contains(file)) {
QSharedPointer<Gallery> gallery(Factory<Gallery>::make(galleryFile));
MemoryGalleries::galleries[file] = gallery->read();
gallerySize = MemoryGalleries::galleries[file].size();
}
}
TemplateList readBlock(bool *done)
{
TemplateList templates = MemoryGalleries::galleries[file].mid(block*readBlockSize, readBlockSize);
for (qint64 i = 0; i < templates.size();i++) {
templates[i].file.set("progress", i + block * readBlockSize);
}
*done = (templates.size() < readBlockSize);
block = *done ? 0 : block+1;
return templates;
}
void write(const Template &t)
{
MemoryGalleries::galleries[file].append(t);
}
qint64 totalSize()
{
return gallerySize;
}
qint64 position()
{
return block * readBlockSize;
}
};
BR_REGISTER(Gallery, memGallery)
FileList FileList::fromGallery(const File &rFile, bool cache)
{
File file = rFile;
file.remove("append");
File targetMeta = file;
targetMeta.name = targetMeta.path() + targetMeta.baseName() + "_meta" + targetMeta.hash() + ".mem";
FileList fileData;
// Did we already read the data?
if (MemoryGalleries::galleries.contains(targetMeta))
{
return MemoryGalleries::galleries[targetMeta].files();
}
TemplateList templates;
// OK we read the data in some form, does the gallery type containing matrices?
if ((QStringList() << "gal" << "mem" << "template" << "ut").contains(file.suffix())) {
// Retrieve it block by block, dropping matrices from read templates.
QScopedPointer<Gallery> gallery(Gallery::make(file));
gallery->set_readBlockSize(10);
bool done = false;
while (!done)
{
TemplateList tList = gallery->readBlock(&done);
for (int i=0; i < tList.size();i++)
{
tList[i].clear();
templates.append(tList[i].file);
}
}
}
else {
// this is a gallery format that doesn't include matrices, so we can just read it
QScopedPointer<Gallery> gallery(Gallery::make(file));
templates= gallery->read();
}
if (cache)
{
QScopedPointer<Gallery> memOutput(Gallery::make(targetMeta));
memOutput->writeBlock(templates);
}
fileData = templates.files();
return fileData;
}
/*!
* \ingroup galleries
* \brief Treats each line as a file.
* \author Josh Klontz \cite jklontz
*
* Columns should be comma separated with first row containing headers.
* The first column in the file should be the path to the file to enroll.
* Other columns will be treated as file metadata.
*
* \see txtGallery
*/
class csvGallery : public FileGallery
{
Q_OBJECT
Q_PROPERTY(int fileIndex READ get_fileIndex WRITE set_fileIndex RESET reset_fileIndex)
BR_PROPERTY(int, fileIndex, 0)
FileList files;
QStringList headers;
~csvGallery()
{
f.close();
if (files.isEmpty()) return;
QMap<QString,QVariant> samples;
foreach (const File &file, files)
foreach (const QString &key, file.localKeys())
if (!samples.contains(key))
samples.insert(key, file.value(key));
// Don't create columns in the CSV for these special fields
samples.remove("Points");
samples.remove("Rects");
QStringList lines;
lines.reserve(files.size()+1);
QMap<QString, int> columnCounts;
{ // Make header
QStringList words;
words.append("File");
foreach (const QString &key, samples.keys()) {
int count = 0;
words.append(getCSVElement(key, samples[key], true, count));
columnCounts.insert(key, count);
}
lines.append(words.join(","));
}
// Make table
foreach (const File &file, files) {
QStringList words;
words.append(file.name);
foreach (const QString &key, samples.keys()) {
int count = columnCounts[key];
words.append(getCSVElement(key, file.value(key), false, count));
}
lines.append(words.join(","));
}
QtUtils::writeFile(file, lines);
}
TemplateList readBlock(bool *done)
{
readOpen();
*done = false;
TemplateList templates;
if (!file.exists()) {
*done = true;
return templates;
}
QRegExp regexp("\\s*,\\s*");
if (f.pos() == 0)
{
// read a line
QByteArray lineBytes = f.readLine();
QString line = QString::fromLocal8Bit(lineBytes).trimmed();
headers = line.split(regexp);
}
for (qint64 i = 0; i < this->readBlockSize && !f.atEnd(); i++){
QByteArray lineBytes = f.readLine();
QString line = QString::fromLocal8Bit(lineBytes).trimmed();
QStringList words = line.split(regexp);
if (words.size() != headers.size()) continue;
File fi;
for (int j=0; j<words.size(); j++) {
if (j == 0) fi.name = words[j];
else fi.set(headers[j], words[j]);
}
templates.append(fi);
templates.last().file.set("progress", f.pos());
}
*done = f.atEnd();
return templates;
}
void write(const Template &t)
{
files.append(t.file);
}
static QString getCSVElement(const QString &key, const QVariant &value, bool header, int & columnCount)
{
if (header)
columnCount = 1;
if (value.canConvert<QString>()) {
if (header) return key;
else {
if (columnCount != 1)
qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
return value.value<QString>();
}
} else if (value.canConvert<QPointF>()) {
const QPointF point = value.value<QPointF>();
if (header) {
columnCount = 2;
return key+"_X,"+key+"_Y";
}
else {
if (columnCount != 2)
qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
return QString::number(point.x())+","+QString::number(point.y());
}
} else if (value.canConvert<QRectF>()) {
const QRectF rect = value.value<QRectF>();
if (header) {
columnCount = 4;
return key+"_X,"+key+"_Y,"+key+"_Width,"+key+"_Height";
}
else {
if (columnCount != 4)
qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
return QString::number(rect.x())+","+QString::number(rect.y())+","+QString::number(rect.width())+","+QString::number(rect.height());
}
} else {
if (header) return key;
else {
QString output = QString::number(std::numeric_limits<float>::quiet_NaN());
for (int i = 1; i < columnCount; i++)
output += "," + QString::number(std::numeric_limits<float>::quiet_NaN());
return output;
}
}
}
};
BR_REGISTER(Gallery, csvGallery)
/*!
* \ingroup galleries
* \brief Treats each line as a file.
* \author Josh Klontz \cite jklontz
*
* The entire line is treated as the file path. An optional label may be specified using a space ' ' separator:
*
\verbatim
<FILE>
<FILE>
...
<FILE>
\endverbatim
* or
\verbatim
<FILE> <LABEL>
<FILE> <LABEL>
...
<FILE> <LABEL>
\endverbatim
* \see csvGallery
*/
class txtGallery : public FileGallery
{
Q_OBJECT
Q_PROPERTY(QString label READ get_label WRITE set_label RESET reset_label STORED false)
BR_PROPERTY(QString, label, "")
TemplateList readBlock(bool *done)
{
readOpen();
*done = false;
if (f.atEnd())
f.seek(0);
TemplateList templates;
for (qint64 i = 0; i < readBlockSize; i++)
{
QByteArray lineBytes = f.readLine();
QString line = QString::fromLocal8Bit(lineBytes).trimmed();
if (!line.isEmpty()){
int splitIndex = line.lastIndexOf(' ');
if (splitIndex == -1) templates.append(File(line));
else templates.append(File(line.mid(0, splitIndex), line.mid(splitIndex+1)));
templates.last().file.set("progress", this->position());
}
if (f.atEnd()) {
*done=true;
break;
}
}
return templates;
}
void write(const Template &t)
{
writeOpen();
QString line = t.file.name;
if (!label.isEmpty())
line += " " + t.file.get<QString>(label);
f.write((line+"\n").toLocal8Bit() );
}
};
BR_REGISTER(Gallery, txtGallery)
/*!
* \ingroup galleries
* \brief Treats each line as a call to File::flat()
* \author Josh Klontz \cite jklontz
*/
class flatGallery : public FileGallery
{
Q_OBJECT
TemplateList readBlock(bool *done)
{
readOpen();
*done = false;
if (f.atEnd())
f.seek(0);
TemplateList templates;
for (qint64 i = 0; i < readBlockSize; i++)
{
QByteArray line = f.readLine();
if (!line.isEmpty()) {
templates.append(File(QString::fromLocal8Bit(line).trimmed()));
templates.last().file.set("progress", this->position());
}
if (f.atEnd()) {
*done=true;
break;
}
}
return templates;
}
void write(const Template &t)
{
writeOpen();
f.write((t.file.flat()+"\n").toLocal8Bit() );
}
};
BR_REGISTER(Gallery, flatGallery)
/*!
* \ingroup galleries
* \brief A \ref sigset input.
* \author Josh Klontz \cite jklontz
*/
class xmlGallery : public FileGallery
{
Q_OBJECT
Q_PROPERTY(bool ignoreMetadata READ get_ignoreMetadata WRITE set_ignoreMetadata RESET reset_ignoreMetadata STORED false)
BR_PROPERTY(bool, ignoreMetadata, false)
FileList files;
QXmlStreamReader reader;
QString currentSignatureName;
bool signatureActive;
~xmlGallery()
{
f.close();
if (!files.isEmpty())
BEE::writeSigset(file, files, ignoreMetadata);
}
TemplateList readBlock(bool *done)
{
if (readOpen())
reader.setDevice(&f);
if (reader.atEnd())
f.seek(0);
TemplateList templates;
qint64 count = 0;
while (!reader.atEnd())
{
// if an identity is active we try to read presentations
if (signatureActive)
{
while (signatureActive)
{
QXmlStreamReader::TokenType signatureToken = reader.readNext();
// did the signature end?
if (signatureToken == QXmlStreamReader::EndElement && reader.name() == "biometric-signature") {
signatureActive = false;
break;
}
// did we reach the end of the document? Theoretically this shoudln't happen without reaching the end of
if (signatureToken == QXmlStreamReader::EndDocument)
break;
// a presentation!
if (signatureToken == QXmlStreamReader::StartElement && reader.name() == "presentation") {
templates.append(Template(File("",currentSignatureName)));
foreach (const QXmlStreamAttribute &attribute, reader.attributes()) {
// file-name is stored directly on file, not as a key/value pair
if (attribute.name() == "file-name")
templates.last().file.name = attribute.value().toString();
// other values are directly set as metadata
else if (!ignoreMetadata) templates.last().file.set(attribute.name().toString(), attribute.value().toString());
}
// a presentation can have bounding boxes as child elements
QList<QRectF> rects = templates.last().file.rects();
while (true)
{
QXmlStreamReader::TokenType pToken = reader.readNext();
if (pToken == QXmlStreamReader::EndElement && reader.name() == "presentation")
break;
if (pToken == QXmlStreamReader::StartElement)
{
if (reader.attributes().hasAttribute("x")
&& reader.attributes().hasAttribute("y")
&& reader.attributes().hasAttribute("width")
&& reader.attributes().hasAttribute("height") )
{
// get bounding box properties as attributes, just going to assume this all works
qreal x = reader.attributes().value("x").string()->toDouble();
qreal y = reader.attributes().value("y").string()->toDouble();
qreal width = reader.attributes().value("width").string()->toDouble();
qreal height = reader.attributes().value("height").string()->toDouble();
rects += QRectF(x, y, width, height);
}
}
}
templates.last().file.setRects(rects);
templates.last().file.set("progress", f.pos());
// we read another complete template
count++;
}
}
}
// otherwise, keep reading elements until the next identity is reacehed
else
{
QXmlStreamReader::TokenType token = reader.readNext();
// end of file?
if (token == QXmlStreamReader::EndDocument)
break;
// we are only interested in new elements
if (token != QXmlStreamReader::StartElement)
continue;
QStringRef elName = reader.name();
// biometric-signature-set is the root element
if (elName == "biometric-signature-set")
continue;
// biometric-signature -- an identity
if (elName == "biometric-signature")
{
// read the name associated with the current signature
if (!reader.attributes().hasAttribute("name"))
{
qDebug() << "Biometric signature missing name";
continue;
}
currentSignatureName = reader.attributes().value("name").toString();
signatureActive = true;
// If we've already read enough templates for this block, then break here.
// We wait untill the start of the next signature to be sure that done should
// actually be false (i.e. there are actually items left in this file)
if (count >= this->readBlockSize) {
*done = false;
return templates;
}
}
}
}
*done = true;
return templates;
}
void write(const Template &t)
{
files.append(t.file);
}
void init()
{
FileGallery::init();
}
};
BR_REGISTER(Gallery, xmlGallery)
/*!
* \ingroup galleries
* \brief Treat the file as a single binary template.
* \author Josh Klontz \cite jklontz
*/
class templateGallery : public Gallery
{
Q_OBJECT
TemplateList readBlock(bool *done)
{
*done = true;
QByteArray data;
QtUtils::readFile(file.name.left(file.name.size()-QString(".template").size()), data);
return TemplateList() << Template(file, cv::Mat(1, data.size(), CV_8UC1, data.data()).clone());
}
void write(const Template &t)
{
(void) t;
qFatal("No supported.");
}
void init()
{
//
}
};
BR_REGISTER(Gallery, templateGallery)
/*!
* \ingroup galleries
* \brief Database input.
* \author Josh Klontz \cite jklontz
*/
class dbGallery : public Gallery
{
Q_OBJECT
TemplateList readBlock(bool *done)
{
TemplateList templates;
br::File import = file.get<QString>("import", "");
QString query = file.get<QString>("query");
QString subset = file.get<QString>("subset", "");
#ifndef BR_EMBEDDED
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(file);
if (!db.open()) qFatal("Failed to open SQLite database %s.", qPrintable(file.name));
if (!import.isNull()) {
qDebug("Parsing %s", qPrintable(import.name));
QStringList lines = QtUtils::readLines(import);
QList<QStringList> cells; cells.reserve(lines.size());
const QRegExp re("\\s*,\\s*");
foreach (const QString &line, lines) {
cells.append(line.split(re));
if (cells.last().size() != cells.first().size()) qFatal("Column count mismatch.");
}
QStringList columns, qMarks;
QList<QVariantList> variantLists;
for (int i=0; i<cells[0].size(); i++) {
bool isNumeric;
cells[1][i].toInt(&isNumeric);
columns.append(cells[0][i] + (isNumeric ? " INTEGER" : " STRING"));
qMarks.append("?");
QVariantList variantList; variantList.reserve(lines.size()-1);
for (int j=1; j<lines.size(); j++) {
if (isNumeric) variantList << cells[j][i].toInt();
else variantList << cells[j][i];
}
variantLists.append(variantList);
}
const QString &table = import.baseName();
qDebug("Creating table %s", qPrintable(table));
QSqlQuery q(db);
if (!q.exec("CREATE TABLE " + table + " (" + columns.join(", ") + ");"))
qFatal("%s.", qPrintable(q.lastError().text()));
if (!q.prepare("insert into " + table + " values (" + qMarks.join(", ") + ")"))
qFatal("%s.", qPrintable(q.lastError().text()));
foreach (const QVariantList &vl, variantLists)
q.addBindValue(vl);
if (!q.execBatch()) qFatal("%s.", qPrintable(q.lastError().text()));
}
QSqlQuery q(db);
if (query.startsWith('\'') && query.endsWith('\''))
query = query.mid(1, query.size()-2);
if (!q.exec(query))
qFatal("%s.", qPrintable(q.lastError().text()));
if ((q.record().count() == 0) || (q.record().count() > 3))
qFatal("Query record expected one to three fields, got %d.", q.record().count());
const bool hasMetadata = (q.record().count() >= 2);
const bool hasFilter = (q.record().count() >= 3);
QString labelName = "Label";
if (q.record().count() >= 2)
labelName = q.record().fieldName(1);
// subset = seed:subjectMaxSize:numSubjects:subjectMinSize or
// subset = seed:{Metadata,...,Metadata}:numSubjects
int seed = 0, subjectMaxSize = std::numeric_limits<int>::max(), numSubjects = std::numeric_limits<int>::max(), subjectMinSize = 0;
QList<QRegExp> metadataFields;
if (!subset.isEmpty()) {
const QStringList &words = subset.split(":");
QtUtils::checkArgsSize("Input", words, 2, 4);
if (words[0] == "train") seed = 0;
else if (words[0] == "test" ) seed = 1;
else seed = QtUtils::toInt(words[0]);
if (words[1].startsWith('{') && words[1].endsWith('}')) {
foreach (const QString ®exp, words[1].mid(1, words[1].size()-2).split(","))
metadataFields.append(QRegExp(regexp));
subjectMaxSize = metadataFields.size();
} else {
subjectMaxSize = QtUtils::toInt(words[1]);
}
numSubjects = words.size() >= 3 ? QtUtils::toInt(words[2]) : std::numeric_limits<int>::max();
subjectMinSize = words.size() >= 4 ? QtUtils::toInt(words[3]) : subjectMaxSize;
}
srand(seed);
typedef QPair<QString,QString> Entry; // QPair<File,Metadata>
QHash<QString, QList<Entry> > entries; // QHash<Label, QList<Entry> >
while (q.next()) {
if (hasFilter && (seed >= 0) && (qHash(q.value(2).toString()) % 2 != (uint)seed % 2)) continue; // Ensures training and testing filters don't overlap
if (metadataFields.isEmpty())
entries[hasMetadata ? q.value(1).toString() : ""].append(QPair<QString,QString>(q.value(0).toString(), hasFilter ? q.value(2).toString() : ""));
else
entries[hasFilter ? q.value(2).toString() : ""].append(QPair<QString,QString>(q.value(0).toString(), hasMetadata ? q.value(1).toString() : ""));
}
QStringList labels = entries.keys();
qSort(labels);
if (hasFilter && ((labels.size() > numSubjects) || (numSubjects == std::numeric_limits<int>::max())))
std::random_shuffle(labels.begin(), labels.end());
foreach (const QString &label, labels) {
QList<Entry> entryList = entries[label];
if ((entryList.size() >= subjectMinSize) && (numSubjects > 0)) {
if (!metadataFields.isEmpty()) {
QList<Entry> subEntryList;
foreach (const QRegExp &metadata, metadataFields) {
for (int i=0; i<entryList.size(); i++) {
if (metadata.exactMatch(entryList[i].second)) {
subEntryList.append(entryList.takeAt(i));
break;
}
}
}
if (subEntryList.size() == metadataFields.size())
entryList = subEntryList;
else
continue;
}
if (entryList.size() > subjectMaxSize)
std::random_shuffle(entryList.begin(), entryList.end());
foreach (const Entry &entry, entryList.mid(0, subjectMaxSize)) {
templates.append(File(entry.first));
templates.last().file.set(labelName, label);
}
numSubjects--;
}
}
db.close();
#endif // BR_EMBEDDED
*done = true;
return templates;
}
void write(const Template &t)
{
(void) t;
qFatal("Not supported.");
}
void init()
{
//
}
};
BR_REGISTER(Gallery, dbGallery)
/*!
* \ingroup inputs
* \brief Input from a google image search.
* \author Josh Klontz \cite jklontz
*/
class googleGallery : public Gallery
{
Q_OBJECT
TemplateList readBlock(bool *done)
{
TemplateList templates;
static const QString search = "http://images.google.com/images?q=%1&start=%2";
QString query = file.name.left(file.name.size()-7); // remove ".google"
#ifndef BR_EMBEDDED
QNetworkAccessManager networkAccessManager;
for (int i=0; i<100; i+=20) { // Retrieve 100 images
QNetworkRequest request(search.arg(query, QString::number(i)));
QNetworkReply *reply = networkAccessManager.get(request);
while (!reply->isFinished())
QThread::yieldCurrentThread();
QString data(reply->readAll());
delete reply;
QStringList words = data.split("imgurl=");
words.takeFirst(); // Remove header
foreach (const QString &word, words) {
QString url = word.left(word.indexOf("&"));
url = url.replace("%2520","%20");
int junk = url.indexOf('%', url.lastIndexOf('.'));
if (junk != -1) url = url.left(junk);
templates.append(File(url,query));
}
}
#endif // BR_EMBEDDED
*done = true;
return templates;
}
void write(const Template &)
{
qFatal("Not supported.");
}
};
BR_REGISTER(Gallery, googleGallery)
/*!
* \ingroup galleries
* \brief Print template statistics.
* \author Josh Klontz \cite jklontz
*/
class statGallery : public Gallery
{
Q_OBJECT
QSet<QString> subjects;
QList<int> bytes;
~statGallery()
{
int emptyTemplates = 0;
for (int i=bytes.size()-1; i>=0; i--)
if (bytes[i] == 0) {
bytes.removeAt(i);
emptyTemplates++;
}
double bytesMean, bytesStdDev;
Common::MeanStdDev(bytes, &bytesMean, &bytesStdDev);
printf("Subjects: %d\nEmpty Templates: %d/%d\nBytes/Template: %.4g +/- %.4g\n",
subjects.size(), emptyTemplates, emptyTemplates+bytes.size(), bytesMean, bytesStdDev);
}
TemplateList readBlock(bool *done)
{
*done = true;
return TemplateList() << file;
}
void write(const Template &t)
{
subjects.insert(t.file.get<QString>("Label"));
bytes.append(t.bytes());
}
};
BR_REGISTER(Gallery, statGallery)
/*!
* \ingroup galleries
* \brief Implements the FDDB detection format.
* \author Josh Klontz \cite jklontz
*
* http://vis-www.cs.umass.edu/fddb/README.txt
*/
class FDDBGallery : public Gallery
{
Q_OBJECT
TemplateList readBlock(bool *done)
{
*done = true;
QStringList lines = QtUtils::readLines(file);
TemplateList templates;
while (!lines.empty()) {
const QString fileName = lines.takeFirst();
int numDetects = lines.takeFirst().toInt();
for (int i=0; i<numDetects; i++) {
const QStringList detect = lines.takeFirst().split(' ');
Template t(fileName);
QList<QVariant> faceList; //to be consistent with slidingWindow
if (detect.size() == 5) { //rectangle
faceList.append(QRectF(detect[0].toFloat(), detect[1].toFloat(), detect[2].toFloat(), detect[3].toFloat()));
t.file.set("Confidence", detect[4].toFloat());
} else if (detect.size() == 6) { //ellipse
float x = detect[3].toFloat(),
y = detect[4].toFloat(),
radius = detect[1].toFloat();
faceList.append(QRectF(x - radius,y - radius,radius * 2.0, radius * 2.0));
t.file.set("Confidence", detect[5].toFloat());
} else {
qFatal("Unknown FDDB annotation format.");
}
t.file.set("Face", faceList);
t.file.set("Label",QString("face"));
templates.append(t);
}
}
return templates;
}
void write(const Template &t)
{
(void) t;
qFatal("Not implemented.");
}
void init()
{
//
}
};
BR_REGISTER(Gallery, FDDBGallery)
/*!
* \ingroup galleries
* \brief Text format for associating anonymous landmarks with images.
* \author Josh Klontz \cite jklontz
*
* \code
* file_name:x1,y1,x2,y2,...,xn,yn
* file_name:x1,y1,x2,y2,...,xn,yn
* ...
* file_name:x1,y1,x2,y2,...,xn,yn
* \endcode
*/
class landmarksGallery : public Gallery
{
Q_OBJECT
TemplateList readBlock(bool *done)
{
*done = true;
TemplateList templates;
foreach (const QString &line, QtUtils::readLines(file)) {
const QStringList words = line.split(':');
if (words.size() != 2) qFatal("Expected exactly one ':' in: %s.", qPrintable(line));
File file(words[0]);
const QList<float> vals = QtUtils::toFloats(words[1].split(','));
if (vals.size() % 2 != 0) qFatal("Expected an even number of comma-separated values.");
QList<QPointF> points; points.reserve(vals.size()/2);
for (int i=0; i<vals.size(); i+=2)
points.append(QPointF(vals[i], vals[i+1]));
file.setPoints(points);
templates.append(file);
}
return templates;
}
void write(const Template &t)
{
(void) t;
qFatal("Not implemented.");
}
void init()
{
//
}
};
BR_REGISTER(Gallery, landmarksGallery)
#ifdef CVMATIO
using namespace cv;
class vbbGallery : public Gallery
{
Q_OBJECT
void init()
{
MatlabIO matio;
QString filename = (Globals->path.isEmpty() ? "" : Globals->path + "/") + file.name;
bool ok = matio.open(filename.toStdString(), "r");
if (!ok) qFatal("Couldn't open the vbb file");
vector<MatlabIOContainer> variables;
variables = matio.read();
matio.close();
double vers = variables[1].data<Mat>().at<double>(0,0);
if (vers != 1.4) qFatal("This is an old vbb version, we don't mess with that.");
A = variables[0].data<vector<vector<MatlabIOContainer> > >().at(0);
objLists = A.at(1).data<vector<MatlabIOContainer> >();
// start at the first frame (duh!)
currFrame = 0;
}
TemplateList readBlock(bool *done)
{
*done = false;
Template rects(file);
if (objLists[currFrame].typeEquals<vector<vector<MatlabIOContainer> > >()) {
vector<vector<MatlabIOContainer> > bbs = objLists[currFrame].data<vector<vector<MatlabIOContainer> > >();
for (unsigned int i=0; i<bbs.size(); i++) {
vector<MatlabIOContainer> bb = bbs[i];
Mat pos = bb[1].data<Mat>();
double left = pos.at<double>(0,0);
double top = pos.at<double>(0,1);
double width = pos.at<double>(0,2);
double height = pos.at<double>(0,3);
rects.file.appendRect(QRectF(left, top, width, height));
}
}
TemplateList tl;
tl.append(rects);
if (++currFrame == (int)objLists.size()) *done = true;
return tl;
}
void write(const Template &t)
{
(void)t; qFatal("Not implemented");
}
private:
// this holds a bunch of stuff, maybe we'll use it all later
vector<MatlabIOContainer> A;
// this, a field in A, holds bounding boxes for each frame
vector<MatlabIOContainer> objLists;
int currFrame;
};
BR_REGISTER(Gallery, vbbGallery)
#endif
// Read a video frame by frame using cv::VideoCapture
class videoGallery : public Gallery
{
Q_OBJECT
public:
qint64 idx;
~videoGallery()
{
video.release();
}
static QMutex openLock;
virtual void deferredInit()
{
bool status = video.open(QtUtils::getAbsolutePath(file.name).toStdString());
if (!status)
qFatal("Failed to open file %s with path %s", qPrintable(file.name), qPrintable(QtUtils::getAbsolutePath(file.name)));
}
TemplateList readBlock(bool *done)
{
if (!video.isOpened()) {
// opening videos appears to not be thread safe on windows
QMutexLocker lock(&openLock);
deferredInit();
idx = 0;
}
Template output;
output.file = file;
output.m() = cv::Mat();
cv::Mat temp;
bool res = video.read(temp);
if (!res) {
// The video capture broke, return an empty list.
output.m() = cv::Mat();
video.release();
*done = true;
return TemplateList();
}
// This clone is critical, if we don't do it then the output matrix will
// be an alias of an internal buffer of the video source, leading to various
// problems later.
output.m() = temp.clone();
output.file.set("progress", idx);
idx++;
TemplateList rVal;
rVal.append(temp);
*done = false;
return rVal;
}
void write(const Template &t)
{
(void)t; qFatal("Not implemented");
}
protected:
cv::VideoCapture video;
};
BR_REGISTER(Gallery,videoGallery)
QMutex videoGallery::openLock;
class aviGallery : public videoGallery
{
Q_OBJECT
};
BR_REGISTER(Gallery, aviGallery)
class wmvGallery : public videoGallery
{
Q_OBJECT
};
BR_REGISTER(Gallery, wmvGallery)
// Mostly the same as videoGallery, but we open the VideoCapture with an integer index
// rather than file name/web address
class webcamGallery : public videoGallery
{
public:
Q_OBJECT
void deferredInit()
{
bool intOK = false;
int anInt = file.baseName().toInt(&intOK);
if (!intOK)
qFatal("Expected integer basename, got %s", qPrintable(file.baseName()));
bool rc = video.open(anInt);
if (!rc)
qFatal("Failed to open webcam with index: %s", qPrintable(file.baseName()));
}
};
BR_REGISTER(Gallery,webcamGallery)
class seqGallery : public Gallery
{
public:
Q_OBJECT
bool open()
{
seqFile.open(QtUtils::getAbsolutePath(file.name).toStdString().c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if (!isOpen()) {
qDebug("Failed to open file %s for reading", qPrintable(file.name));
return false;
}
int headSize = 1024;
// start at end of file to get full size
int fileSize = seqFile.tellg();
if (fileSize < headSize) {
qDebug("No header in seq file");
return false;
}
// first 4 bytes store 0xEDFE, next 24 store 'Norpix seq '
char firstFour[4];
seqFile.seekg(0, std::ios::beg);
seqFile.read(firstFour, 4);
char nextTwentyFour[24];
readText(24, nextTwentyFour);
if (firstFour[0] != (char)0xED || firstFour[1] != (char)0xFE || strncmp(nextTwentyFour, "Norpix seq", 10) != 0) {
qDebug("Invalid header in seq file");
return false;
}
// next 8 bytes for version (skipped below) and header size (1024), then 512 for descr
seqFile.seekg(4, std::ios::cur);
int hSize = readInt();
if (hSize != headSize) {
qDebug("Invalid header size");
return false;
}
char desc[512];
readText(512, desc);
file.set("Description", QString(desc));
width = readInt();
height = readInt();
// get # channels from bit depth
numChan = readInt()/8;
int imageBitDepthReal = readInt();
if (imageBitDepthReal != 8) {
qDebug("Invalid bit depth");
return false;
}
// the size of just the image part of a raw img
imgSizeBytes = readInt();
int imgFormatInt = readInt();
if (imgFormatInt == 100 || imgFormatInt == 200 || imgFormatInt == 101) {
imgFormat = "raw";
} else if (imgFormatInt == 102 || imgFormatInt == 201 || imgFormatInt == 103 ||
imgFormatInt == 1 || imgFormatInt == 2) {
imgFormat = "compressed";
} else {
qFatal("unsupported image format");
}
numFrames = readInt();
// skip empty int
seqFile.seekg(4, std::ios::cur);
// the size of a full raw file, with extra crap after img data
trueImgSizeBytes = readInt();
// gather all the frame positions in an array
seekPos.reserve(numFrames);
// start at end of header
seekPos.append(headSize);
// extra 8 bytes at end of img
int extra = 8;
for (int i=1; i<numFrames; i++) {
int s;
// compressed images have different sizes
// the first byte at the beginning of the file
// says how big the current img is
if (imgFormat == "compressed") {
int lastPos = seekPos[i-1];
seqFile.seekg(lastPos, std::ios::beg);
int currSize = readInt();
s = lastPos + currSize + extra;
// but there might be 16 extra bytes instead of 8...
if (i == 1) {
seqFile.seekg(s, std::ios::beg);
char zero;
seqFile.read(&zero, 1);
if (zero == 0) {
s += 8;
extra += 8;
}
}
}
// raw images are all the same size
else {
s = headSize + (i*trueImgSizeBytes);
}
seekPos.enqueue(s);
}
#ifdef CVMATIO
if (basis.file.contains("vbb")) {
QString vbb = basis.file.get<QString>("vbb");
annotations = TemplateList::fromGallery(File(vbb));
}
#else
qWarning("cvmatio not installed, bounding boxes will not be available. Add -DBR_WITH_CVMATIO cmake flag to install.");
#endif
return true;
}
bool isOpen()
{
return seqFile.is_open();
}
void close()
{
seqFile.close();
}
TemplateList readBlock(bool *done)
{
if (!isOpen()) {
if (!open())
qFatal("Failed to open file %s for reading", qPrintable(file.name));
else
idx = 0;
}
// if we've reached the last frame, we're done
if (seekPos.size() == 0) {
*done = true;
return TemplateList();
}
seqFile.seekg(seekPos.dequeue(), std::ios::beg);
cv::Mat temp;
// let imdecode do all the work to decode the compressed img
if (imgFormat == "compressed") {
int imgSize = readInt() - 4;
std::vector<char> imgBuf(imgSize);
seqFile.read(&imgBuf[0], imgSize);
// flags < 0 means load image as-is (keep color info if available)
cv::imdecode(imgBuf, -1, &temp);
}
// raw images can be loaded straight into a Mat
else {
char *imgBuf = new char[imgSizeBytes];
seqFile.read(imgBuf, imgSizeBytes);
int type = (numChan == 1 ? CV_8UC1 : CV_8UC3);
temp = cv::Mat(height, width, type, imgBuf);
}
Template output;
output.file = file;
if (!annotations.empty()) {
output.file.setRects(annotations.first().file.rects());
annotations.removeFirst();
}
output.m() = temp;
output.file.set("position",idx);
idx++;
*done = false;
TemplateList rVal;
rVal.append(output);
return rVal;
}
void write(const Template &t)
{
(void) t;
qFatal("Not implemented.");
}
private:
qint64 idx;
int readInt()
{
int num;
seqFile.read((char*)&num, 4);
return num;
}
// apparently the text in seq files is 16 bit characters (UTF-16?)
// since we don't really need the last byte, snad since it gets interpreted as
// a terminating char, let's just grab the first byte for storage
void readText(int bytes, char *buffer)
{
seqFile.read(buffer, bytes);
for (int i=0; i<bytes; i+=2) {
buffer[i/2] = buffer[i];
}
buffer[bytes/2] = '\0';
}
protected:
std::ifstream seqFile;
QQueue<int> seekPos;
int width, height, numChan, imgSizeBytes, trueImgSizeBytes, numFrames;
QString imgFormat;
TemplateList annotations;
};
BR_REGISTER(Gallery, seqGallery)
void FileGallery::init()
{
f.setFileName(file);
Gallery::init();
}
void FileGallery::writeOpen()
{
if (!f.isOpen() ) {
QtUtils::touchDir(f);
if (!f.open(QFile::WriteOnly))
qFatal("Failed to open %s for writing.", qPrintable(file));
}
}
bool FileGallery::readOpen()
{
if (!f.isOpen() ) {
if (!f.exists() ) {
qFatal("File %s does not exist.", qPrintable(file));
}
if (!f.open(QFile::ReadOnly))
qFatal("Failed to open %s for reading.", qPrintable(file));
return true;
}
return false;
}
qint64 FileGallery::totalSize()
{
readOpen();
return f.size();
}
} // namespace br
#include "gallery.moc"