diff --git a/openbr/plugins/gallery.cpp b/openbr/plugins/gallery.cpp index a08a8fa..02b0061 100644 --- a/openbr/plugins/gallery.cpp +++ b/openbr/plugins/gallery.cpp @@ -232,28 +232,24 @@ class utGallery : public BinaryGallery Template t; br_universal_template ut; if (gallery.read((char*)&ut, sizeof(br_universal_template)) == sizeof(br_universal_template)) { - QByteArray data(ut.size, Qt::Uninitialized); + QByteArray data(ut.urlSize + ut.fvSize, Qt::Uninitialized); char *dst = data.data(); - qint64 bytesNeeded = ut.size; + 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.size)); + 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; } - if (QCryptographicHash::hash(data.mid(ut.urlSize), QCryptographicHash::Md5) != QByteArray((const char*)ut.templateID, 16)) - qFatal("MD5 hash check failure!"); - t.file.set("ImageID", QVariant(QByteArray((const char*)ut.imageID, 16).toHex())); - t.file.set("TemplateID", QVariant(QByteArray((const char*)ut.templateID, 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.size - 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(dataStart); @@ -267,13 +263,13 @@ class utGallery : public BinaryGallery dataSize -= sizeof(uint32_t)*4; t.file.set("First_Eye", QPointF(*rightEyeX, *rightEyeY)); t.file.set("Second_Eye", QPointF(*leftEyeX, *leftEyeY)); - } - else { + } 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()) @@ -317,30 +313,28 @@ class utGallery : public BinaryGallery width = t.file.get("Width", 0); height = t.file.get("Height", 0); } - - QCryptographicHash templateID(QCryptographicHash::Md5); - templateID.addData(header); - if (algorithmID != 0) - templateID.addData((const char*) t.m().data, t.m().rows * t.m().cols * t.m().elemSize()); + const uint32_t label = t.file.get("Label", 0); gallery.write(imageID); - gallery.write(templateID.result()); - gallery.write((const char*) &algorithmID, sizeof(uint32_t)); + 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 fvSize = t.m().rows * t.m().cols * t.m().elemSize(); - const uint32_t size = urlSize + header.size() + (algorithmID == 0 ? 0 : fvSize); - gallery.write((const char*) &size, 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); - gallery.write(header); - if (algorithmID != 0) - gallery.write((const char*) t.m().data, fvSize); + if (algorithmID != 0) { + gallery.write(header); + gallery.write((const char*) t.m().data, signatureSize); + } } }; diff --git a/openbr/universal_template.cpp b/openbr/universal_template.cpp index 59feea4..79731ac 100644 --- a/openbr/universal_template.cpp +++ b/openbr/universal_template.cpp @@ -8,22 +8,21 @@ #include "universal_template.h" -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) +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) { const uint32_t urlSize = strlen(url) + 1; - const uint32_t size = urlSize + dataSize; - br_utemplate utemplate = (br_utemplate) malloc(sizeof(br_universal_template) + size); + br_utemplate utemplate = (br_utemplate) malloc(sizeof(br_universal_template) + urlSize + fvSize); memcpy(utemplate->imageID, imageID, 16); - memcpy(utemplate->templateID, QCryptographicHash::hash(QByteArray(data, dataSize), QCryptographicHash::Md5).data(), 16); utemplate->algorithmID = algorithmID; utemplate->x = x; utemplate->y = y; utemplate->width = width; utemplate->height = height; + utemplate->label = label; utemplate->urlSize = urlSize; - utemplate->size = size; + utemplate->fvSize = fvSize; memcpy(reinterpret_cast(utemplate+1) + 0, url , urlSize); - memcpy(reinterpret_cast(utemplate+1) + urlSize, data, dataSize); + memcpy(reinterpret_cast(utemplate+1) + urlSize, fv, fvSize); return utemplate; } @@ -34,14 +33,14 @@ void br_free_utemplate(br_const_utemplate utemplate) void br_append_utemplate(FILE *file, br_const_utemplate utemplate) { - fwrite(utemplate, sizeof(br_universal_template) + utemplate->size, 1, file); + fwrite(utemplate, sizeof(br_universal_template) + utemplate->urlSize + utemplate->fvSize, 1, file); } void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_utemplate_callback callback, br_callback_context context) { while (begin != end) { callback(begin, context); - begin = reinterpret_cast(reinterpret_cast(begin) + sizeof(br_universal_template) + begin->size); + begin = reinterpret_cast(reinterpret_cast(begin) + sizeof(br_universal_template) + begin->urlSize + begin->fvSize); if (begin > end) qFatal("Overshot end of buffer"); } @@ -86,8 +85,8 @@ void br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_c return; } - t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->size); - read_buffer(file, (char*) &t->data, t->size, false); + t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->urlSize + t->fvSize); + read_buffer(file, (char*) &t->data, t->urlSize + t->fvSize, false); if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context)); else callAndFree(callback, t, context); diff --git a/openbr/universal_template.h b/openbr/universal_template.h index 61cf741..979871d 100644 --- a/openbr/universal_template.h +++ b/openbr/universal_template.h @@ -35,19 +35,19 @@ extern "C" { */ struct br_universal_template { - unsigned char imageID[16]; /*!< MD5 hash of the undecoded origin file. */ - unsigned char templateID[16]; /*!< MD5 hash of _data_ after _urlSize_. */ - int32_t algorithmID; /*!< interpretation of _data_ after _urlSize_. */ + unsigned char imageID[16]; /*!< MD5 hash of the undecoded origin file. */ + int32_t algorithmID; /*!< interpretation of _data_ after _urlSize_. */ uint32_t x; /*!< region of interest horizontal offset (pixels). */ uint32_t y; /*!< region of interest vertical offset (pixels). */ uint32_t width; /*!< region of interest horizontal size (pixels). */ uint32_t height; /*!< region of interest vertical size (pixels). */ + uint32_t label; /*!< supervised training class or manually annotated ground truth. */ uint32_t urlSize; /*!< length of null-terminated URL at the beginning of _data_, including the null-terminator character. */ - uint32_t size; /*!< length of _data_. */ - unsigned char data[]; /*!< _size_-byte buffer. + uint32_t fvSize; /*!< length of the feature vector after the URL in _data_. */ + unsigned char data[]; /*!< (_urlSize_ + _fvSize_)-byte buffer. The first _urlSize_ bytes represent the URL. - The remaining (_size_ - _urlSize_) bytes represent the template data. */ + The remaining _fvSize_ bytes represent the feature vector. */ }; typedef struct br_universal_template *br_utemplate; @@ -57,7 +57,7 @@ typedef const struct br_universal_template *br_const_utemplate; * \brief br_universal_template constructor. * \see br_free_utemplate */ -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); +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); /*! * \brief br_universal_template destructor.