diff --git a/app/br-download/br-download.cpp b/app/br-download/br-download.cpp index ae5fd41..af96e2c 100644 --- a/app/br-download/br-download.cpp +++ b/app/br-download/br-download.cpp @@ -60,9 +60,6 @@ static bool processReply(QNetworkReply* reply) if (!permissive && imdecode(Mat(1, data.size(), CV_8UC1, (void*)data.data()), IMREAD_ANYDEPTH | IMREAD_ANYCOLOR).empty()) return false; - static QMutex lock; - QMutexLocker locker(&lock); - const QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5); br_append_utemplate_contents(stdout, reinterpret_cast(hash.data()), reinterpret_cast(hash.data()), 3, data.size(), reinterpret_cast(data.data())); return true; @@ -75,7 +72,7 @@ int main(int argc, char *argv[]) QNetworkAccessManager networkAccessManager; for (int i=1; ialgorithmID != 3) qFatal("Expected an encoded image."); - TemplateList templateList; - templateList.append(Template(imdecode(Mat(1, utemplate->size, CV_8UC1, (void*) utemplate->data), IMREAD_UNCHANGED))); - templateList >> *algorithm; + TemplateList templates; + templates.append(Template(imdecode(Mat(1, utemplate->size, CV_8UC1, (void*) utemplate->data), IMREAD_UNCHANGED))); + templates >> *algorithm; + + foreach (const Template &t, templates) { + const Mat &m = t.m(); + const uint32_t size = m.rows * m.cols * m.elemSize(); + const QByteArray templateID = QCryptographicHash::hash(QByteArray((const char*) m.data, size), QCryptographicHash::Md5); + br_append_utemplate_contents(stdout, utemplate->imageID, (const int8_t*) templateID.data(), -1, size, (const int8_t*) m.data); + } } int main(int argc, char *argv[]) { for (int i=1; iquiet = true; algorithm = Transform::fromAlgorithm("FaceRecognition"); br_iterate_utemplates_file(stdin, enroll_utemplate); Context::finalize(); diff --git a/openbr/universal_template.cpp b/openbr/universal_template.cpp index a9f9eca..524496f 100644 --- a/openbr/universal_template.cpp +++ b/openbr/universal_template.cpp @@ -27,13 +27,14 @@ void br_append_utemplate(FILE *file, br_const_utemplate utemplate) void br_append_utemplate_contents(FILE *file, const int8_t *imageID, const int8_t *templateID, int32_t algorithmID, uint32_t size, const int8_t *data) { - QFile qFile; - qFile.open(file, QFile::WriteOnly | QFile::Append); - qFile.write((const char*) imageID, 16); - qFile.write((const char*) templateID, 16); - qFile.write((const char*) &algorithmID, 4); - qFile.write((const char*) &size, 4); - qFile.write((const char*) data, size); + static QMutex lock; + QMutexLocker locker(&lock); + + fwrite(imageID, 16, 1, file); + fwrite(templateID, 16, 1, file); + fwrite(&algorithmID, 4, 1, file); + fwrite(&size, 4, 1, file); + fwrite(data, 1, size, file); } void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_utemplate_callback callback) @@ -46,18 +47,16 @@ void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_ void br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback) { - QFile qFile; - qFile.open(file, QFile::ReadOnly); - while (!qFile.atEnd()) { - br_universal_template header; - if (qFile.peek((char*) &header, sizeof(br_universal_template)) != sizeof(br_universal_template)) - qFatal("Unexpected EOF when peeking universal template header."); - - const uint32_t size = sizeof(br_universal_template) + header.size; - QByteArray data = qFile.read(size); - if (uint32_t(data.size()) != size) - qFatal("Unexepected EOF when reading universal template."); - - callback(reinterpret_cast(data.data())); + while (!feof(file)) { + br_utemplate t = (br_utemplate) malloc(sizeof(br_universal_template)); + + if (fread(t, sizeof(br_universal_template), 1, file) > 0) { + t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->size); + if (fread(t+1, 1, t->size, file) != t->size) + qFatal("Unexepected EOF when reading universal template data."); + callback(t); + } + + free(t); } }