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 11 install(DIRECTORY ${Stasm_DIR}/build/ DESTINATION lib)
12 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 15 endif()
... ...
openbr/plugins/stasm4.cpp
1 1 #include <stasm_lib.h>
2   -#include <opencv2/highgui/highgui.hpp>
  2 +#include <opencv2/objdetect/objdetect.hpp>
3 3 #include "openbr_internal.h"
4 4  
5 5 using namespace cv;
... ... @@ -7,6 +7,31 @@ using namespace cv;
7 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 36 * \ingroup initializers
12 37 * \brief Initialize Stasm
... ... @@ -39,18 +64,25 @@ class StasmTransform : public UntrainableTransform
39 64 //QList<ASM_MODEL> models;
40 65 mutable QMutex mutex;
41 66  
  67 + Resource<CascadeClassifier> cascadeResource;
  68 +
42 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 75 void project(const Template &src, Template &dst) const
48 76 {
49 77 QMutexLocker locker(&mutex);
50 78  
  79 + CascadeClassifier *cascade = cascadeResource.acquire();
  80 +
51 81 int foundface;
52 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 87 if (!foundface)
56 88 qDebug() << "No face found in " << qPrintable("/Users/scottklum/facesketchid/data/img/" + src.file.path() + "/" + src.file.fileName());
... ...