I need to build OpenCV from source and I have limited space, so, I have to build OpenCV on a module basis (https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html - Build limited set of modules). OK, cmake and make work fine.
Would be easier to use these OpenCV modules from python so I add cmake -D BUILD_opencv_python3=ON
but make install
does not create the prefix/lib/pythonX.Y/site-packages
directory I expected. There is no python related option but only a CMake option as far as I understand the doc.
How to get python(3) binding for OpenCV when building it on a module basis ?
Once installed, how to set up environnement to get python -c "import cv2"
to work with my custom-built OpenCV ?
UPDATE
Running ubuntu L4T on jetson. CMake outputs :
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.6.9)
-- Libraries: NO
-- numpy: /home/me/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.19.5)
-- install path: -
So I guess empty install path hides a problem behind... But grep -ni OPENCV_PYTHON3_INSTALL_PATH CMakeCache.txt
returns nothing.
At this point, I tried to add cmake -DOPENCV_PYTHON3_INSTALL_PATH=~/Programs/opencv/local/lib/python3.6/site-packages/
, still nothing in cmake output (blank install path) but
grep -ni OPENCV_PYTHON3_INSTALL_PATH CMakeCache.txt
1076:OPENCV_PYTHON3_INSTALL_PATH:UNINITIALIZED=~/Programs/opencv/local/lib/python3.6/site-packages/
And after make install
(without sudo - I don't have root privilege and need local install), I have no python binding in the local install.
... Any clue would be appreciated !
Note: compiling opencv-4.5.4 on L4T ubuntu
CodePudding user response:
To have py binding, make sure the cmake config output near the end has the Python 3
section i.e.
Python 3:
Interpreter: /home/user/anaconda3/envs/cv451/bin/python3 (ver 3.8.12)
Libraries: /home/user/anaconda3/envs/cv451/lib/libpython3.8.so (ver 3.8.12)
numpy: /home/user/anaconda3/envs/cv451/lib/python3.8/site-packages/numpy/core/include (ver 1.21.2)
install path: /home/user/anaconda3/envs/cv451/lib/python3.8/site-packages/cv2/python-3.8
For me that section was missing at first. My problem was python version mismatch between the lib (found from system) and the interpreter (from conda env).
Thus in my case the fix was:
- Have cmake find the conda env's python instead of the system's python:
CMAKE_LIBRARY_PATH=/home/user/anaconda3/envs/cv451/lib cmake /path/to/opencv/arc
- By default the binding is installed to system's python. To specify the python binding install path, use the cmake flag
OPENCV_PYTHON3_INSTALL_PATH
i.e.
-D OPENCV_PYTHON3_INSTALL_PATH=/home/user/anaconda3/envs/cv451/lib/python3.8/site-packages/
- Remember to install after the compile
sudo make install
Ref:
- https://gist.github.com/raulqf/f42c718a658cddc16f9df07ecc627be7
- https://qengineering.eu/install-opencv-4.5-on-jetson-nano.html
CodePudding user response:
cmake -DBUILD_LIST=python3 ..
solved the problem: getting clean install directory including python site-packages directory.