23

I would like to install opencv to my conda environment from source. Since I'm using Jetson, there is no pip or conda packages that are available for opencv.

I use this command for installing from source,

    -D BUILD_EXAMPLES=OFF
    -D BUILD_opencv_python2=ON
    -D BUILD_opencv_python3=ON
    -D CMAKE_BUILD_TYPE=RELEASE
    -D CMAKE_INSTALL_PREFIX=${PREFIX}
    -D CUDA_ARCH_BIN=5.3,6.2,7.2
    -D CUDA_ARCH_PTX=
    -D CUDA_FAST_MATH=ON
    -D CUDNN_VERSION='8.0'
    -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 
    -D ENABLE_NEON=ON
    -D OPENCV_DNN_CUDA=ON
    -D OPENCV_ENABLE_NONFREE=ON
    -D OPENCV_EXTRA_MODULES_PATH=/tmp/build_opencv/opencv_contrib/modules
    -D OPENCV_GENERATE_PKGCONFIG=ON
    -D WITH_CUBLAS=ON
    -D WITH_CUDA=ON
    -D WITH_CUDNN=ON
    -D WITH_GSTREAMER=ON
    -D WITH_LIBV4L=ON
    -D WITH_OPENGL=ON"

How do I install the python dependencies to my conda environment instead of installing it to usr/local/python?

Ehsan
  • 3,711
  • 27
  • 30
user1241241
  • 664
  • 5
  • 20
  • What architecture is Jetson that it has Conda but can't use `conda-forge::opencv`? At the very least, [the Conda Forge recipe](https://github.com/conda-forge/opencv-feedstock/tree/master/recipe), in particular the `meta.yaml` and the `build.sh` should be extremely useful points of reference for how to compile with and into Conda. – merv May 24 '21 at 21:52

4 Answers4

8

By default it will install to your system Python path which you can see by entering:

which python

in the terminal. In your cmake commands (the above list you posted) you need to tell it which python executable path you want to build to. At the moment your build is pointing to the above default Python location, and now you want to point it to your Conda Python path. So for example, my base path for my Python environment in Anaconda is:

/home/robert/anaconda3/

You can get a list of your Anaconda environments and their location by entering this in the terminal:

conda env list

To do this, you'll need to update the cmake commands to tell it where the Python path which you want to build to is located. I've used this post before to help me correctly specify the Python executable build path, and it has worked for me when specifying the Python path for a venv.

For example, if I wanted to install to one of my Anaconda environments I would do something like this in my cmake:

-D PYTHON_DEFAULT_EXECUTABLE=$(/home/robert/anaconda3/envs/venv_openvcv/python3)

When you build cmake, scroll through the output and pay particular attention to the line which says something like:

Python (for build): /home/robert/anaconda3/envs/venv_openvcv/python3

This is your way of confirming if it is about to build opencv to the correct Python executable (the Anaconda one you have specified).

Edit: Additionally here is a tutorial which outlines in detail the steps to compile OpenCV for an Anaconda environment - Installing OpenCV for Conda Virtual Environments

Robert Young
  • 456
  • 2
  • 8
  • Basically what I gather from here, is that one needs to tell the `cmake` command everything about the structure of a conda environment (the path to the lib, include, bin and etc directories) for this to work, and not just follow a naive thought of providing only the conda environment's root. Which actually makes sense when I consider that cmake is not supposed to know anything about the tool conda. – matanster May 24 '21 at 15:48
3

On Ubuntu 20.04, this worked for me from within a new clean build directory:

export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/lib/python3.8
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=$CONDA_PREFIX \
    -D PYTHON3_LIBRARY=$CONDA_PREFIX/lib/python3.8 \
    -D PYTHON3_INCLUDE_DIR=$CONDA_PREFIX/include/python3.8 \
    -D PYTHON3_EXECUTABLE=$CONDA_PREFIX/bin/python \
    -D PYTHON3_PACKAGES_PATH=$CONDA_PREFIX/lib/python3.8/site-packages \
    ..

I had also installed OpenCL header files for my platform to overcome an intervening other error before getting this to work. And If you don't already have many additional developer header files on your system you probably need to install more of them first. This got me past the cmake stage alright, and then through building with make.

There was no need to symlink anything after the make install.

So maybe this is a good base recipe for python 3.8; It seems the only necessary modification to the official Ubuntu OpenCV build documentation was specifying the cmake arguments pointing at the conda environment directories, like above.

(OpenCV git hash used was 69357b1)

I'd probably try -j for concurrency in the build process next time around, as OpenCV takes ~30 minutes to build on a rather modern CPU series.

matanster
  • 15,072
  • 19
  • 88
  • 167
3

I know this is already solved, but I just wanted to share the lines that helped me installing OpenCV from source in a conda environment with Python 3.10.2 in Ubuntu 20.04.4 LTS. My conda env is called cv4

export CPLUS_INCLUDE_PATH=~/miniconda3/envs/cv4/lib/python3.10
cmake   -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
        -D PYTHON3_LIBRARY=~/miniconda3/envs/cv4/lib/libpython3.10.so \
        -D PYTHON3_INCLUDE_DIR=~/miniconda3/envs/cv4/include/python3.10 \
        -D PYTHON3_EXECUTABLE=~/miniconda3/envs/cv4/bin/python \
        -D PYTHON3_PACKAGES_PATH=~/miniconda3/envs/cv4/lib/python3.10/site-packages \
        -D BUILD_opencv_python2=OFF \
        -D BUILD_opencv_python3=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D BUILD_EXAMPLES=ON ..

With those lines (that I found here and here) helped me install OpenCV from source following this guide.

Alex Turner
  • 698
  • 6
  • 16
0

Well, all the above answers are great. But I needed to do some additional tweaking to make it run. I believe an end-to-end description of which worked for me will be helpful.

  • Clone/Download opencv and opencv-contrib. Contrib contains additional functionality you most likely want. Make sure both of the versions are the same. In my case:
git clone https://github.com/opencv/opencv.git -b 4.8.0 --single-branch
git clone https://github.com/opencv/opencv_contrib.git -b 4.8.0 --single-branch

Change the version pointer from 4.8.0 to your desired version according to your need. Keep these two clones inside the same parent directory.

  • Go inside of opencv folder and make a folder called build and go inside of build:
cd opencv && mkdir build && cd build
  • Open up the terminal and activate your desired Anaconda environment. Activating Anaconda might not be required, but I did this anyway.
  • Enter the following in the terminal:
CONDA_PREFIX=~/anaconda3/envs/YOUR_ENV_NAME
PYTHON_VERSION=3.11

Here use your anaconda environment name instead.

  • Run the following:
export CPLUS_INCLUDE_PATH=$CONDA_PREFIX/lib/python$PYTHON_VERSION
cmake   -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=$CONDA_PREFIX/ \
        -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
        -D PYTHON3_LIBRARY=$CONDA_PREFIX/lib/libpython$PYTHON_VERSION \
        -D PYTHON3_INCLUDE_DIR=$CONDA_PREFIX/include/python$PYTHON_VERSION \
        -D PYTHON3_EXECUTABLE=$CONDA_PREFIX/bin/python$PYTHON_VERSION \
        -D PYTHON3_PACKAGES_PATH=$CONDA_PREFIX/lib/python$PYTHON_VERSION/site-packages \
        -D BUILD_opencv_python2=OFF \
        -D BUILD_opencv_python3=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D BUILD_EXAMPLES=ON ..

Here I am assuming you are only interested to install opencv for Python3 and not for Python2.

After building is done, now make it. -j$(nproc) ensures we are using all CPU cores to make the process faster:

make -j$(nproc)
  • Finally, install this:
sudo make install
  • After installation is done, inside your anaconda environment, open Python shell and try to import cv2. You may get the following similar to it:
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hafiz031/anaconda3/envs/CV_mod/lib/python3.11/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/hafiz031/anaconda3/envs/CV_mod/lib/python3.11/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/hafiz031/anaconda3/envs/CV_mod/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: /home/hafiz031/anaconda3/envs/CV_mod/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.30' not found (required by /home/hafiz031/anaconda3/envs/CV_mod/lib/libopencv_gapi.so.408)
  • If so, then a little bit of work is yet to be done:
conda install -c conda-forge gcc=12.1.0
  • Now let's try one more time:
>>> import cv2
>>> cv2.__version__
'4.8.0'

If so, then you are done with your installation .

hafiz031
  • 2,236
  • 3
  • 26
  • 48