发布
社区首页 >问答首页 >使C2027在使用dlib::frontal_face_detector时使用未定义类型的“dlib::image_traits<image_type>”错误

使C2027在使用dlib::frontal_face_detector时使用未定义类型的“dlib::image_traits<image_type>”错误
EN

Stack Overflow用户
提问于 2020-08-15 07:28:47
回答 1查看 380关注 0票数 0

我正在使用dlib的最新版本,并试图构建一个使用其dlib::frontal_face_detectordlib::shape_predictor predictor检测人脸的DLL。但是,在尝试使用detector时,只需执行以下操作:

代码语言:javascript
代码运行次数:0
复制
auto faces = this->detector(img, 0);

我得到了这个编译错误:

代码语言:javascript
代码运行次数: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 

代码如下所示:

代码语言:javascript
代码运行次数:0
复制
#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;
};
代码语言:javascript
代码运行次数:0
复制
//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 2019中使用OpenCV3.4.11和libtorch1.6。该语言被设置为C++17。我的C++预处理程序中也有这些:

代码语言:javascript
代码运行次数:0
复制
NDEBUG
_CONSOLE
_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
_CRT_SECURE_NO_WARNINGS

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

代码语言:javascript
代码运行次数:0
复制
/D_TEST_BUILD_DLL  /bigobj 

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

更新:

如果不使用任何其他库(如torch ),就会再次发生这种情况。因此,这与任何其他库无关。这是纯的dlib/opencv!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-15 07:54:35

这个错误有点误导性,这个问题是由向cv::Matdlib::shape_predictor对象提供一个dlib::get_frontal_face_detector()dlib::shape_predictor对象造成的,从而导致了声明的错误。将其转换为预期类型解决了这个问题:

代码语言:javascript
代码运行次数:0
复制
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>

代码语言:javascript
代码运行次数:0
复制
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中使用它的示例方法(在我的例子中,我使用它创建一个DLL):

第1部分-使用Visual 2019构建Dlib

第2部分-在DLL项目中使用它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63423659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档