diff --git a/openbr/janus b/openbr/janus index 734353f..a8e295a 160000 --- a/openbr/janus +++ b/openbr/janus @@ -1 +1 @@ -Subproject commit 734353f82cfb4754f1da40bc69370bcee33b929b +Subproject commit a8e295a7cfb8941b7a2468c5423c591a387071d2 diff --git a/openbr/janus.cpp b/openbr/janus.cpp index e7073ca..b5f6e4f 100644 --- a/openbr/janus.cpp +++ b/openbr/janus.cpp @@ -8,51 +8,107 @@ // Use the provided default implementation of some functions #include "janus/src/janus.cpp" +using namespace br; + +static QSharedPointer transform; +static QSharedPointer distance; + janus_error janus_initialize(const char *sdk_path, const char *model_file) { int argc = 1; - const char *argv[1] = { "" }; - br::Context::initialize(argc, (char**)argv, sdk_path); + const char *argv[1] = { "janus" }; + Context::initialize(argc, (char**)argv, sdk_path); QString algorithm = model_file; - if (algorithm.isEmpty()) algorithm = "FaceRecognition"; - br::Globals->algorithm = algorithm; + if (algorithm.isEmpty()) algorithm = "Cvt(Gray)+Affine(88,88,0.25,0.35)+++:ByteL1"; + transform = Transform::fromAlgorithm(algorithm, false); + distance = Distance::fromAlgorithm(algorithm); return JANUS_SUCCESS; } janus_error janus_finalize() { - br::Context::finalize(); + Context::finalize(); return JANUS_SUCCESS; } +struct janus_incomplete_template_type +{ + QList data; +}; + janus_error janus_initialize_template(janus_incomplete_template *incomplete_template) { - (void) incomplete_template; + *incomplete_template = new janus_incomplete_template_type(); return JANUS_SUCCESS; } janus_error janus_add_image(const janus_image image, const janus_attribute_list attributes, janus_incomplete_template incomplete_template) { - (void) image; - (void) attributes; - (void) incomplete_template; + Template t; + t.append(cv::Mat(image.height, + image.width, + image.color_space == JANUS_GRAY8 ? CV_8UC1 : CV_8UC1, + image.data)); + for (size_t i=0; i("JANUS_RIGHT_EYE_X"), t.file.get("JANUS_RIGHT_EYE_Y"))); + t.file.set("Affine_1", QPointF(t.file.get("JANUS_LEFT_EYE_X"), t.file.get("JANUS_LEFT_EYE_Y"))); + Template u; + transform->project(t, u); + incomplete_template->data.append(u); return JANUS_SUCCESS; } janus_error janus_finalize_template(janus_incomplete_template incomplete_template, janus_template template_, size_t *bytes) -{ - (void) incomplete_template; - (void) template_; - *bytes = 0; +{ + size_t templateBytes = 0; + size_t numTemplates = 0; + *bytes = sizeof(templateBytes) + sizeof(numTemplates); + janus_template pos = template_ + *bytes; + + foreach (const cv::Mat &m, incomplete_template->data) { + assert(m.isContinuous()); + const size_t currentTemplateBytes = m.rows * m.cols * m.elemSize(); + if (templateBytes == 0) + templateBytes = currentTemplateBytes; + if (templateBytes != currentTemplateBytes) + return JANUS_UNKNOWN_ERROR; + if (*bytes + templateBytes > JANUS_MAX_TEMPLATE_SIZE) + break; + memcpy(pos, m.data, templateBytes); + *bytes += templateBytes; + pos = pos + templateBytes; + numTemplates++; + } + + *(reinterpret_cast(template_)+0) = templateBytes; + *(reinterpret_cast(template_)+1) = numTemplates; + delete incomplete_template; return JANUS_SUCCESS; } -janus_error janus_verify(const janus_template a, const size_t a_bytes, const janus_template b, const size_t b_bytes, float *similarity) +janus_error janus_verify(const janus_template a, const janus_template b, float *similarity) { - (void) a; - (void) a_bytes; - (void) b; - (void) b_bytes; - *similarity = 0; + size_t a_bytes, a_templates, b_bytes, b_templates; + a_bytes = *(reinterpret_cast(a)+0); + a_templates = *(reinterpret_cast(a)+1); + b_bytes = *(reinterpret_cast(b)+0); + b_templates = *(reinterpret_cast(b)+1); + if (a_bytes != b_bytes) + return JANUS_UNKNOWN_ERROR; + + float dist = 0; + for (size_t i=0; icompare(cv::Mat(1, a_bytes, CV_8UC1, a+2*sizeof(size_t)+i*a_bytes), + cv::Mat(1, b_bytes, CV_8UC1, b+2*sizeof(size_t)+i*b_bytes)); + *similarity = a_templates * b_templates / dist; return JANUS_SUCCESS; }