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
?
With anaconda 3, type conda install opencv command on annaconda prompt. Installs 3.4. 1 opencv and various dependent packages with it. Worked for me and also it did not require any custom installing specification.
Step 1:- After installing the anaconda open the Anaconda Prompt. Step 2:- Type the given command, press enter, and let it download the whole package. Step 3:- Now simply import OpenCV in your python program in which you want to use image processing functions.
We also learned which package to install and how to create a virtual environment for working on the package. For all purposes of learning computer vision using Python, we highly recommend installing opencv-contrib-python using prebuilt binaries.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With