[shotwell] Make face detection compatible with OpenCV4
- From: Jens Georg <jensgeorg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [shotwell] Make face detection compatible with OpenCV4
- Date: Sun, 15 Mar 2020 09:24:04 +0000 (UTC)
commit d943f6a126d04fefbf0ef08f8d2d1fa9188c998a
Author: Jens Georg <mail jensge org>
Date: Sun Mar 15 10:22:40 2020 +0100
Make face detection compatible with OpenCV4
Fixes #223
.../shotwell-facedetect/facedetect-opencv.cpp | 32 +++++++++++++++-------
subprojects/shotwell-facedetect/meson.build | 20 ++++++++++----
.../shotwell-facedetect/shotwell-facedetect.hpp | 4 +--
3 files changed, 38 insertions(+), 18 deletions(-)
---
diff --git a/subprojects/shotwell-facedetect/facedetect-opencv.cpp
b/subprojects/shotwell-facedetect/facedetect-opencv.cpp
index 70669e30..41439a34 100644
--- a/subprojects/shotwell-facedetect/facedetect-opencv.cpp
+++ b/subprojects/shotwell-facedetect/facedetect-opencv.cpp
@@ -5,6 +5,18 @@
#define OPENFACE_RECOG_TORCH_NET "openface.nn4.small2.v1.t7"
#define RESNET_DETECT_CAFFE_NET "res10_300x300_ssd_iter_140000_fp16.caffemodel"
+#ifndef HAVE_OPENCV_4
+namespace cv {
+ enum {
+ COLOR_BGR2GRAY = CV_BGR2GRAY;
+ };
+
+ enum {
+ CASCADE_SCALE_IMAGE = CV_HAAR_SCALE_IMAGE;
+ };
+}
+#endif
+
// Detect faces in a photo
std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName, double scale, bool infer =
false) {
cv::CascadeClassifier cascade;
@@ -25,7 +37,7 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
cv::Size smallImgSize;
static bool disableDnn;
-#ifdef HAS_OPENCV_DNN
+#ifdef HAVE_OPENCV_DNN_HPP
disableDnn = faceDetectNet.empty();
#else
disableDnn = true;
@@ -33,17 +45,17 @@ std::vector<FaceRect> detectFaces(cv::String inputName, cv::String cascadeName,
if (disableDnn) {
// Classical face detection
cv::Mat gray;
- cvtColor(img, gray, CV_BGR2GRAY);
+ cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::Mat smallImg(cvRound(img.rows / scale), cvRound(img.cols / scale), CV_8UC1);
smallImgSize = smallImg.size();
cv::resize(gray, smallImg, smallImgSize, 0, 0, cv::INTER_LINEAR);
cv::equalizeHist(smallImg, smallImg);
+ cascade.detectMultiScale(smallImg, faces, 1.1, 2, cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
- cascade.detectMultiScale(smallImg, faces, 1.1, 2, CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));
} else {
-#ifdef HAS_OPENCV_DNN
+#ifdef HAVE_OPENCV_DNN_HPP
// DNN based face detection
faces = detectFacesMat(img);
smallImgSize = img.size(); // Not using the small image here
@@ -57,7 +69,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
+#ifdef HAVE_OPENCV_DNN_HPP
if (infer && !faceRecogNet.empty()) {
// Get colour image for vector generation
cv::Mat colourImg;
@@ -77,7 +89,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
+#ifdef HAVE_OPENCV_DNN_HPP
try {
faceDetectNet = cv::dnn::readNetFromCaffe(baseDir + "/deploy.prototxt",
baseDir + "/" + RESNET_DETECT_CAFFE_NET);
@@ -102,7 +114,7 @@ bool loadNet(cv::String baseDir) {
// 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
+#ifdef HAVE_OPENCV_DNN_HPP
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);
@@ -138,7 +150,7 @@ std::vector<cv::Rect> detectFacesMat(cv::Mat img) {
// 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
+#ifdef HAVE_OPENCV_DNN_HPP
std::vector<double> faceToVecMat(cv::Mat img) {
std::vector<double> ret;
cv::Mat smallImg(96, 96, CV_8UC1);
@@ -159,13 +171,13 @@ std::vector<double> faceToVecMat(cv::Mat img) {
std::vector<double> faceToVec(cv::String inputName) {
std::vector<double> ret;
- cv::Mat img = imread(inputName, 1);
+ cv::Mat img = cv::imread(inputName, 1);
if (img.empty()) {
std::cout << "error;Could not load the file to process. Filename: \"" << inputName << "\"" <<
std::endl;
ret.assign(128, 0);
return ret;
}
-#ifdef HAS_OPENCV_DNN
+#ifdef HAVE_OPENCV_DNN_HPP
ret = faceToVecMat(img);
#else
ret.assign(128, 0);
diff --git a/subprojects/shotwell-facedetect/meson.build b/subprojects/shotwell-facedetect/meson.build
index 493bc04b..19320803 100644
--- a/subprojects/shotwell-facedetect/meson.build
+++ b/subprojects/shotwell-facedetect/meson.build
@@ -1,12 +1,20 @@
project('shotwell-facedetect', ['c', 'cpp'], default_options : ['cpp_std=c++14'])
gnome = import('gnome')
-facedetect_dep = dependency('opencv', version : ['>= 2.3.0'], required : true)
+opencv = dependency('opencv4', version : ['>= 4.0.0'], required : false)
+if opencv.found()
+ add_global_arguments(['-DHAVE_OPENCV_4'], language: 'cpp')
+else
+ opencv = dependency('opencv', version: ['>= 2.3.0'], required : true)
+endif
+
cpp = meson.get_compiler('cpp')
-has_dnn = cpp.has_header('opencv2/dnn.hpp', dependencies: facedetect_dep)
+has_dnn = cpp.has_header('opencv2/dnn.hpp', dependencies: opencv)
+if opencv.found()
+ add_global_arguments(['-DHAVE_OPENCV_4'], language: 'cpp')
+endif
+
if has_dnn
- dnn_define = declare_dependency(compile_args: '-DHAS_OPENCV_DNN')
-else
- dnn_define = []
+ add_global_arguments(['-DHAVE_OPENCV2_DNN_HPP'], language: 'cpp')
endif
libexecdir = join_paths(get_option('libexecdir'), 'shotwell')
@@ -35,7 +43,7 @@ endif
executable('shotwell-facedetect',
'shotwell-facedetect.cpp', 'facedetect-opencv.cpp', gdbus_src,
- dependencies : [facedetect_dep, gio, gio_unix, dnn_define],
+ dependencies : [opencv, gio, gio_unix],
install : true,
include_directories: config_incdir,
install_dir : libexecdir)
diff --git a/subprojects/shotwell-facedetect/shotwell-facedetect.hpp
b/subprojects/shotwell-facedetect/shotwell-facedetect.hpp
index 688a1012..a78a9857 100644
--- a/subprojects/shotwell-facedetect/shotwell-facedetect.hpp
+++ b/subprojects/shotwell-facedetect/shotwell-facedetect.hpp
@@ -10,7 +10,7 @@
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
-#ifdef HAS_OPENCV_DNN
+#ifdef HAVE_OPENCV_DNN_HPP
#include <opencv2/dnn.hpp>
#endif
@@ -24,7 +24,7 @@ typedef struct {
} FaceRect;
// Global variable for DNN to generate vector out of face
-#ifdef HAS_OPENCV_DNN
+#ifdef HAVE_OPENCV_DNN_HPP
static cv::dnn::Net faceRecogNet;
static cv::dnn::Net faceDetectNet;
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]