【问题标题】:Getting C2027 use of undefined type 'dlib::image_traits<image_type>' error when using dlib::frontal_face_detector使用 dlib::frontal_face_detector 时出现 C2027 使用未定义类型 'dlib::image_traits<image_type>' 错误
【发布时间】:2020-12-04 23:37:49
【问题描述】:

我正在使用最新版本的 dlib 并尝试构建一个 DLL,该 DLL 使用其dlib::frontal_face_detector 和dlib::shape_predictor predictor 检测面部。然而, 在尝试通过简单地使用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,我包含了它的包含路径,并将 dlibs source.cpp 添加到我的 dll 项目中,并像这样构建我的项目。 这些也是我的构建选项:

/D_TEST_BUILD_DLL  /bigobj 

为什么我会看到这个错误?我在这里错过了什么?

更新:

如果不使用任何其他库,例如torch,这种情况会再次发生。所以这与任何其他库无关。这是纯粹的 dlib/opencv!

【问题讨论】:

  • 请提取并提供minimal reproducible example,如果没有它,您的问题将是题外话。
  • 无论是否提供 mre,这个问题都不是题外话。在这种情况下,不需要 imo。万一需要,投票也无济于事。
  • minimal reproducible example 是一种确保问题具有一定质量的工具。您的问题缺乏质量,因为未知代码会导致多个错误。问号太多了,这就是恕我直言不赞成投票的原因。如果您是这里的新用户,我会放过您的,但您在这里的时间足够长,知道这些事情。
  • @UlrichEckhardt,我不会在这里和你争论,因为你刚才提到的同样的原因,我认为没有必要,因为这里显示的大部分代码是实际上主要操作对结果负责。没有一个依赖项会影响相关操作。这是MRE!如果你再看一遍。我刚刚添加了那些extra 信息,以防万一有这样的库冲突。如您所见,问题只是与缺乏适当的转换有关。无论如何,谢谢您的时间。

标签: c++ dlib


【解决方案1】:

该错误具有误导性,该问题是由将 cv::Mat 提供给 dlib::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&lt;unsigned char&gt; 代替:

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 更改为 dlib::array2d 时,错误仍然存​​在留下了!我认为情况并非如此,于是开始了长达 14 小时的马拉松式的建设和重建,以不同的方式并使用不同的方法最终达到这一点!

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

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

Part2-Using it in a DLL project

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2019-06-14
    • 2016-10-04
    相关资源
    最近更新 更多