[shotwell/wip/cleanup-facedetect: 2/6] Use original filename of haarcascade




commit 250ba96ff0c5e6f84059c2123d5863516b46f18e
Author: Jens Georg <mail jensge org>
Date:   Sun Aug 14 17:32:02 2022 +0200

    Use original filename of haarcascade
    
    So that it is somewhat understandable in future

 .../shotwell-facedetect/facedetect-opencv.cpp      | 29 ++++++++++++++++------
 ...cascade.xml => haarcascade_frontalface_alt.xml} |  0
 subprojects/shotwell-facedetect/meson.build        |  2 +-
 .../org.gnome.ShotwellFaces1.xml                   |  5 ++--
 4 files changed, 25 insertions(+), 11 deletions(-)
---
diff --git a/subprojects/shotwell-facedetect/facedetect-opencv.cpp 
b/subprojects/shotwell-facedetect/facedetect-opencv.cpp
index 209f9e6d..6a3efa9d 100644
--- a/subprojects/shotwell-facedetect/facedetect-opencv.cpp
+++ b/subprojects/shotwell-facedetect/facedetect-opencv.cpp
@@ -16,21 +16,26 @@ static cv::dnn::Net faceRecogNet;
 static cv::dnn::Net faceDetectNet;
 #endif
 
+static cv::CascadeClassifier cascade;
+static bool disableDnn{ true };
+
 constexpr char OPENFACE_RECOG_TORCH_NET[]{ "openface.nn4.small2.v1.t7" };
 constexpr char RESNET_DETECT_CAFFE_NET[]{ "res10_300x300_ssd_iter_140000_fp16.caffemodel" };
+constexpr char HAARCASCADE[]{ "haarcascade_frontalface_alt.xml" };
 
 std::vector<cv::Rect> detectFacesMat(cv::Mat img);
 std::vector<double> faceToVecMat(cv::Mat img);
 
 // Detect faces in a photo
 std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName, double scale, bool infer = 
false) {
-    cv::CascadeClassifier cascade;
-       if (!cascade.load(cascadeName)) {
-        std::cout << "error;Could not load classifier cascade. Filename: \"" << cascadeName << "\"" << 
std::endl;
-       }
+    if(cascade.empty()) {
+        g_warning("No cascade file loaded. Did you call loadNet()?");
+        return {};
+    }
 
        if (inputName.empty()) {
-        std::cout << "error;You must specify the file to process." << std::endl;
+        g_warning("No file to process. aborting");
+        return {};
        }
 
     cv::Mat img = cv::imread(inputName, 1);
@@ -40,7 +45,6 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
 
     std::vector<cv::Rect> faces;
     cv::Size smallImgSize;
-    static bool disableDnn;
 
 #ifdef HAS_OPENCV_DNN
     disableDnn = faceDetectNet.empty();
@@ -89,6 +93,12 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
 
 // Load network into global var
 bool loadNet(cv::String baseDir) {
+    cascade.load(baseDir + "/" + HAARCASCADE);
+    if(cascade.empty()) {
+        g_warning("Failed to load haarcascade file: %s/%s", baseDir.c_str(), HAARCASCADE);
+    } else {
+        g_debug("Successfully loaded haarcascade %s/%s", baseDir.c_str(), HAARCASCADE);
+    }
 #ifdef HAS_OPENCV_DNN
     try {
         faceDetectNet = cv::dnn::readNetFromCaffe(baseDir + "/deploy.prototxt",
@@ -96,16 +106,19 @@ bool loadNet(cv::String baseDir) {
         faceRecogNet = cv::dnn::readNetFromTorch(baseDir + "/" + OPENFACE_RECOG_TORCH_NET);
     } catch(cv::Exception &e) {
         std::cout << "File load failed: " << e.msg << std::endl;
-        return false;
+        disableDnn = true;
     }
+
     if (faceRecogNet.empty() || faceDetectNet.empty()) {
         std::cout << "Loading open-face net failed!" << std::endl;
+        disableDnn = true;
         return false;
     } else {
+        disableDnn = false;
         return true;
     }
 #else
-    return true;
+    return not cascade.empty();
 #endif
 }
 
diff --git a/subprojects/shotwell-facedetect/facedetect-haarcascade.xml 
b/subprojects/shotwell-facedetect/haarcascade_frontalface_alt.xml
similarity index 100%
rename from subprojects/shotwell-facedetect/facedetect-haarcascade.xml
rename to subprojects/shotwell-facedetect/haarcascade_frontalface_alt.xml
diff --git a/subprojects/shotwell-facedetect/meson.build b/subprojects/shotwell-facedetect/meson.build
index 77adb5b7..3cbe5e32 100644
--- a/subprojects/shotwell-facedetect/meson.build
+++ b/subprojects/shotwell-facedetect/meson.build
@@ -42,7 +42,7 @@ executable('shotwell-facedetect',
            install : true,
            include_directories: config_incdir,
            install_dir : libexecdir)
-install_data('facedetect-haarcascade.xml',
+install_data('haarcascade_frontalface_alt.xml',
               install_dir : join_paths(get_option('datadir'), 'shotwell'))
 
 configure_file(
diff --git a/subprojects/shotwell-facedetect/org.gnome.ShotwellFaces1.xml 
b/subprojects/shotwell-facedetect/org.gnome.ShotwellFaces1.xml
index 2391bf6f..3bf10717 100644
--- a/subprojects/shotwell-facedetect/org.gnome.ShotwellFaces1.xml
+++ b/subprojects/shotwell-facedetect/org.gnome.ShotwellFaces1.xml
@@ -10,8 +10,9 @@
     <!--
         DetectFaces
         @image: Image file to run face detection on
-        @cascade: Cascade XML file
+        @cascade: Cascade XML file - unused
         @scale: Scaling to apply on image
+        @infer: Provide an
         Returns an array of face bounding boxes (x,y,w,h) in dimensionless units
     -->
     <method name="DetectFaces">
@@ -24,7 +25,7 @@
 
     <!--
         LoadNet
-        @net: Torch t7 net file
+        @net: path to folder containing the DNN
         Returns non-zero on any error
     -->
     <method name="LoadNet">


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