I'm writing a class that basically wraps the cv::face::FacemarkLBF OpenCV class for face detection and facial landmark detection. As part of this effort, I wrote my own face detector function, which is a class member since it uses parameters that may change at any time. Doing this, I fail to pass this member function to OpenCV successfully.
When the method (face_cascade_impl) is static (with static parameters to cv::CascadeClassifier::detectMultiScale), all is fine (compilation and runtime). However, I need to bind the instance member function now that I want to use class member parameters.
Here is my face_cascade_impl function that I need to pass to the OpenCV FacemarkLBF instance:
bool LBPDetector::face_cascade_impl(cv::InputArray img, cv::OutputArray ROIs, void *data) {
cv::Mat gray;
cv::UMat gray_umat;
std::vector<cv::Rect> faces;
cv::CascadeClassifier *cascade = reinterpret_cast<cv::CascadeClassifier *>(data);
// respect input mat type: separate codepaths for mat/umat
if (img.isUMat()) {
// convert to 8-bit single channel if necessary
if (img.channels() > 1) {
cv::cvtColor(img, gray_umat, cv::COLOR_BGR2GRAY);
cv::equalizeHist(gray_umat, gray_umat);
} else {
cv::equalizeHist(img, gray_umat);
}
} else {
if (img.channels() > 1) {
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::equalizeHist(gray, gray);
} else {
cv::equalizeHist(img, gray);
}
}
// detect faces
if (img.isUMat()) {
cascade->detectMultiScale(gray_umat, faces, m_params.cascade_scale_factor,
m_params.cascade_min_neighbours, m_params.cascade_flags,
m_params.cascade_min_size);
} else {
cascade->detectMultiScale(gray, faces, m_params.cascade_scale_factor,
m_params.cascade_min_neighbours, m_params.cascade_flags,
m_params.cascade_min_size);
}
cv::Mat(faces).copyTo(ROIs);
return true;
};
Said cv::face::FacemarkLBF instance is created in my LBPDetector constructor:
LBPDetector::LBPDetector() {
m_impl = cv::face::FacemarkLBF::create(m_facemark_params);
m_face_cascade = nullptr;
m_facemark_model_loaded = false;
m_params.cascade_scale_factor = 1.1;
m_params.cascade_min_neighbours = 3;
m_params.cascade_flags = cv::CASCADE_SCALE_IMAGE;
m_params.cascade_min_size = cv::Size(30, 30);
}
Now there is another member function that actually initializes that instance and loads a face cascade model:
bool LBPDetector::load_face_cascade(const std::string &path) {
m_facemark_model_loaded = false;
if (path.empty()) {
return false;
}
m_face_cascade = new cv::CascadeClassifier(path);
m_impl->setFaceDetector(std::bind(&LBPDetector::face_cascade_impl, this,
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3), m_face_cascade);
return true;
}
However, the above code fails to compile.
Here's the GCC error message:
lbp_detector.cpp:77:29: error: no viable conversion from 'typename _Bind_helper<__is_socketlike<bool (LBPDetector::*)(const _InputArray &, const _OutputArray &, void *)>::value, bool (LBPDetector::*)(const _InputArray &, const _OutputArray &, void *), LBPDetector *, const _Placeholder<1> &, const _Placeholder<2> &, const _Placeholder<3> &>::type' (aka '_Bind<bool (sph::face::LBPDetector::*(sph::face::LBPDetector *, std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>))(const cv::_InputArray &, const cv::_OutputArray &, void *)>') to 'cv::face::FN_FaceDetector' (aka 'bool (*)(const cv::_InputArray &, const cv::_OutputArray &, void *)')
facemark_train.hpp:351:50: note: passing argument to parameter 'detector' here
My system is Fedora 30 with GCC 9:
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Ziel: x86_64-redhat-linux
Konfiguriert mit: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread-Modell: posix
gcc-Version 9.1.1 20190503 (Red Hat 9.1.1-1) (GCC)
A bound function has state, and as such cannot be converted to a function pointer. You will need a free function or static member function.
Luckily, that third void* userData
parameter exists. You can use that to pass a pointer to your object:
bool LBPDetector::load_face_cascade(const std::string &path) {
//...
m_face_cascade = new cv::CascadeClassifier(path);
m_impl->setFaceDetector(LBPDetector::face_cascade_static, this);
return true;
}
bool LBPDetector::face_cascade_static(cv::InputArray img, cv::OutputArray ROIs, void *data)
{
return reinterpret_cast<LBPDetector*>(data)->face_cascade_impl(img, ROIs);
}
bool LBPDetector::face_cascade_impl(cv::InputArray img, cv::OutputArray ROIs)
{
//...
cv::CascadeClassifier *cascade = m_face_cascade;
//...
}