[shotwell/wip/phako/enhanced-faces: 96/136] Added ifdef protection for OpenCV dnn usage



commit 41da9cc11c4344a3ac317013fd4450d1df1673fa
Author: NarendraMA <narendra_m_a yahoo com>
Date:   Sat Sep 15 13:35:03 2018 +0530

    Added ifdef protection for OpenCV dnn usage

 facedetect/facedetect-opencv.cpp   | 31 ++++++++++++++++++++++++++++---
 facedetect/meson.build             |  9 ++++++++-
 facedetect/shotwell-facedetect.hpp |  4 ++++
 3 files changed, 40 insertions(+), 4 deletions(-)
---
diff --git a/facedetect/facedetect-opencv.cpp b/facedetect/facedetect-opencv.cpp
index b80a4aab..fc2f7016 100644
--- a/facedetect/facedetect-opencv.cpp
+++ b/facedetect/facedetect-opencv.cpp
@@ -21,7 +21,14 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
 
     std::vector<cv::Rect> faces;
     cv::Size smallImgSize;
-    if (faceDetectNet.empty()) {
+    static bool disableDnn;
+
+#ifdef HAS_OPENCV_DNN
+    disableDnn = faceDetectNet.empty();
+#else
+    disableDnn = true;
+#endif
+    if (disableDnn) {
         // Classical face detection
         cv::Mat gray;
         cvtColor(img, gray, CV_BGR2GRAY);
@@ -34,9 +41,11 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
 
         cascade.detectMultiScale(smallImg, faces, 1.1, 2, CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));
     } else {
+#ifdef HAS_OPENCV_DNN
         // DNN based face detection
         faces = detectFacesMat(img);
         smallImgSize = img.size(); // Not using the small image here
+#endif
     }
 
     std::vector<FaceRect> scaled;
@@ -46,6 +55,7 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
         i.y = (float) r->y / smallImgSize.height;
         i.width = (float) r->width / smallImgSize.width;
         i.height = (float) r->height / smallImgSize.height;
+#ifdef HAS_OPENCV_DNN
         if (infer && !faceRecogNet.empty()) {
             // Get colour image for vector generation
             cv::Mat colourImg;
@@ -54,6 +64,9 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
         } else {
             i.vec.assign(128, 0);
         }
+#else
+        i.vec.assign(128, 0);
+#endif
         scaled.push_back(i);
     }
 
@@ -62,6 +75,7 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
 
 // Load network into global var
 bool loadNet(cv::String baseDir) {
+#ifdef HAS_OPENCV_DNN
     try {
         faceDetectNet = cv::dnn::readNetFromCaffe(baseDir + "/deploy.prototxt",
                                                   baseDir + "/" + RESNET_DETECT_CAFFE_NET);
@@ -76,17 +90,21 @@ bool loadNet(cv::String baseDir) {
     } else {
         return true;
     }
+#else
+    return false;
+#endif
 }
 
 // Face detector
 // Adapted from OpenCV example:
 // https://github.com/opencv/opencv/blob/master/samples/dnn/js_face_recognition.html
 std::vector<cv::Rect> detectFacesMat(cv::Mat img) {
+    std::vector<cv::Rect> faces;
+#ifdef HAS_OPENCV_DNN
     cv::Mat blob = cv::dnn::blobFromImage(img, 1.0, cv::Size(128*8, 96*8),
                                           cv::Scalar(104, 177, 123, 0), false, false);
     faceDetectNet.setInput(blob);
     cv::Mat out = faceDetectNet.forward();
-    std::vector<cv::Rect> faces;
     // out is a 4D matrix [1 x 1 x n x 7]
     // n - number of results
     assert(out.dims == 4);
@@ -111,12 +129,14 @@ std::vector<cv::Rect> detectFacesMat(cv::Mat img) {
             faces.push_back(rect);
         }
     }
+#endif // HAS_OPENCV_DNN
     return faces;
 }
 
 // Face to vector convertor
 // Adapted from OpenCV example:
 // https://github.com/opencv/opencv/blob/master/samples/dnn/js_face_recognition.html
+#ifdef HAS_OPENCV_DNN
 std::vector<double> faceToVecMat(cv::Mat img) {
     std::vector<double> ret;
     cv::Mat smallImg(96, 96, CV_8UC1);
@@ -133,6 +153,7 @@ std::vector<double> faceToVecMat(cv::Mat img) {
         ret.insert(ret.end(), vec.ptr<float>(i), vec.ptr<float>(i) + vec.cols);
     return ret;
 }
+#endif
 
 std::vector<double> faceToVec(cv::String inputName) {
     std::vector<double> ret;
@@ -141,8 +162,12 @@ std::vector<double> faceToVec(cv::String inputName) {
         std::cout << "error;Could not load the file to process. Filename: \"" << inputName << "\"" << 
std::endl;
         ret.assign(128, 0);
         return ret;
-       }
+    }
+#ifdef HAS_OPENCV_DNN
     ret = faceToVecMat(img);
+#else
+    ret.assign(128, 0);
+#endif
     return ret;
 }
 
diff --git a/facedetect/meson.build b/facedetect/meson.build
index f1931e37..a19ffc0c 100644
--- a/facedetect/meson.build
+++ b/facedetect/meson.build
@@ -2,13 +2,20 @@ gnome = import('gnome')
 subproject = ('facedetect')
 add_languages('cpp')
 facedetect_dep = dependency('opencv', version : ['>= 2.3.0'], required : true)
+cpp = meson.get_compiler('cpp')
+has_dnn = cpp.has_header('opencv2/dnn.hpp', dependencies: facedetect_dep)
+if has_dnn
+  dnn_define = declare_dependency(compile_args: '-DHAS_OPENCV_DNN')
+else
+  dnn_define = []
+endif
 gio_unix = dependency('gio-unix-2.0', required : true)
 gdbus_src = gnome.gdbus_codegen('dbus-interface',
   sources: 'org.gnome.ShotwellFaces1.xml',
   interface_prefix : 'org.gnome.')
 executable('shotwell-facedetect',
            'shotwell-facedetect.cpp', 'facedetect-opencv.cpp', gdbus_src,
-           dependencies : [facedetect_dep, gio, gio_unix],
+           dependencies : [facedetect_dep, gio, gio_unix, dnn_define],
            install : true,
            include_directories: config_incdir,
            install_dir : join_paths(get_option('libexecdir'), 'shotwell'))
diff --git a/facedetect/shotwell-facedetect.hpp b/facedetect/shotwell-facedetect.hpp
index 0c138c6f..2d167622 100644
--- a/facedetect/shotwell-facedetect.hpp
+++ b/facedetect/shotwell-facedetect.hpp
@@ -11,7 +11,9 @@
 #include <opencv2/objdetect/objdetect.hpp>
 #include <opencv2/highgui/highgui.hpp>
 #include <opencv2/imgproc/imgproc.hpp>
+#ifdef HAS_OPENCV_DNN
 #include <opencv2/dnn.hpp>
+#endif
 
 #include <iostream>
 #include <stdio.h>
@@ -23,8 +25,10 @@ typedef struct {
 } FaceRect;
 
 // Global variable for DNN to generate vector out of face
+#ifdef HAS_OPENCV_DNN
 static cv::dnn::Net faceRecogNet;
 static cv::dnn::Net faceDetectNet;
+#endif
 
 bool loadNet(cv::String netFile);
 std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName, double scale, bool infer);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]