From a8aad6202f1d80d5e2459312902dafafd823e692 Mon Sep 17 00:00:00 2001 From: Scott Klum Date: Mon, 16 Feb 2015 10:55:10 -0500 Subject: [PATCH] Added DLib --- openbr/plugins/dlib.cmake | 11 +++++++++++ openbr/plugins/dlib.cpp | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ share/openbr/cmake/FindDLib.cmake | 9 +++++++++ 3 files changed, 191 insertions(+), 0 deletions(-) create mode 100644 openbr/plugins/dlib.cmake create mode 100644 openbr/plugins/dlib.cpp create mode 100644 share/openbr/cmake/FindDLib.cmake diff --git a/openbr/plugins/dlib.cmake b/openbr/plugins/dlib.cmake new file mode 100644 index 0000000..f122509 --- /dev/null +++ b/openbr/plugins/dlib.cmake @@ -0,0 +1,11 @@ +set(BR_WITH_DLIB OFF CACHE BOOL "Build with LibLinear") + +message(${BR_WITH_DLIB}) + +if(${BR_WITH_DLIB}) + find_package(DLib REQUIRED) + set(BR_THIRDPARTY_SRC ${BR_THIRDPARTY_SRC} plugins/dlib.cpp) + set(BR_THIRDPARTY_LIBS ${BR_THIRDPARTY_LIBS} ${DLib_LIBS}) + + install(DIRECTORY ${DLib_DIR}/models/ DESTINATION share/openbr/models/dlib) +endif() diff --git a/openbr/plugins/dlib.cpp b/openbr/plugins/dlib.cpp new file mode 100644 index 0000000..c4b82d7 --- /dev/null +++ b/openbr/plugins/dlib.cpp @@ -0,0 +1,171 @@ +#include "openbr_internal.h" +#include "openbr/core/qtutils.h" +#include "openbr/core/eigenutils.h" + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace std; +using namespace dlib; + +namespace br +{ + +class DLibShapeResourceMaker : public ResourceMaker +{ + +private: + shape_predictor *make() const + { + shape_predictor *sp = new shape_predictor(); + dlib::deserialize(qPrintable(Globals->sdkPath + "/share/openbr/models/dlib/shape_predictor_68_face_landmarks.dat")) >> *sp; + return sp; + } +}; + +class DLandmarkerTransform : public UntrainableTransform +{ + Q_OBJECT + +private: + + Resource shapeResource; + + void init() + { + shapeResource.setResourceMaker(new DLibShapeResourceMaker()); + } + + void project(const Template &src, Template &dst) const + { + dst = src; + + if (!src.file.rects().isEmpty()) { + shape_predictor *sp = shapeResource.acquire(); + + cv_image cimg(src.m().clone()); + + for (unsigned long j = 0; j < src.file.rects().size(); ++j) + { + QRectF rect = src.file.rects()[j]; + rectangle r(rect.left(),rect.top(),rect.right(),rect.bottom()); + full_object_detection shape = (*sp)(cimg, r); + for (int i=0; i > image_scanner_type; + mutable object_detector detector; + + void train(const TemplateList &data) + { + dlib::array > samples; + std::vector > boxes; + + foreach (const Template &t, data) { + if (!t.file.rects().isEmpty()) { + cv_image cimg(t.m().clone()); + + array2d image; + assign_image(image,cimg); + + samples.push_back(image); + + std::vector b; + foreach (const QRectF &r, t.file.rects()) + b.push_back(rectangle(r.left(),r.top(),r.right(),r.bottom())); + + boxes.push_back(b); + } + } + + add_image_left_right_flips(samples, boxes); + + image_scanner_type scanner; + + scanner.set_detection_window_size(winSize, winSize); + structural_object_detection_trainer trainer(scanner); + trainer.set_num_threads(max(1,QThread::idealThreadCount())); + trainer.set_c(C); + trainer.set_epsilon(epsilon); + + detector = trainer.train(samples, boxes); + } + + void project(const Template &src, Template &dst) const + { + dst = src; + cv_image cimg(src.m().clone()); + array2d image; + assign_image(image,cimg); + + std::vector dets = detector(image); + + for (int i=0; i> data; + + // Create local file + QTemporaryFile tempFile(QDir::tempPath()+"/model"); + tempFile.open(); + tempFile.write(data); + tempFile.close(); + + // Load MLP from local file + dlib::deserialize(qPrintable(tempFile.fileName())) >> detector; + } +}; + +BR_REGISTER(Transform, DObjectDetectTransform) + +} // namespace br + +#include "dlib.moc" diff --git a/share/openbr/cmake/FindDLib.cmake b/share/openbr/cmake/FindDLib.cmake new file mode 100644 index 0000000..01a86c0 --- /dev/null +++ b/share/openbr/cmake/FindDLib.cmake @@ -0,0 +1,9 @@ +find_path(DLib_DIR dlib ${CMAKE_SOURCE_DIR}/3rdparty/*) + +message(${DLib_DIR}) + +mark_as_advanced(DLib_DIR) +include_directories(${DLib_DIR}) +link_directories(${DLib_DIR}/examples/build/dlib_build) + +set(DLib_LIBS dlib) \ No newline at end of file -- libgit2 0.21.4