tensorflow: opencv cannot read any image with tensorflow

It is the same issue as #1924, since the bug is closed, I open a new one, because this bug haven’t been solved yet.

From subashp

I am using the TF 1.4 and linking against C++ code. Below code always says it failed to read the file. I have incorporated above suggestions and it doesnt make any difference. Thoughts/suggestions?

cv::String pathImg = argv[1];
cv::Mat img = cv::imread(pathImg, CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH);
if (img.empty()) {
error("Failed to read the file {}", argv[1]);
return -1;
}

From rmmal

we cloned the latest version of tensorflow and still there is a problem of reading images using opencv , everytime i initiate a TENSOR object the opencv doesn’t work.

From me, using tensor1.4, build from source

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

//unable to read any image if I include this header, no matter
//it before or after opencv
#include <tensorflow/cc/ops/const_op.h>

#include <iostream>

int main(int argc, char *argv[])
{
    cv::Mat input_mat = cv::imread(argv[1], cv::IMREAD_COLOR);
    std::cout<<argv[1]<<", size:"<<input_mat.size()<<std::endl;    
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 40 (16 by maintainers)

Most upvoted comments

@bitzy Right, that’s a symbol conflict. My original comments in this thread were incorrect; you do need libtensorflow_framework.so for protocol buffers right now, and that’s the one with symbols which conflict with OpenCV. The eventual solution is going to be for us to split out a third shared object with just the protocol buffer symbols for use with the C++ API (which I believe @gunan is considering anyway).

In the meantime, as long as you’re not using any custom ops you can build libtensorflow_cc.so with bazel build --config=monolithic, which will condense everything together into one shared object (no libtensorflow_framework dependence) and seal off non-TensorFlow symbols. That shared object will have protocol buffer symbols.

@allenlavoie Me neither. Whatever, we prefer another tool for our jobs.

Tensorflow is the most popular deep learning library, got rich features, huge community, support by the most brilliant engineers hired by google, amazing tensorboard, tensorflow have a lot of pros, but it got obvious drawback too(no perfect library, just pick the one suit your need)

  1. Very complicated dependencies and build system, I do not know why google prefer bazel but not cmake, cmake is mature and seldom break backward compatibility, but bazel do, it do not looks like a mature tool compare with cmake

  2. Api of tensroflow are far too complicated compared with keras and pytorch.Especially pytorch, it is much easier to debug and describe complicated network architectures by pytorch, everything are modularize and works just like numpy and the good old python way, you do not need to learn tons of new concepts when using pytorch, just some basic and you are good to go.

  3. Deployment steps are non-trivial and not yet mature, like the bugs we encountered in this post

Almost everything in tensorflow need us spend lots of times to pick up new concepts, tons of times to study, figure out how to use it properly. Tensorflow keep telling us “deep learning is super hard”, while keras and pytorch show us how easy deep learning could be.

I do not know what the other thinks, these are the major drawback I found in tensorflow.

@allenlavoie It’s wired! I’m using opencv 2.4.13.2. I cann’t use imread to load image if I add the tensorflow’s include file path & link lib to the compile command.

But when I’m using opencv 3.2.0. I can add the tensorflow’s include path & link lib to the compile command and load file normally except the circumstance that I defined SessionOptions sessOptions; variable.

opencv 3.2.0

  • link option:-lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -ltensorflow_cc -ltensorflow_framework; ==> connot imread image;
  • link option: -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -ltensorflow_cc; ==>can imread image; but SessionOptions sessOptions; included in my source code, the compile gets error: /usr/bin/ld: /tmp/ccCMSFkT.o: undefined reference to symbol ‘_ZN10tensorflow14SessionOptionsC1Ev’ //usr/lib/libtensorflow_framework.so: error adding symbols: DSO missing from command line.

opencv 2.4.13.2

  • link option: -lopencv_core -lopencv_highgui -lopencv_imgproc -ltensorflow_cc -ltensorflow_framework; ==> connot imread image;
  • link option: -lopencv_core -lopencv_highgui -lopencv_imgproc -ltensorflow_cc ==>the same as opencv3.2.0 link without tensorflow_framework.

So maybe the problem is tensorflow_framework.so.

Hi! I meet the same problem. I’m using opencv3.1 tensorflow1.4 in ubuntu14.04. When I included tensorflow headers like #include <tensorflow/core/public/session.h> or #include “tensorflow/cc/ops/standard_ops.h”, cv::imread can not read images encoded by JPEG, but can read other encoding images like Uncompressed 8-bit RGB. When I commented TF headers, I can read any images by cv::imread. Is there any solution? Thanks a lot!!! @allenlavoie @bitzy

update: I rebuild libtensorflow_cc.so using command bazel build --config=monolithic :libtensorflow_cc.so , and can read any images successfully. Thank you @allenlavoie . Though the problem appear to have been solved, I have some questions below: 1.What is the root cause of this problem?(libjpeg version in opencv and tensorflow conficts? or other reason? ) 2.Since there is no libtensorflow_framework, are there some functions that I can’t use? 3.Could we get a new TF version in the future, which can solve this problem?