Commit 244a6b2132ba9fe34253855a63f371bffd65851d

Authored by Josh Klontz
2 parents 2b2e5b4a 4aff6f12

Merge pull request #275 from biometrics/ut_refactor

Ut refactor
openbr/plugins/gallery.cpp
@@ -232,28 +232,24 @@ class utGallery : public BinaryGallery @@ -232,28 +232,24 @@ class utGallery : public BinaryGallery
232 Template t; 232 Template t;
233 br_universal_template ut; 233 br_universal_template ut;
234 if (gallery.read((char*)&ut, sizeof(br_universal_template)) == sizeof(br_universal_template)) { 234 if (gallery.read((char*)&ut, sizeof(br_universal_template)) == sizeof(br_universal_template)) {
235 - QByteArray data(ut.size, Qt::Uninitialized); 235 + QByteArray data(ut.urlSize + ut.fvSize, Qt::Uninitialized);
236 char *dst = data.data(); 236 char *dst = data.data();
237 - qint64 bytesNeeded = ut.size; 237 + qint64 bytesNeeded = ut.urlSize + ut.fvSize;
238 while (bytesNeeded > 0) { 238 while (bytesNeeded > 0) {
239 qint64 bytesRead = gallery.read(dst, bytesNeeded); 239 qint64 bytesRead = gallery.read(dst, bytesNeeded);
240 if (bytesRead <= 0) { 240 if (bytesRead <= 0) {
241 qDebug() << gallery.errorString(); 241 qDebug() << gallery.errorString();
242 - qFatal("Unexepected EOF while reading universal template data, needed: %d more of: %d bytes.", int(bytesNeeded), int(ut.size)); 242 + qFatal("Unexepected EOF while reading universal template data, needed: %d more of: %d bytes.", int(bytesNeeded), int(ut.urlSize + ut.fvSize));
243 } 243 }
244 bytesNeeded -= bytesRead; 244 bytesNeeded -= bytesRead;
245 dst += bytesRead; 245 dst += bytesRead;
246 } 246 }
247 247
248 - if (QCryptographicHash::hash(data.mid(ut.urlSize), QCryptographicHash::Md5) != QByteArray((const char*)ut.templateID, 16))  
249 - qFatal("MD5 hash check failure!");  
250 -  
251 t.file.set("ImageID", QVariant(QByteArray((const char*)ut.imageID, 16).toHex())); 248 t.file.set("ImageID", QVariant(QByteArray((const char*)ut.imageID, 16).toHex()));
252 - t.file.set("TemplateID", QVariant(QByteArray((const char*)ut.templateID, 16).toHex()));  
253 t.file.set("AlgorithmID", ut.algorithmID); 249 t.file.set("AlgorithmID", ut.algorithmID);
254 t.file.set("URL", QString(data.data())); 250 t.file.set("URL", QString(data.data()));
255 char *dataStart = data.data() + ut.urlSize; 251 char *dataStart = data.data() + ut.urlSize;
256 - uint32_t dataSize = ut.size - ut.urlSize; 252 + uint32_t dataSize = ut.fvSize;
257 if ((ut.algorithmID <= -1) && (ut.algorithmID >= -3)) { 253 if ((ut.algorithmID <= -1) && (ut.algorithmID >= -3)) {
258 t.file.set("FrontalFace", QRectF(ut.x, ut.y, ut.width, ut.height)); 254 t.file.set("FrontalFace", QRectF(ut.x, ut.y, ut.width, ut.height));
259 uint32_t *rightEyeX = reinterpret_cast<uint32_t*>(dataStart); 255 uint32_t *rightEyeX = reinterpret_cast<uint32_t*>(dataStart);
@@ -267,13 +263,13 @@ class utGallery : public BinaryGallery @@ -267,13 +263,13 @@ class utGallery : public BinaryGallery
267 dataSize -= sizeof(uint32_t)*4; 263 dataSize -= sizeof(uint32_t)*4;
268 t.file.set("First_Eye", QPointF(*rightEyeX, *rightEyeY)); 264 t.file.set("First_Eye", QPointF(*rightEyeX, *rightEyeY));
269 t.file.set("Second_Eye", QPointF(*leftEyeX, *leftEyeY)); 265 t.file.set("Second_Eye", QPointF(*leftEyeX, *leftEyeY));
270 - }  
271 - else { 266 + } else {
272 t.file.set("X", ut.x); 267 t.file.set("X", ut.x);
273 t.file.set("Y", ut.y); 268 t.file.set("Y", ut.y);
274 t.file.set("Width", ut.width); 269 t.file.set("Width", ut.width);
275 t.file.set("Height", ut.height); 270 t.file.set("Height", ut.height);
276 } 271 }
  272 + t.file.set("Label", ut.label);
277 t.append(cv::Mat(1, dataSize, CV_8UC1, dataStart).clone() /* We don't want a shallow copy! */); 273 t.append(cv::Mat(1, dataSize, CV_8UC1, dataStart).clone() /* We don't want a shallow copy! */);
278 } else { 274 } else {
279 if (!gallery.atEnd()) 275 if (!gallery.atEnd())
@@ -317,30 +313,28 @@ class utGallery : public BinaryGallery @@ -317,30 +313,28 @@ class utGallery : public BinaryGallery
317 width = t.file.get<uint32_t>("Width", 0); 313 width = t.file.get<uint32_t>("Width", 0);
318 height = t.file.get<uint32_t>("Height", 0); 314 height = t.file.get<uint32_t>("Height", 0);
319 } 315 }
320 -  
321 - QCryptographicHash templateID(QCryptographicHash::Md5);  
322 - templateID.addData(header);  
323 - if (algorithmID != 0)  
324 - templateID.addData((const char*) t.m().data, t.m().rows * t.m().cols * t.m().elemSize()); 316 + const uint32_t label = t.file.get<uint32_t>("Label", 0);
325 317
326 gallery.write(imageID); 318 gallery.write(imageID);
327 - gallery.write(templateID.result());  
328 - gallery.write((const char*) &algorithmID, sizeof(uint32_t)); 319 + gallery.write((const char*) &algorithmID, sizeof(int32_t));
329 gallery.write((const char*) &x , sizeof(uint32_t)); 320 gallery.write((const char*) &x , sizeof(uint32_t));
330 gallery.write((const char*) &y , sizeof(uint32_t)); 321 gallery.write((const char*) &y , sizeof(uint32_t));
331 gallery.write((const char*) &width , sizeof(uint32_t)); 322 gallery.write((const char*) &width , sizeof(uint32_t));
332 gallery.write((const char*) &height , sizeof(uint32_t)); 323 gallery.write((const char*) &height , sizeof(uint32_t));
  324 + gallery.write((const char*) &label , sizeof(uint32_t));
333 325
334 const uint32_t urlSize = url.size() + 1; 326 const uint32_t urlSize = url.size() + 1;
335 gallery.write((const char*) &urlSize, sizeof(uint32_t)); 327 gallery.write((const char*) &urlSize, sizeof(uint32_t));
336 328
337 - const uint32_t fvSize = t.m().rows * t.m().cols * t.m().elemSize();  
338 - const uint32_t size = urlSize + header.size() + (algorithmID == 0 ? 0 : fvSize);  
339 - gallery.write((const char*) &size, sizeof(uint32_t)); 329 + const uint32_t signatureSize = (algorithmID == 0) ? 0 : t.m().rows * t.m().cols * t.m().elemSize();
  330 + const uint32_t fvSize = header.size() + signatureSize;
  331 + gallery.write((const char*) &fvSize, sizeof(uint32_t));
  332 +
340 gallery.write((const char*) url.data(), urlSize); 333 gallery.write((const char*) url.data(), urlSize);
341 - gallery.write(header);  
342 - if (algorithmID != 0)  
343 - gallery.write((const char*) t.m().data, fvSize); 334 + if (algorithmID != 0) {
  335 + gallery.write(header);
  336 + gallery.write((const char*) t.m().data, signatureSize);
  337 + }
344 } 338 }
345 }; 339 };
346 340
openbr/universal_template.cpp
@@ -8,22 +8,21 @@ @@ -8,22 +8,21 @@
8 8
9 #include "universal_template.h" 9 #include "universal_template.h"
10 10
11 -br_utemplate br_new_utemplate(const int8_t *imageID, int32_t algorithmID, size_t x, size_t y, size_t width, size_t height, const char *url, const char *data, uint32_t dataSize) 11 +br_utemplate br_new_utemplate(const char *imageID, int32_t algorithmID, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t label, const char *url, const char *fv, uint32_t fvSize)
12 { 12 {
13 const uint32_t urlSize = strlen(url) + 1; 13 const uint32_t urlSize = strlen(url) + 1;
14 - const uint32_t size = urlSize + dataSize;  
15 - br_utemplate utemplate = (br_utemplate) malloc(sizeof(br_universal_template) + size); 14 + br_utemplate utemplate = (br_utemplate) malloc(sizeof(br_universal_template) + urlSize + fvSize);
16 memcpy(utemplate->imageID, imageID, 16); 15 memcpy(utemplate->imageID, imageID, 16);
17 - memcpy(utemplate->templateID, QCryptographicHash::hash(QByteArray(data, dataSize), QCryptographicHash::Md5).data(), 16);  
18 utemplate->algorithmID = algorithmID; 16 utemplate->algorithmID = algorithmID;
19 utemplate->x = x; 17 utemplate->x = x;
20 utemplate->y = y; 18 utemplate->y = y;
21 utemplate->width = width; 19 utemplate->width = width;
22 utemplate->height = height; 20 utemplate->height = height;
  21 + utemplate->label = label;
23 utemplate->urlSize = urlSize; 22 utemplate->urlSize = urlSize;
24 - utemplate->size = size; 23 + utemplate->fvSize = fvSize;
25 memcpy(reinterpret_cast<char*>(utemplate+1) + 0, url , urlSize); 24 memcpy(reinterpret_cast<char*>(utemplate+1) + 0, url , urlSize);
26 - memcpy(reinterpret_cast<char*>(utemplate+1) + urlSize, data, dataSize); 25 + memcpy(reinterpret_cast<char*>(utemplate+1) + urlSize, fv, fvSize);
27 return utemplate; 26 return utemplate;
28 } 27 }
29 28
@@ -34,14 +33,14 @@ void br_free_utemplate(br_const_utemplate utemplate) @@ -34,14 +33,14 @@ void br_free_utemplate(br_const_utemplate utemplate)
34 33
35 void br_append_utemplate(FILE *file, br_const_utemplate utemplate) 34 void br_append_utemplate(FILE *file, br_const_utemplate utemplate)
36 { 35 {
37 - fwrite(utemplate, sizeof(br_universal_template) + utemplate->size, 1, file); 36 + fwrite(utemplate, sizeof(br_universal_template) + utemplate->urlSize + utemplate->fvSize, 1, file);
38 } 37 }
39 38
40 void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_utemplate_callback callback, br_callback_context context) 39 void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_utemplate_callback callback, br_callback_context context)
41 { 40 {
42 while (begin != end) { 41 while (begin != end) {
43 callback(begin, context); 42 callback(begin, context);
44 - begin = reinterpret_cast<br_const_utemplate>(reinterpret_cast<const char*>(begin) + sizeof(br_universal_template) + begin->size); 43 + begin = reinterpret_cast<br_const_utemplate>(reinterpret_cast<const char*>(begin) + sizeof(br_universal_template) + begin->urlSize + begin->fvSize);
45 if (begin > end) 44 if (begin > end)
46 qFatal("Overshot end of buffer"); 45 qFatal("Overshot end of buffer");
47 } 46 }
@@ -86,8 +85,8 @@ void br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_c @@ -86,8 +85,8 @@ void br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_c
86 return; 85 return;
87 } 86 }
88 87
89 - t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->size);  
90 - read_buffer(file, (char*) &t->data, t->size, false); 88 + t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->urlSize + t->fvSize);
  89 + read_buffer(file, (char*) &t->data, t->urlSize + t->fvSize, false);
91 90
92 if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context)); 91 if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context));
93 else callAndFree(callback, t, context); 92 else callAndFree(callback, t, context);
openbr/universal_template.h
@@ -35,19 +35,19 @@ extern &quot;C&quot; { @@ -35,19 +35,19 @@ extern &quot;C&quot; {
35 */ 35 */
36 struct br_universal_template 36 struct br_universal_template
37 { 37 {
38 - unsigned char imageID[16]; /*!< MD5 hash of the undecoded origin file. */  
39 - unsigned char templateID[16]; /*!< MD5 hash of _data_ after _urlSize_. */  
40 - int32_t algorithmID; /*!< interpretation of _data_ after _urlSize_. */ 38 + unsigned char imageID[16]; /*!< MD5 hash of the undecoded origin file. */
  39 + int32_t algorithmID; /*!< interpretation of _data_ after _urlSize_. */
41 uint32_t x; /*!< region of interest horizontal offset (pixels). */ 40 uint32_t x; /*!< region of interest horizontal offset (pixels). */
42 uint32_t y; /*!< region of interest vertical offset (pixels). */ 41 uint32_t y; /*!< region of interest vertical offset (pixels). */
43 uint32_t width; /*!< region of interest horizontal size (pixels). */ 42 uint32_t width; /*!< region of interest horizontal size (pixels). */
44 uint32_t height; /*!< region of interest vertical size (pixels). */ 43 uint32_t height; /*!< region of interest vertical size (pixels). */
  44 + uint32_t label; /*!< supervised training class or manually annotated ground truth. */
45 uint32_t urlSize; /*!< length of null-terminated URL at the beginning of _data_, 45 uint32_t urlSize; /*!< length of null-terminated URL at the beginning of _data_,
46 including the null-terminator character. */ 46 including the null-terminator character. */
47 - uint32_t size; /*!< length of _data_. */  
48 - unsigned char data[]; /*!< _size_-byte buffer. 47 + uint32_t fvSize; /*!< length of the feature vector after the URL in _data_. */
  48 + unsigned char data[]; /*!< (_urlSize_ + _fvSize_)-byte buffer.
49 The first _urlSize_ bytes represent the URL. 49 The first _urlSize_ bytes represent the URL.
50 - The remaining (_size_ - _urlSize_) bytes represent the template data. */ 50 + The remaining _fvSize_ bytes represent the feature vector. */
51 }; 51 };
52 52
53 typedef struct br_universal_template *br_utemplate; 53 typedef struct br_universal_template *br_utemplate;
@@ -57,7 +57,7 @@ typedef const struct br_universal_template *br_const_utemplate; @@ -57,7 +57,7 @@ typedef const struct br_universal_template *br_const_utemplate;
57 * \brief br_universal_template constructor. 57 * \brief br_universal_template constructor.
58 * \see br_free_utemplate 58 * \see br_free_utemplate
59 */ 59 */
60 -BR_EXPORT br_utemplate br_new_utemplate(const int8_t *imageID, int32_t algorithmID, size_t x, size_t y, size_t width, size_t height, const char *url, const char *data, uint32_t dataSize); 60 +BR_EXPORT br_utemplate br_new_utemplate(const char *imageID, int32_t algorithmID, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t label, const char *url, const char *fv, uint32_t fvSize);
61 61
62 /*! 62 /*!
63 * \brief br_universal_template destructor. 63 * \brief br_universal_template destructor.