使用dlib :: frontal_face_detector

问题描述

我正在使用最新版本的dlib,并尝试构建一个使用其dlib::frontal_face_detectordlib::shape_predictor predictor检测面部的DLL。然而, 尝试通过简单地使用detector来实现:

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

我收到此编译错误

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 

这是代码的样子:

#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;
    }
}

我在Visual Studio 2019中与libtorch1.6一起使用OpenCV3.4.11。语言设置为C ++ 17。 我的C ++预处理器中也有这些:

NDEBUG
_CONSOLE
_SILENCE_EXPERIMENTAL_FILESYstem_DEPRECATION_WARNING
_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
_CRT_SECURE_NO_WARNINGS

我正在使用dlib 19.21,并且包括了它的include路径,并将dlib source.cpp添加到我的dll项目中,并像这样构建我的项目。 这些也是我的构建选项:

/D_TEST_BUILD_DLL  /bigobj 

为什么我会看到此错误?我在这里想念什么?

更新:

在不使用任何其他库(例如手电筒)的情况下,这种情况再次发生。因此,这与其他任何库都没有关系。这是纯dlib / opencv!

解决方法

该错误有点令人误解,该问题是由于将cv::Matdlib::get_frontal_face_detector()对象提供了dlib::shape_predictor导致的,导致了所述错误。将其转换为预期的类型可以解决此问题:

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);

更具体地说,当我们将输入图像转换为灰度图像时,我们可以简单地使用cv_image<unsigned char>来代替:

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

旁注

实际上,我花了接下来的14个小时才能达到最初发布的相同解决方案,因为我完全错过了对dlib::shape_predictor的输入,因为当我将检测器的输入从cv::Mat更改为{{1 }},错误仍然存​​在!我认为情况并非如此,因此开始了长达14小时的马拉松比赛,以不同的口味进行构建和重建,并使用不同的方法最终达到这一点!

根据经验,请提供所有与dlib相关的方法,即dlib array2d!我不知道为什么在这种情况下它不发出编译错误。 如果dlib在这种情况下专门发出错误,我们就不会遇到此类问题。

以下是示例方法,可以用来构建dlib并在Visual Studio中使用它(在我的情况下,我使用它来创建DLL):
Part1-Building Dlib using Visual Studio 2019

Part2-Using it in a DLL project