Commit 0d5390bbc6ecf9b27909d54b782938605f9f681a

Authored by Scott Klum
1 parent d4bc60e2

Set up stasm 4 models, removed some globals

openbr/plugins/stasm4.cmake
@@ -11,5 +11,5 @@ if(${BR_WITH_STASM4}) @@ -11,5 +11,5 @@ if(${BR_WITH_STASM4})
11 install(DIRECTORY ${Stasm_DIR}/build/ DESTINATION lib) 11 install(DIRECTORY ${Stasm_DIR}/build/ DESTINATION lib)
12 endif() 12 endif()
13 13
14 - #install(DIRECTORY ${Stasm_DIR}/models/ DESTINATION share/openbr/models/stasm) 14 + install(DIRECTORY ${Stasm_DIR}/data/ DESTINATION share/openbr/models/stasm)
15 endif() 15 endif()
openbr/plugins/stasm4.cpp
1 #include <stasm_lib.h> 1 #include <stasm_lib.h>
2 -#include <opencv2/highgui/highgui.hpp> 2 +#include <opencv2/objdetect/objdetect.hpp>
3 #include "openbr_internal.h" 3 #include "openbr_internal.h"
4 4
5 using namespace cv; 5 using namespace cv;
@@ -7,6 +7,31 @@ using namespace cv; @@ -7,6 +7,31 @@ using namespace cv;
7 namespace br 7 namespace br
8 { 8 {
9 9
  10 +class CascadeResourceMaker : public ResourceMaker<CascadeClassifier>
  11 +{
  12 + QString file;
  13 +
  14 +public:
  15 + CascadeResourceMaker(const QString &model)
  16 + {
  17 + file = Globals->sdkPath + "/share/openbr/models/";
  18 + if (model == "Ear") file += "haarcascades/haarcascade_ear.xml";
  19 + else if (model == "Eye") file += "haarcascades/haarcascade_eye_tree_eyeglasses.xml";
  20 + else if (model == "FrontalFace") file += "haarcascades/haarcascade_frontalface_alt2.xml";
  21 + else if (model == "ProfileFace") file += "haarcascades/haarcascade_profileface.xml";
  22 + else qFatal("Invalid model.");
  23 + }
  24 +
  25 +private:
  26 + CascadeClassifier *make() const
  27 + {
  28 + CascadeClassifier *cascade = new CascadeClassifier();
  29 + if (!cascade->load(file.toStdString()))
  30 + qFatal("Failed to load: %s", qPrintable(file));
  31 + return cascade;
  32 + }
  33 +};
  34 +
10 /*! 35 /*!
11 * \ingroup initializers 36 * \ingroup initializers
12 * \brief Initialize Stasm 37 * \brief Initialize Stasm
@@ -39,18 +64,25 @@ class StasmTransform : public UntrainableTransform @@ -39,18 +64,25 @@ class StasmTransform : public UntrainableTransform
39 //QList<ASM_MODEL> models; 64 //QList<ASM_MODEL> models;
40 mutable QMutex mutex; 65 mutable QMutex mutex;
41 66
  67 + Resource<CascadeClassifier> cascadeResource;
  68 +
42 void init() 69 void init()
43 { 70 {
44 - 71 + if (!stasm_init(qPrintable(Globals->sdkPath + "/share/openbr/models/stasm"), 0)) qFatal("Failed to initalize stasm.");
  72 + cascadeResource.setResourceMaker(new CascadeResourceMaker("FrontalFace"));
45 } 73 }
46 74
47 void project(const Template &src, Template &dst) const 75 void project(const Template &src, Template &dst) const
48 { 76 {
49 QMutexLocker locker(&mutex); 77 QMutexLocker locker(&mutex);
50 78
  79 + CascadeClassifier *cascade = cascadeResource.acquire();
  80 +
51 int foundface; 81 int foundface;
52 float landmarks[2 * stasm_NLANDMARKS]; // x,y coords (note the 2) 82 float landmarks[2 * stasm_NLANDMARKS]; // x,y coords (note the 2)
53 - stasm_search_single(&foundface, landmarks, reinterpret_cast<const char*>(src.m().data), src.m().cols, src.m().rows, NULL, "/Users/scottklum/openbr/3rdparty/stasm4.0.0/data"); 83 + stasm_search_single(&foundface, landmarks, reinterpret_cast<const char*>(src.m().data), src.m().cols, src.m().rows, *cascade, NULL, NULL);
  84 +
  85 + cascadeResource.release(cascade);
54 86
55 if (!foundface) 87 if (!foundface)
56 qDebug() << "No face found in " << qPrintable("/Users/scottklum/facesketchid/data/img/" + src.file.path() + "/" + src.file.fileName()); 88 qDebug() << "No face found in " << qPrintable("/Users/scottklum/facesketchid/data/img/" + src.file.path() + "/" + src.file.fileName());