Commit 343cfe9b592731fc23dbb4007ada8fbe662a03e8

Authored by Josh Klontz
1 parent 2b2e5b4a

removed br_universal_template::templateID, introduced br_universal_template::lab…

…el, repurposed br_universal_template::size as br_universal_template::fvSize
openbr/plugins/gallery.cpp
... ... @@ -232,28 +232,24 @@ class utGallery : public BinaryGallery
232 232 Template t;
233 233 br_universal_template ut;
234 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 236 char *dst = data.data();
237   - qint64 bytesNeeded = ut.size;
  237 + qint64 bytesNeeded = ut.urlSize + ut.fvSize;
238 238 while (bytesNeeded > 0) {
239 239 qint64 bytesRead = gallery.read(dst, bytesNeeded);
240 240 if (bytesRead <= 0) {
241 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 244 bytesNeeded -= bytesRead;
245 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 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 249 t.file.set("AlgorithmID", ut.algorithmID);
254 250 t.file.set("URL", QString(data.data()));
255 251 char *dataStart = data.data() + ut.urlSize;
256   - uint32_t dataSize = ut.size - ut.urlSize;
  252 + uint32_t dataSize = ut.fvSize;
257 253 if ((ut.algorithmID <= -1) && (ut.algorithmID >= -3)) {
258 254 t.file.set("FrontalFace", QRectF(ut.x, ut.y, ut.width, ut.height));
259 255 uint32_t *rightEyeX = reinterpret_cast<uint32_t*>(dataStart);
... ... @@ -267,13 +263,13 @@ class utGallery : public BinaryGallery
267 263 dataSize -= sizeof(uint32_t)*4;
268 264 t.file.set("First_Eye", QPointF(*rightEyeX, *rightEyeY));
269 265 t.file.set("Second_Eye", QPointF(*leftEyeX, *leftEyeY));
270   - }
271   - else {
  266 + } else {
272 267 t.file.set("X", ut.x);
273 268 t.file.set("Y", ut.y);
274 269 t.file.set("Width", ut.width);
275 270 t.file.set("Height", ut.height);
276 271 }
  272 + t.file.set("Label", ut.label);
277 273 t.append(cv::Mat(1, dataSize, CV_8UC1, dataStart).clone() /* We don't want a shallow copy! */);
278 274 } else {
279 275 if (!gallery.atEnd())
... ... @@ -317,30 +313,28 @@ class utGallery : public BinaryGallery
317 313 width = t.file.get<uint32_t>("Width", 0);
318 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 double label = t.file.get<double>("Label", 0);
325 317  
326 318 gallery.write(imageID);
327   - gallery.write(templateID.result());
328 319 gallery.write((const char*) &algorithmID, sizeof(uint32_t));
329 320 gallery.write((const char*) &x , sizeof(uint32_t));
330 321 gallery.write((const char*) &y , sizeof(uint32_t));
331 322 gallery.write((const char*) &width , sizeof(uint32_t));
332 323 gallery.write((const char*) &height , sizeof(uint32_t));
  324 + gallery.write((const char*) &label , sizeof(double));
333 325  
334 326 const uint32_t urlSize = url.size() + 1;
335 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 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 8  
9 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 int8_t *imageID, int32_t algorithmID, size_t x, size_t y, size_t width, size_t height, double label, const char *url, const char *fv, uint32_t fvSize)
12 12 {
13 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 15 memcpy(utemplate->imageID, imageID, 16);
17   - memcpy(utemplate->templateID, QCryptographicHash::hash(QByteArray(data, dataSize), QCryptographicHash::Md5).data(), 16);
18 16 utemplate->algorithmID = algorithmID;
19 17 utemplate->x = x;
20 18 utemplate->y = y;
21 19 utemplate->width = width;
22 20 utemplate->height = height;
  21 + utemplate->label = label;
23 22 utemplate->urlSize = urlSize;
24   - utemplate->size = size;
  23 + utemplate->fvSize = fvSize;
25 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 26 return utemplate;
28 27 }
29 28  
... ... @@ -34,14 +33,14 @@ void br_free_utemplate(br_const_utemplate utemplate)
34 33  
35 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 39 void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_utemplate_callback callback, br_callback_context context)
41 40 {
42 41 while (begin != end) {
43 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 44 if (begin > end)
46 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 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 91 if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context));
93 92 else callAndFree(callback, t, context);
... ...
openbr/universal_template.h
... ... @@ -35,19 +35,20 @@ extern &quot;C&quot; {
35 35 */
36 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 40 uint32_t x; /*!< region of interest horizontal offset (pixels). */
42 41 uint32_t y; /*!< region of interest vertical offset (pixels). */
43 42 uint32_t width; /*!< region of interest horizontal size (pixels). */
44 43 uint32_t height; /*!< region of interest vertical size (pixels). */
  44 + double label; /*!< classification/regression supervised training class,
  45 + or manually annotated ground truth. */
45 46 uint32_t urlSize; /*!< length of null-terminated URL at the beginning of _data_,
46 47 including the null-terminator character. */
47   - uint32_t size; /*!< length of _data_. */
48   - unsigned char data[]; /*!< _size_-byte buffer.
  48 + uint32_t fvSize; /*!< length of the feature vector after the URL in _data_. */
  49 + unsigned char data[]; /*!< (_urlSize_ + _fvSize_)-byte buffer.
49 50 The first _urlSize_ bytes represent the URL.
50   - The remaining (_size_ - _urlSize_) bytes represent the template data. */
  51 + The remaining _fvSize_ bytes represent the feature vector. */
51 52 };
52 53  
53 54 typedef struct br_universal_template *br_utemplate;
... ... @@ -57,7 +58,7 @@ typedef const struct br_universal_template *br_const_utemplate;
57 58 * \brief br_universal_template constructor.
58 59 * \see br_free_utemplate
59 60 */
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);
  61 +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, double label, const char *url, const char *fv, uint32_t fvSize);
61 62  
62 63 /*!
63 64 * \brief br_universal_template destructor.
... ...