我尝试使用opencv加载图像并使用tensorflow框架对其进行进一步处理。不幸的是,我的行为很奇怪:

使用Bazel中的cc_binary(...)可以毫无问题地加载图像。将其更改为tf_cc_binary(...)不会停止代码的编译或运行,但是opencv无法再加载任何图像。

load("//tensorflow:tensorflow.bzl", "tf_cc_binary")

#tf_cc_binary( <-- using this, no image could be loaded anymore
cc_binary(
    name = "main",
    srcs = ["main.cpp"],
    linkopts = [
        "-lopencv_core",
        "-lopencv_highgui",
        "-lopencv_imgcodecs",
        "-lopencv_imgproc",
    ],
    visibility=["//visibility:public"]
)

我使用来自opencv网站的标准示例代码。同样,它正在运行,并且使用cc_binary(加载了图像:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat image;
    image = imread("tensorflow/test/imageHolder/data/example.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

我发现了tf_cc_binary(...)的定义:
# Links in the framework shared object
# (//third_party/tensorflow:libtensorflow_framework.so) when not building
# statically. Also adds linker options (rpaths) so that the framework shared
# object can be found.
def tf_cc_binary(name,
                 srcs=[],
                 deps=[],
                 linkopts=[],
                 **kwargs):
  native.cc_binary(
      name=name,
      srcs=srcs + _binary_additional_srcs(),
      deps=deps + if_mkl(
          [
              "//third_party/mkl:intel_binary_blob",
          ],
      ),
      linkopts=linkopts + _rpath_linkopts(name),
      **kwargs)

我什至不完全知道问题是什么。我该如何解决?

最佳答案

tf_cc_binary只是围绕cc_binary的宏,用于添加一些特定于Tensorflow的属性(从代码中,我看到它添加了//tensorflow:libtensorflow_framework.so和一些rpaths。我不知道是什么原因导致了您的问题,但我有2个嫌疑犯:

  • 您依赖于libopencv_core,但是bazel不知道它是一个依赖项(这是解决此问题的好线程:https://groups.google.com/forum/#!topic/bazel-discuss/Ndd820uaq2U)
  • 如果您没有使用bazel runbazel test运行测试,请忽略此操作。您阅读了tensorflow/test/imageHolder/data/example.jpg,但是bazel仍然不知道此文件,因此您应该将其添加为data依赖项。

  • 我要进一步调试的方法是,使用sandbox_debug运行bazel,以查看bazel将哪些文件复制到沙箱,并使用--subcommands来运行bazel,以查看bazel生成了哪些命令行,并查看了哪些更改。您还可以在tf_cc_binary宏实现中以增量方式添加/删除属性,以查看是什么触发了问题。

    关于c++ - tf_cc_binary()使opencv无法加载图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46394316/

    10-12 19:08
    查看更多