pythonopencvconda

Install Opencv from source to conda environment


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?


Solution

  • 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