Commit 93d6e860e326c08acdc8cec17712fbe1b7e22191
1 parent
99e03750
first draft of br-enroll reference implementation
Showing
3 changed files
with
32 additions
and
28 deletions
app/br-download/br-download.cpp
| ... | ... | @@ -60,9 +60,6 @@ static bool processReply(QNetworkReply* reply) |
| 60 | 60 | if (!permissive && imdecode(Mat(1, data.size(), CV_8UC1, (void*)data.data()), IMREAD_ANYDEPTH | IMREAD_ANYCOLOR).empty()) |
| 61 | 61 | return false; |
| 62 | 62 | |
| 63 | - static QMutex lock; | |
| 64 | - QMutexLocker locker(&lock); | |
| 65 | - | |
| 66 | 63 | const QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5); |
| 67 | 64 | br_append_utemplate_contents(stdout, reinterpret_cast<const int8_t*>(hash.data()), reinterpret_cast<const int8_t*>(hash.data()), 3, data.size(), reinterpret_cast<const int8_t*>(data.data())); |
| 68 | 65 | return true; |
| ... | ... | @@ -75,7 +72,7 @@ int main(int argc, char *argv[]) |
| 75 | 72 | QNetworkAccessManager networkAccessManager; |
| 76 | 73 | |
| 77 | 74 | for (int i=1; i<argc; i++) { |
| 78 | - if (!strcmp(argv[i], "-help" )) { help(); exit(0); } | |
| 75 | + if (!strcmp(argv[i], "-help" )) { help(); exit(EXIT_SUCCESS); } | |
| 79 | 76 | else if (!strcmp(argv[i], "-json" )) json = true; |
| 80 | 77 | else if (!strcmp(argv[i], "-permissive")) permissive = true; |
| 81 | 78 | else { url_provided = processReply(networkAccessManager.get(QNetworkRequest(QUrl(argv[i])))); } | ... | ... |
app/br-enroll/br-enroll.cpp
| ... | ... | @@ -46,17 +46,25 @@ static void enroll_utemplate(br_const_utemplate utemplate) |
| 46 | 46 | if (utemplate->algorithmID != 3) |
| 47 | 47 | qFatal("Expected an encoded image."); |
| 48 | 48 | |
| 49 | - TemplateList templateList; | |
| 50 | - templateList.append(Template(imdecode(Mat(1, utemplate->size, CV_8UC1, (void*) utemplate->data), IMREAD_UNCHANGED))); | |
| 51 | - templateList >> *algorithm; | |
| 49 | + TemplateList templates; | |
| 50 | + templates.append(Template(imdecode(Mat(1, utemplate->size, CV_8UC1, (void*) utemplate->data), IMREAD_UNCHANGED))); | |
| 51 | + templates >> *algorithm; | |
| 52 | + | |
| 53 | + foreach (const Template &t, templates) { | |
| 54 | + const Mat &m = t.m(); | |
| 55 | + const uint32_t size = m.rows * m.cols * m.elemSize(); | |
| 56 | + const QByteArray templateID = QCryptographicHash::hash(QByteArray((const char*) m.data, size), QCryptographicHash::Md5); | |
| 57 | + br_append_utemplate_contents(stdout, utemplate->imageID, (const int8_t*) templateID.data(), -1, size, (const int8_t*) m.data); | |
| 58 | + } | |
| 52 | 59 | } |
| 53 | 60 | |
| 54 | 61 | int main(int argc, char *argv[]) |
| 55 | 62 | { |
| 56 | 63 | for (int i=1; i<argc; i++) |
| 57 | - if (!strcmp(argv[i], "-help")) { help(); exit(0); } | |
| 64 | + if (!strcmp(argv[i], "-help")) { help(); exit(EXIT_SUCCESS); } | |
| 58 | 65 | |
| 59 | 66 | Context::initialize(argc, argv, "", false); |
| 67 | + Globals->quiet = true; | |
| 60 | 68 | algorithm = Transform::fromAlgorithm("FaceRecognition"); |
| 61 | 69 | br_iterate_utemplates_file(stdin, enroll_utemplate); |
| 62 | 70 | Context::finalize(); | ... | ... |
openbr/universal_template.cpp
| ... | ... | @@ -27,13 +27,14 @@ void br_append_utemplate(FILE *file, br_const_utemplate utemplate) |
| 27 | 27 | |
| 28 | 28 | 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) |
| 29 | 29 | { |
| 30 | - QFile qFile; | |
| 31 | - qFile.open(file, QFile::WriteOnly | QFile::Append); | |
| 32 | - qFile.write((const char*) imageID, 16); | |
| 33 | - qFile.write((const char*) templateID, 16); | |
| 34 | - qFile.write((const char*) &algorithmID, 4); | |
| 35 | - qFile.write((const char*) &size, 4); | |
| 36 | - qFile.write((const char*) data, size); | |
| 30 | + static QMutex lock; | |
| 31 | + QMutexLocker locker(&lock); | |
| 32 | + | |
| 33 | + fwrite(imageID, 16, 1, file); | |
| 34 | + fwrite(templateID, 16, 1, file); | |
| 35 | + fwrite(&algorithmID, 4, 1, file); | |
| 36 | + fwrite(&size, 4, 1, file); | |
| 37 | + fwrite(data, 1, size, file); | |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | 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_ |
| 46 | 47 | |
| 47 | 48 | void br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback) |
| 48 | 49 | { |
| 49 | - QFile qFile; | |
| 50 | - qFile.open(file, QFile::ReadOnly); | |
| 51 | - while (!qFile.atEnd()) { | |
| 52 | - br_universal_template header; | |
| 53 | - if (qFile.peek((char*) &header, sizeof(br_universal_template)) != sizeof(br_universal_template)) | |
| 54 | - qFatal("Unexpected EOF when peeking universal template header."); | |
| 55 | - | |
| 56 | - const uint32_t size = sizeof(br_universal_template) + header.size; | |
| 57 | - QByteArray data = qFile.read(size); | |
| 58 | - if (uint32_t(data.size()) != size) | |
| 59 | - qFatal("Unexepected EOF when reading universal template."); | |
| 60 | - | |
| 61 | - callback(reinterpret_cast<br_const_utemplate>(data.data())); | |
| 50 | + while (!feof(file)) { | |
| 51 | + br_utemplate t = (br_utemplate) malloc(sizeof(br_universal_template)); | |
| 52 | + | |
| 53 | + if (fread(t, sizeof(br_universal_template), 1, file) > 0) { | |
| 54 | + t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->size); | |
| 55 | + if (fread(t+1, 1, t->size, file) != t->size) | |
| 56 | + qFatal("Unexepected EOF when reading universal template data."); | |
| 57 | + callback(t); | |
| 58 | + } | |
| 59 | + | |
| 60 | + free(t); | |
| 62 | 61 | } |
| 63 | 62 | } | ... | ... |