3

I am trying to follow these instructions inspired from here to compile OpenCV so that it gets integrated within a Conda environment that I previously set. However, when I import cv2 within this conda environment, python3 doesn't find it.

Here are the instructions that I follow to install Opencv :

sudo apt-get update
sudo apt-get upgrade


sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install python3.6-dev python3-numpy libtbb2 libtbb-dev

sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

sudo -s
cd /opt
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git


cd opencv
mkdir release
cd release


cmake -D WITH_CUDA=OFF -D BUILD_TIFF=ON -D BUILD_opencv_java=OFF -D ENABLE_AVX=ON -D WITH_OPENGL=ON -D WITH_OPENCL=ON -D WITH_IPP=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_V4L=ON -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_python2=OFF -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -D PYTHON3_EXECUTABLE=$(which python3) -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_EXECUTABLE=~/anaconda3/envs/cvenv2/bin/python -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules -D BUILD_EXAMPLES=ON /opt/opencv/

make -j8
make install
ldconfig
exit
cd ~

The conda virtual environment is ~/anaconda3/envs/cvenv2. I believe that compilation is needed because I am not able to use ffmpeg without it.

YellowishLight
  • 238
  • 3
  • 21

2 Answers2

2

I had to link the cv2.so file to my conda environment!

# navigate to the conda environment
cd ~/anaconda3/envs/<env-name>/lib/python3.6/site-packages
ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

see this issue: Unable to import cv2 after building from source

my cmake command was this one:

cmake -D WITH_CUDA=OFF \
-D BUILD_TIFF=ON \
-D BUILD_opencv_java=OFF \
-D WITH_QT=ON \
-D WITH_V4L=ON \
-D WITH_VTK=OFF \
-D BUILD_TESTS=OFF \
-D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_opencv_python2=OFF \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
-D PYTHON_EXECUTABLE=~/anaconda3/envs/<env-name>/bin/python \
../

I activated my conda enviroment for the build.

1

Problem solved, I was installing the latest version of OpenCV outside the virtual environment. Here are the updated instructions :

conda activate yourenvironment

sudo apt-get update
sudo apt-get upgrade


sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install python3.6-dev python3-numpy libtbb2 libtbb-dev

You need to install libjasper-dev package alone according to OpenCV in Ubuntu 17.04

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev


git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git


cd opencv
mkdir release
cd release

Make sure to adapt the name of your virtual environment in the CMAKE command PYTHON_EXECUTABLE=~/anaconda3/envs/yourvirtualenv/bin/python

cmake -D WITH_CUDA=OFF -D BUILD_TIFF=ON -D BUILD_opencv_java=OFF -D ENABLE_AVX=ON -D WITH_OPENGL=ON -D WITH_OPENCL=ON -D WITH_IPP=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_V4L=ON -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_python2=OFF -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -D PYTHON3_EXECUTABLE=$(which python3) -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_EXECUTABLE=~/anaconda3/envs/cvenv2/bin/python -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..

make -j8
sudo make install
sudo ldconfig

To verify opencv installation within the virtual environment :

pkg-config --modversion opencv

Credits go mostly to these two websites from were I got the instruction and figured out the solution : site1, site2

YellowishLight
  • 238
  • 3
  • 21