Hossein
Hossein

Reputation: 26004

Getting C2027 use of undefined type 'dlib::image_traits<image_type>' error when using dlib::frontal_face_detector

I am using the latest version of dlib and trying to build a DLL that detects faces using its dlib::frontal_face_detector and dlib::shape_predictor predictor. However, upon trying to use the detector by simply doing :

auto faces = this->detector(img, 0);

I get this compilation error :

Severity    Code    Description Project File    Line    Suppression State
Error   C2146   syntax error: missing ';' before identifier 'pixel_type'    Test    D:\Codes\Dependencies\include\dlib\image_processing\scan_fhog_pyramid.h 601 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    Test    D:\Codes\Dependencies\include\dlib\image_processing\scan_fhog_pyramid.h 601 
Error   C2027   use of undefined type 'dlib::image_traits<image_type>'  Test    D:\Codes\Dependencies\include\dlib\image_processing\scan_fhog_pyramid.h 601 

This is how the code looks like :

#include <iostream>
#include <tuple>
#include <chrono>
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
#include <functional>

//#include <torch/torch.h>
//#include <torch/script.h>


#include <dlib/opencv.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>

//#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>

class TEST_API Test
{
public:
    Test(std::string shape_predictor_path = "");
    void main_op(cv::Mat img_ori, bool show_debug_info = true);
    ~Test();

private:
    dlib::frontal_face_detector detector;
    dlib::shape_predictor predictor;
    //torch::jit::script::Module net;
};
//in Test.cpp
#include <Test/Test.h>
Test::Test(std::string shape_predictor_path)
{
    auto cdir = fs::current_path();
    detector = dlib::get_frontal_face_detector();

    auto landmark_path = shape_predictor_path == "" ? (cdir / "shape_predictor_68_face_landmarks.dat").string() : shape_predictor_path;
    auto model_path = (cdir / "net_4.jit").string();
    std::cout << "landmark path: " << landmark_path << "model path: " << model_path << std::endl;
    dlib::deserialize(landmark_path) >> this->predictor;
    //this->net = torch::jit::load(model_path);
}
void Test::main_op(cv::Mat img, bool show_debug_info)
{
    try
    {
        //cv::Mat img_cpy(img);
        //grayscale
        cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
        auto faces = this->detector(img, 0);
         //....
    }
    catch(std::exception exp)
    {
        std::cout << exp.what() << std::endl;
    }
}

I'm using OpenCV3.4.11 along-side libtorch1.6 in Visual Studio 2019. and The language is set to C++17. I also have these in my C++ preprocessors :

NDEBUG
_CONSOLE
_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
_CRT_SECURE_NO_WARNINGS

I'm using dlib 19.21 and I included its include path and added dlibs source.cpp to the dll project of mine and build my project like this. These are also my build options :

/D_TEST_BUILD_DLL  /bigobj 

Why am I seeing this error? What am I missing here?

Update:

Without using any other libraries such as torch, this happens again. so this is not related to any other libraries. this is pure dlib/opencv!

Upvotes: 1

Views: 656

Answers (1)

Hossein
Hossein

Reputation: 26004

The error was kind of misleading, The issue was caused by feeding a cv::Mat to the dlib::get_frontal_face_detector() and dlib::shape_predictor objects, resulting in the stated error. Converting it to the expected type solved the issue:

dlib::array2d<dlib::rgb_pixel> img_dlib;
dlib::assign_image(img_dlib, dlib::cv_image<dlib::bgr_pixel>(img));
auto faces = this->detector(img_dlib, 0);

More specifically as we were converting the input image into grayscale we can simply use cv_image<unsigned char> instead:

dlib::array2d<unsigned char> img_dlib;
dlib::assign_image(img_dlib, dlib::cv_image<unsigned char>(img));
auto faces = this->detector(img_dlib, 0);

Side note

It actually took me the next 14 hours to reach the same solution I initially posted because I completely missed the input to the dlib::shape_predictor, since when I changed the detectors input from cv::Mat to dlib::array2d and the error still remained! I thought that wasn't the case and started a 14 hours marathon of building and rebuilding in different flavors and using different approaches to ultimately reach this point!

So as a rule of thumb, feed all dlib related methods, the dlib array2d! I have no idea why it does not issue a compilation error when this happens. Were dlib to specifically issue an error in such cases, we wouldn't be facing such issues.

Here is the sample approach one can take to build dlib and use it in a Visual Studio (in my case, I used it to create a DLL):
Part1-Building Dlib using Visual Studio 2019

Part2-Using it in a DLL project

Upvotes: 2

Related Questions