Commit a8aad6202f1d80d5e2459312902dafafd823e692

Authored by Scott Klum
1 parent ade222ac

Added DLib

openbr/plugins/dlib.cmake 0 โ†’ 100644
  1 +set(BR_WITH_DLIB OFF CACHE BOOL "Build with LibLinear")
  2 +
  3 +message(${BR_WITH_DLIB})
  4 +
  5 +if(${BR_WITH_DLIB})
  6 + find_package(DLib REQUIRED)
  7 + set(BR_THIRDPARTY_SRC ${BR_THIRDPARTY_SRC} plugins/dlib.cpp)
  8 + set(BR_THIRDPARTY_LIBS ${BR_THIRDPARTY_LIBS} ${DLib_LIBS})
  9 +
  10 + install(DIRECTORY ${DLib_DIR}/models/ DESTINATION share/openbr/models/dlib)
  11 +endif()
... ...
openbr/plugins/dlib.cpp 0 โ†’ 100644
  1 +#include "openbr_internal.h"
  2 +#include "openbr/core/qtutils.h"
  3 +#include "openbr/core/eigenutils.h"
  4 +
  5 +#include <dlib/image_processing/frontal_face_detector.h>
  6 +#include <dlib/image_processing/render_face_detections.h>
  7 +#include <dlib/svm_threaded.h>
  8 +#include <dlib/image_processing.h>
  9 +#include <dlib/gui_widgets.h>
  10 +#include <dlib/image_io.h>
  11 +#include <dlib/opencv.h>
  12 +
  13 +#include <QTemporaryFile>
  14 +
  15 +using namespace std;
  16 +using namespace dlib;
  17 +
  18 +namespace br
  19 +{
  20 +
  21 +class DLibShapeResourceMaker : public ResourceMaker<shape_predictor>
  22 +{
  23 +
  24 +private:
  25 + shape_predictor *make() const
  26 + {
  27 + shape_predictor *sp = new shape_predictor();
  28 + dlib::deserialize(qPrintable(Globals->sdkPath + "/share/openbr/models/dlib/shape_predictor_68_face_landmarks.dat")) >> *sp;
  29 + return sp;
  30 + }
  31 +};
  32 +
  33 +class DLandmarkerTransform : public UntrainableTransform
  34 +{
  35 + Q_OBJECT
  36 +
  37 +private:
  38 +
  39 + Resource<shape_predictor> shapeResource;
  40 +
  41 + void init()
  42 + {
  43 + shapeResource.setResourceMaker(new DLibShapeResourceMaker());
  44 + }
  45 +
  46 + void project(const Template &src, Template &dst) const
  47 + {
  48 + dst = src;
  49 +
  50 + if (!src.file.rects().isEmpty()) {
  51 + shape_predictor *sp = shapeResource.acquire();
  52 +
  53 + cv_image<bgr_pixel> cimg(src.m().clone());
  54 +
  55 + for (unsigned long j = 0; j < src.file.rects().size(); ++j)
  56 + {
  57 + QRectF rect = src.file.rects()[j];
  58 + rectangle r(rect.left(),rect.top(),rect.right(),rect.bottom());
  59 + full_object_detection shape = (*sp)(cimg, r);
  60 + for (int i=0; i<shape.num_parts(); i++)
  61 + dst.file.appendPoint(QPointF(shape.part(i)(0),shape.part(i)(1)));
  62 + }
  63 +
  64 + shapeResource.release(sp);
  65 + }
  66 + }
  67 +};
  68 +
  69 +BR_REGISTER(Transform, DLandmarkerTransform)
  70 +
  71 +class DObjectDetectTransform : public Transform
  72 +{
  73 + Q_OBJECT
  74 +
  75 + Q_PROPERTY(int winSize READ get_winSize WRITE set_winSize RESET reset_winSize STORED true)
  76 + Q_PROPERTY(float C READ get_C WRITE set_C RESET reset_C STORED true)
  77 + Q_PROPERTY(float epsilon READ get_epsilon WRITE set_epsilon RESET reset_epsilon STORED true)
  78 + BR_PROPERTY(int, winSize, 80)
  79 + BR_PROPERTY(float, C, 1)
  80 + BR_PROPERTY(float, epsilon, .01)
  81 +
  82 +private:
  83 + typedef scan_fhog_pyramid<pyramid_down<6> > image_scanner_type;
  84 + mutable object_detector<image_scanner_type> detector;
  85 +
  86 + void train(const TemplateList &data)
  87 + {
  88 + dlib::array<array2d<unsigned char> > samples;
  89 + std::vector<std::vector<rectangle> > boxes;
  90 +
  91 + foreach (const Template &t, data) {
  92 + if (!t.file.rects().isEmpty()) {
  93 + cv_image<bgr_pixel> cimg(t.m().clone());
  94 +
  95 + array2d<unsigned char> image;
  96 + assign_image(image,cimg);
  97 +
  98 + samples.push_back(image);
  99 +
  100 + std::vector<rectangle> b;
  101 + foreach (const QRectF &r, t.file.rects())
  102 + b.push_back(rectangle(r.left(),r.top(),r.right(),r.bottom()));
  103 +
  104 + boxes.push_back(b);
  105 + }
  106 + }
  107 +
  108 + add_image_left_right_flips(samples, boxes);
  109 +
  110 + image_scanner_type scanner;
  111 +
  112 + scanner.set_detection_window_size(winSize, winSize);
  113 + structural_object_detection_trainer<image_scanner_type> trainer(scanner);
  114 + trainer.set_num_threads(max(1,QThread::idealThreadCount()));
  115 + trainer.set_c(C);
  116 + trainer.set_epsilon(epsilon);
  117 +
  118 + detector = trainer.train(samples, boxes);
  119 + }
  120 +
  121 + void project(const Template &src, Template &dst) const
  122 + {
  123 + dst = src;
  124 + cv_image<bgr_pixel> cimg(src.m().clone());
  125 + array2d<unsigned char> image;
  126 + assign_image(image,cimg);
  127 +
  128 + std::vector<rectangle> dets = detector(image);
  129 +
  130 + for (int i=0; i<dets.size(); i++)
  131 + dst.file.appendRect(QRectF(QPointF(dets[i].left(),dets[i].top()),QPointF(dets[i].right(),dets[i].bottom())));
  132 + }
  133 +
  134 + void store(QDataStream &stream) const
  135 + {
  136 + // Create local file
  137 + QTemporaryFile tempFile;
  138 + tempFile.open();
  139 + tempFile.close();
  140 +
  141 + dlib::serialize(qPrintable(tempFile.fileName())) << detector;
  142 +
  143 + // Copy local file contents to stream
  144 + tempFile.open();
  145 + QByteArray data = tempFile.readAll();
  146 + tempFile.close();
  147 + stream << data;
  148 + }
  149 +
  150 + void load(QDataStream &stream)
  151 + {
  152 + // Copy local file contents from stream
  153 + QByteArray data;
  154 + stream >> data;
  155 +
  156 + // Create local file
  157 + QTemporaryFile tempFile(QDir::tempPath()+"/model");
  158 + tempFile.open();
  159 + tempFile.write(data);
  160 + tempFile.close();
  161 +
  162 + // Load MLP from local file
  163 + dlib::deserialize(qPrintable(tempFile.fileName())) >> detector;
  164 + }
  165 +};
  166 +
  167 +BR_REGISTER(Transform, DObjectDetectTransform)
  168 +
  169 +} // namespace br
  170 +
  171 +#include "dlib.moc"
... ...
share/openbr/cmake/FindDLib.cmake 0 โ†’ 100644
  1 +find_path(DLib_DIR dlib ${CMAKE_SOURCE_DIR}/3rdparty/*)
  2 +
  3 +message(${DLib_DIR})
  4 +
  5 +mark_as_advanced(DLib_DIR)
  6 +include_directories(${DLib_DIR})
  7 +link_directories(${DLib_DIR}/examples/build/dlib_build)
  8 +
  9 +set(DLib_LIBS dlib)
0 10 \ No newline at end of file
... ...