Commit 3b730b32f21dbde0fb5257e11fafd8b4c6192a91
Merge branch 'master' of https://github.com/biometrics/openbr
Showing
7 changed files
with
136 additions
and
23 deletions
openbr/plugins/cmake/likely.cmake
| @@ -5,6 +5,6 @@ if(${BR_WITH_LIKELY}) | @@ -5,6 +5,6 @@ if(${BR_WITH_LIKELY}) | ||
| 5 | set(BR_THIRDPARTY_LIBS ${BR_THIRDPARTY_LIBS} ${Likely_LIBS}) | 5 | set(BR_THIRDPARTY_LIBS ${BR_THIRDPARTY_LIBS} ${Likely_LIBS}) |
| 6 | else() | 6 | else() |
| 7 | set(BR_EXCLUDED_PLUGINS ${BR_EXCLUDED_PLUGINS} plugins/core/likely.cpp) | 7 | set(BR_EXCLUDED_PLUGINS ${BR_EXCLUDED_PLUGINS} plugins/core/likely.cpp) |
| 8 | - set(BR_EXCLUDED_PLUGINS ${BR_EXCLUDED_PLUGINS} plugins/format/lmat.cpp) | ||
| 9 | - set(BR_EXCLUDED_PLUGINS ${BR_EXCLUDED_PLUGINS} plugins/gallery/lmat.cpp) | 8 | + set(BR_EXCLUDED_PLUGINS ${BR_EXCLUDED_PLUGINS} plugins/format/lm.cpp) |
| 9 | + set(BR_EXCLUDED_PLUGINS ${BR_EXCLUDED_PLUGINS} plugins/gallery/lm.cpp) | ||
| 10 | endif() | 10 | endif() |
openbr/plugins/core/likely.cpp
| @@ -40,23 +40,26 @@ class LikelyTransform : public Transform | @@ -40,23 +40,26 @@ class LikelyTransform : public Transform | ||
| 40 | qFatal("Failed to compile: %s", qPrintable(sourceFile)); | 40 | qFatal("Failed to compile: %s", qPrintable(sourceFile)); |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | - void train(const TemplateList &) | 43 | + void train(const TemplateList &trainingData) |
| 44 | { | 44 | { |
| 45 | + const likely_const_mat data = likelyFromOpenCVMats(trainingData.data().toVector().toStdVector()); | ||
| 46 | + | ||
| 45 | QByteArray sourceCode; | 47 | QByteArray sourceCode; |
| 46 | QtUtils::readFile(sourceFile, sourceCode); | 48 | QtUtils::readFile(sourceFile, sourceCode); |
| 47 | 49 | ||
| 48 | // Pick settings to minimize code size | 50 | // Pick settings to minimize code size |
| 49 | - likely_settings settings; | ||
| 50 | - settings.opt_level = 2; | ||
| 51 | - settings.size_level = 2; | ||
| 52 | - settings.multicore = false; | ||
| 53 | - settings.heterogeneous = false; | ||
| 54 | - settings.unroll_loops = false; | ||
| 55 | - settings.vectorize_loops = false; | ||
| 56 | - settings.verbose = false; | 51 | + likely_settings settings = likely_default_settings(likely_file_bitcode, false); |
| 52 | + settings.runtime_only = true; | ||
| 57 | 53 | ||
| 58 | likely_mat output; | 54 | likely_mat output; |
| 59 | - const likely_const_env parent = likely_standard(settings, &output, likely_file_bitcode); | 55 | + likely_const_env parent = likely_standard(settings, &output, likely_file_bitcode); |
| 56 | + | ||
| 57 | + { // Construct an environment where `data` is accessible | ||
| 58 | + const likely_const_env env = likely_define("data", data, parent); | ||
| 59 | + likely_release_env(parent); | ||
| 60 | + parent = env; | ||
| 61 | + } | ||
| 62 | + | ||
| 60 | likely_release_env(likely_lex_parse_and_eval(sourceCode.data(), likely_guess_file_type(qPrintable(sourceFile)), parent)); | 63 | likely_release_env(likely_lex_parse_and_eval(sourceCode.data(), likely_guess_file_type(qPrintable(sourceFile)), parent)); |
| 61 | likely_release_env(parent); | 64 | likely_release_env(parent); |
| 62 | 65 | ||
| @@ -64,6 +67,7 @@ class LikelyTransform : public Transform | @@ -64,6 +67,7 @@ class LikelyTransform : public Transform | ||
| 64 | likely_release_mat(output); | 67 | likely_release_mat(output); |
| 65 | 68 | ||
| 66 | compile(); | 69 | compile(); |
| 70 | + likely_release_mat(data); | ||
| 67 | } | 71 | } |
| 68 | 72 | ||
| 69 | void project(const Template &src, Template &dst) const | 73 | void project(const Template &src, Template &dst) const |
openbr/plugins/distance/emd.cpp
0 → 100644
| 1 | +#include <openbr/plugins/openbr_internal.h> | ||
| 2 | +#include <opencv2/imgproc/imgproc.hpp> | ||
| 3 | + | ||
| 4 | +using namespace cv; | ||
| 5 | + | ||
| 6 | +namespace br | ||
| 7 | +{ | ||
| 8 | + | ||
| 9 | +/*! | ||
| 10 | + * \ingroup distances | ||
| 11 | + * \brief Computes Earth Mover's Distance | ||
| 12 | + * \author Scott Klum \cite sklum | ||
| 13 | + * \brief https://www.cs.duke.edu/~tomasi/papers/rubner/rubnerTr98.pdf | ||
| 14 | + */ | ||
| 15 | +class EMDDistance : public UntrainableDistance | ||
| 16 | +{ | ||
| 17 | + Q_OBJECT | ||
| 18 | + | ||
| 19 | + Q_ENUMS(Metric) | ||
| 20 | + Q_PROPERTY(Metric metric READ get_metric WRITE set_metric RESET reset_metric STORED false) | ||
| 21 | + | ||
| 22 | +public: | ||
| 23 | + enum Metric { L1 = CV_DIST_L1, | ||
| 24 | + L2 = CV_DIST_L2, | ||
| 25 | + C = CV_DIST_C }; | ||
| 26 | + | ||
| 27 | +private: | ||
| 28 | + BR_PROPERTY(Metric, metric, L2) | ||
| 29 | + | ||
| 30 | + float compare(const Template &a, const Template &b) const | ||
| 31 | + { | ||
| 32 | + const int dims_a = a.m().rows > 1 ? 3 : 2; | ||
| 33 | + const int dims_b = b.m().rows > 1 ? 3 : 2; | ||
| 34 | + | ||
| 35 | + Mat sig_a(a.m().cols, dims_a, CV_32FC1); | ||
| 36 | + Mat sig_b(b.m().cols, dims_b, CV_32FC1); | ||
| 37 | + | ||
| 38 | + for (int i=0; i<a.m().rows; i++) { | ||
| 39 | + for (int j=0; j<a.m().cols; j++) { | ||
| 40 | + sig_a.at<float>(i*a.m().cols+j,0) = a.m().at<float>(i,j); | ||
| 41 | + sig_a.at<float>(i*a.m().cols+j,1) = j; | ||
| 42 | + if (dims_a == 3) sig_a.at<float>(i*a.m().cols+j,2) = i; | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + for (int i=0; i<b.m().rows; i++) { | ||
| 47 | + for (int j=0; j<b.m().cols; j++) { | ||
| 48 | + sig_b.at<float>(i*b.m().cols+j,0) = b.m().at<float>(i,j); | ||
| 49 | + sig_b.at<float>(i*b.m().cols+j,1) = j; | ||
| 50 | + if (dims_b == 3) sig_a.at<float>(i*b.m().cols+j,2) = i; | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + return EMD(sig_a,sig_b,metric); | ||
| 55 | + } | ||
| 56 | +}; | ||
| 57 | + | ||
| 58 | +BR_REGISTER(Distance, EMDDistance) | ||
| 59 | + | ||
| 60 | +} // namespace br | ||
| 61 | + | ||
| 62 | +#include "distance/emd.moc" |
openbr/plugins/format/lmat.cpp renamed to openbr/plugins/format/lm.cpp
| @@ -13,7 +13,7 @@ namespace br | @@ -13,7 +13,7 @@ namespace br | ||
| 13 | * www.liblikely.org | 13 | * www.liblikely.org |
| 14 | * \author Josh Klontz \cite jklontz | 14 | * \author Josh Klontz \cite jklontz |
| 15 | */ | 15 | */ |
| 16 | -class lmatFormat : public Format | 16 | +class lmFormat : public Format |
| 17 | { | 17 | { |
| 18 | Q_OBJECT | 18 | Q_OBJECT |
| 19 | 19 | ||
| @@ -33,8 +33,8 @@ class lmatFormat : public Format | @@ -33,8 +33,8 @@ class lmatFormat : public Format | ||
| 33 | } | 33 | } |
| 34 | }; | 34 | }; |
| 35 | 35 | ||
| 36 | -BR_REGISTER(Format, lmatFormat) | 36 | +BR_REGISTER(Format, lmFormat) |
| 37 | 37 | ||
| 38 | } // namespace br | 38 | } // namespace br |
| 39 | 39 | ||
| 40 | -#include "format/lmat.moc" | 40 | +#include "format/lm.moc" |
openbr/plugins/gallery/lmat.cpp renamed to openbr/plugins/gallery/lm.cpp
| @@ -14,22 +14,30 @@ namespace br | @@ -14,22 +14,30 @@ namespace br | ||
| 14 | * www.liblikely.org | 14 | * www.liblikely.org |
| 15 | * \author Josh Klontz \cite jklontz | 15 | * \author Josh Klontz \cite jklontz |
| 16 | */ | 16 | */ |
| 17 | -class lmatGallery : public Gallery | 17 | +class lmGallery : public Gallery |
| 18 | { | 18 | { |
| 19 | Q_OBJECT | 19 | Q_OBJECT |
| 20 | QList<cv::Mat> mats; | 20 | QList<cv::Mat> mats; |
| 21 | 21 | ||
| 22 | - ~lmatGallery() | 22 | + ~lmGallery() |
| 23 | { | 23 | { |
| 24 | - const likely_const_mat m = likelyFromOpenCVMat(OpenCVUtils::toMatByRow(mats)); | ||
| 25 | - likely_write(m, qPrintable(file.name)); | 24 | + if (mats.empty()) |
| 25 | + return; | ||
| 26 | + likely_const_mat m = likelyFromOpenCVMats(mats.toVector().toStdVector()); | ||
| 27 | + if (!likely_write(m, qPrintable(file.name))) | ||
| 28 | + qFatal("Write failed"); | ||
| 26 | likely_release_mat(m); | 29 | likely_release_mat(m); |
| 27 | } | 30 | } |
| 28 | 31 | ||
| 29 | TemplateList readBlock(bool *done) | 32 | TemplateList readBlock(bool *done) |
| 30 | { | 33 | { |
| 31 | *done = true; | 34 | *done = true; |
| 32 | - qFatal("Not supported."); | 35 | + TemplateList templates; |
| 36 | + const likely_const_mat m = likely_read(qPrintable(file.name), likely_file_matrix, likely_void); | ||
| 37 | + foreach (const cv::Mat &mat, likelyToOpenCVMats(m)) | ||
| 38 | + templates.append(mat); | ||
| 39 | + likely_release_mat(m); | ||
| 40 | + return templates; | ||
| 33 | } | 41 | } |
| 34 | 42 | ||
| 35 | void write(const Template &t) | 43 | void write(const Template &t) |
| @@ -38,8 +46,8 @@ class lmatGallery : public Gallery | @@ -38,8 +46,8 @@ class lmatGallery : public Gallery | ||
| 38 | } | 46 | } |
| 39 | }; | 47 | }; |
| 40 | 48 | ||
| 41 | -BR_REGISTER(Gallery, lmatGallery) | 49 | +BR_REGISTER(Gallery, lmGallery) |
| 42 | 50 | ||
| 43 | } // namespace br | 51 | } // namespace br |
| 44 | 52 | ||
| 45 | -#include "gallery/lmat.moc" | 53 | +#include "gallery/lm.moc" |
openbr/plugins/gallery/video.cpp
share/openbr/likely/face_recognition.tex
0 → 100644
| 1 | +\documentclass{article} | ||
| 2 | +\usepackage{verbatim} | ||
| 3 | + | ||
| 4 | +\newenvironment{likely} | ||
| 5 | +{ \verbatim } | ||
| 6 | +{ \endverbatim } | ||
| 7 | + | ||
| 8 | +\title{Likely Port: Face Recognition} | ||
| 9 | +\author{Joshua C. Klontz} | ||
| 10 | +\date{\today} | ||
| 11 | +\begin{document} | ||
| 12 | +\maketitle | ||
| 13 | + | ||
| 14 | +\begin{abstract} | ||
| 15 | +This document represents a long-term effort to port the OpenBR face recognition algorithm to Likely\footnote{www.liblikely.org}. | ||
| 16 | +As Likely is a literate programming language, this document is both the source code \emph{and} the documentation. | ||
| 17 | +\end{abstract} | ||
| 18 | + | ||
| 19 | +\section{Consolidated Algorithm} | ||
| 20 | +The top level definition of the face recognition algorithm. | ||
| 21 | + | ||
| 22 | +\begin{likely} | ||
| 23 | +face-recognition := | ||
| 24 | + src :-> | ||
| 25 | + { | ||
| 26 | + dst := src.imitate | ||
| 27 | + (dst src) :=> | ||
| 28 | + dst :<- src | ||
| 29 | + } | ||
| 30 | +\end{likely} | ||
| 31 | + | ||
| 32 | +\section{Entry Point} | ||
| 33 | +The entry point to the outside world is a single C function \texttt{face\_recognition} that takes as input a \texttt{likely\_mat} (the input image) and outputs a new \texttt{likely\_mat} (the feature vector). | ||
| 34 | + | ||
| 35 | +\begin{likely} | ||
| 36 | +(extern u8CXY "face_recognition" u8CXY face-recognition) | ||
| 37 | +\end{likely} | ||
| 38 | + | ||
| 39 | +\end{document} | ||
| 0 | \ No newline at end of file | 40 | \ No newline at end of file |