2017-09-24 155 views
0

我尝试使用opencv加载图像,并使用tensorflow框架对其进行进一步处理。不幸的是我得到了一个非常奇怪的行为:tf_cc_binary()使opencv无法加载图像

该图像在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) 

我甚至不明白到底是什么问题。我该如何解决它?

回答

1

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

  • 你依靠libopencv_core,但巴泽尔不知道这是一个依赖关系(这里是关于这个问题的一个很好的线索:https://groups.google.com/forum/#!topic/bazel-discuss/Ndd820uaq2U)。
  • 忽略这一点,如果你不使用bazel runbazel test运行测试你阅读tensorflow/test/imageHolder/data/example.jpg但是,bazel不知道这个文件,因此您应该将其添加为data依赖项。

我会做进一步调试,这是与sandbox_debug运行巴泽勒看到哪些文件巴泽勒复制到沙箱中,并与--subcommands,看看有什么命令行巴泽勒生成并看看有什么变化。您还可以逐步添加/删除tf_cc_binary宏实现中的属性,以查看哪些更改会触发问题。

+0

你对数据的依赖是什么意思? – Jonas

+0

[this]中指定的依赖关系(https://docs.bazel.build/versions/master/be/common-definitions.html#common.data)属性。 – mhlopko