【问题标题】:How to rewrite Dlib's Python code in C++?如何用 C++ 重写 Dlib 的 Python 代码?
【发布时间】:2019-09-04 05:31:24
【问题描述】:

我已经使用 dlib 编写了一个 python 代码,但是由于项目规范,我需要用 C++ 重写相同的代码。几乎都完成了,但最重要的是我在 C++ 中找不到完全等价的。

python版本是:

[boxes, confidences, detector_idxs] = dlib.fhog_object_detector.run_multiple(detectors, image, upsample_num_times=1, adjust_threshold=0.0)

我尝试过的 C++ 是:

vector<rectangle> detection = evaluate_detectors(detectors, img, adjust_threshold);

我仍然没有运行代码,但我不确定我在 C++ 中使用的这个函数是否会像在 python 中一样返回三个信息(框、置信度、检测器 IDX),据我所见,我认为它只会返回盒子。

你知道我的做法是否正确吗?如何获取我需要的这三个信息?

编辑 01: 使用函数的错误:

tuple<std::vector<dlib::rectangle>, list<T> confidences>, list<T> detector_idxs> = run_multiple_rect_detectors(detectores, img, upsampling_amount, adjust_threshold);

error C2065: 'T': undeclared identifier

error C2923: 'std::list': 'T' is not a valid template type argument for parameter '_Ty'

error C2903: 'allocator': symbol is neither a class template nor a function template

error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type

error C2146: syntax error: missing '>' before identifier 'confidences'

error C2059: syntax error: ','

编辑 01-1:

error C2872: 'rectangle': ambiguous symbol
note: could be 'dlib::rectangle'
note: or       'rectangle'
error C2146: syntax error: missing '>' before identifier 'rectangles'
error C2653: 'pybind11': is not a class or namespace name
error C3861: 'run_multiple_rect_detectors': identifier not found

修复VS显示的语法错误后:

error C3861: 'run_multiple_rect_detectors': identifier not found

编辑 02:

Error   C2027   use of undefined type 'dlib::image_traits<image_type>'
Error   C2146   syntax error: missing ';' before identifier 'pixel_type'
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int

【问题讨论】:

  • 您可以检查 Python 绑定的源代码并查看它委托给什么。
  • 你应该看看C++ HOG object detector example。对于 C++ 标准,它非常简单。
  • 为什么要重写有效的代码?从 C++ 调用 python 代码。
  • 我也在尝试在 C++ 中使用 FHOG,但是我在变量类型方面遇到了一些问题,它不像在 python 中那么容易
  • @NicolasZ。 DLib 是一个高度模板化的库。我建议先看看 C++ 模板。 C++ 与 Python 大不相同。

标签: python c++ dlib


【解决方案1】:

run_multiple(...) 看起来像它的调用 run_multiple_rect_detectors(...), 假设是,您应该可以调用:

std::tuple<std::vector<rectangle> rectangles, std::vector<double> confidences, std::vector<unsigned long> detector_idxs> detection = dlib::run_multiple_rect_detectors(detectors, img, 1, 0.0);

1 是上采样量,0.0 是阈值。

因为 pybind 会自动在 C++11 和 Python 元组之间进行转换。

然后您可以使用 std::vector rectangles = detection.first() 将元组分成不同的向量,以此类推,用于 detection.second() 和 detection.third()。

【讨论】:

  • 谢谢,可惜没用,我在VS2017上调用这个函数有几个错误
  • 这段代码是美化的伪代码;我不知道confidencesdetector_idxs 的类型,也不知道函数的参数。确保针对 pybind 库的编译成功。此外,如果您使用堆栈跟踪编辑问题,我很乐意提供帮助。
  • 抱歉,回答的时间太长,我现在编辑问题
  • 我已经测试了你的代码 jackw11111,但是我在尝试运行它时遇到了一些错误,我用它编辑了我的问题(EDIT 01-1)。我已经修复了 VS 显示的一些语法错误,但是我无法从任何头文件调用函数 run_multiple_rect_detectors(),我已经尝试了很多。我在网上看,我已经看到我尝试过的 c++ 函数 evaluate_detectors() 可能是正确的,但不幸的是我在运行它时遇到了错误(EDIT 02)。
  • 我知道了,我为其他对象做了我自己的 get_frontal_face_detector() 版本,我不需要调用外部 svm 文件。我可以肯定地说这比我以前尝试做的要容易。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多