tensorflow: opencv imported after tensorflow can't read jpeg

Environment info

Operating System: Ubuntu 14.04.

If installed from sources, provide the commit hash: f952246b17562

Steps to reproduce

$ python2 -c "import tensorflow; import cv2; print cv2.imread('cat.jpg')"
None

What have you tried?

import cv2 before tensorflow works.

This problem also exists on CentOS 7. It doesn’t happen on a latest ArchLinux. It’s likely due to that tensorflow has it’s own libjpeg that conflicts with opencv on some platforms.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 54 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@beniz the linker script defines what symbols in libtensorflow.so will be exported: in this case it exports symbols that match the pattern *tensorflow* and keeps everything else (eg. everything from libpng and libjpeg) internal.

I think all you need to do is add this to the libtensorflow.so Bazel rule: linkopts = [ "-Wl,--version-script=tensorflow/tf_version_script.lds" ], Then rebuild libtensorflow.so.

Have the same issue with tensorflow1.4 and opencv3.3.1

The problem still exists in 1.4.1. The fix for version 1.3 mentioned above no longer works.

A workaround I’ve done is that I loaded the image with an external library (stb_image) and then fed the data to the cv::Mat like so:

#include <opencv2/opencv.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

std::map<int, int> channels_map = {
	{1, CV_8UC1},
	{3, CV_8UC3},
	{4, CV_8UC4}
};

int width, height, channels;
std::uint8_t* image_data = stbi_load(file_name.c_str(), &width, &height, &channels, 0);
cv::Mat img(height, width, channels_map[channels], image_data); // You must free the image_data yourself

Hope that helps.

@chenbiaolong copts is only used when compiling; you need to use the linker script with the linker so it really needs to be in linkopts. Maybe you can’t have a space between -Wl, and --version-script...