I have worked with OpenCV with Python bindings in the past few months and I have found that the easiest way to make sure OpenCV uses accelerations and other libraries (e.g. CUDA, GStreamer) has been to "make" it myself. This would involve building OpenCV from source on the target platform and then copying some ".so" files. However, I have run into two problems that I think are connected:
- IntelliJ refuses to pick up the cv2 library. I can live with that, but it is annoying to not be able to use my tools.
- I don't know how to link the library to a different virtualenv - so I have to recompile it for every new environment
I have been following a modified version of Adrian's fantastic guide.
Assuming that you have already cloned opencv and opencv_contrib and are in the virtual environment of your choice:
PYTHON_VERS=python$(python -c "import sys; print('%s.%sm' % (sys.version_info.major, sys.version_info.minor))")
PYTHON_VERS_NO_MALLOC=python$(python -c "import sys; print('%s.%s' % (sys.version_info.major, sys.version_info.minor))")
PYTHON_ROOT=$(python -c "import sys; print(sys.prefix)")
export CPLUS_INCLUDE_PATH=${PYTHON_ROOT}/include/${PYTHON_VERS}/
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-DPYTHON_INCLUDE_DIR=${PYTHON_ROOT}/include/${PYTHON_VERS}/ \
-DPYTHON_LIBRARY=${PYTHON_ROOT}/lib/lib${PYTHON_VERS}.dylib \
-DPYTHON_EXECUTABLE=${PYTHON_ROOT}/bin/python \
-DPYTHON_PACKAGES_PATH=${PYTHON_ROOT}/lib/${PYTHON_VERS}/site-packages/ \
-D CMAKE_INSTALL_PREFIX=~/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_FFMPEG=ON \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_GSTREAMER=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
make -j12 > opencvbuild.log 2>&1 &
make install
cp ~/local/lib/${PYTHON_VERS_NO_MALLOC}/site-packages/cv2.*.so ${PYTHON_ROOT}/lib/${PYTHON_VERS_NO_MALLOC}/site-packages/cv2.so
What I hope is possible is to compile OpenCV only once and then reuse it in all my environments. Not only that, but when I recompile it due to binary dependency changes (e.g. new OpenCL/CUDA support, GStreamer updates, Intel AVX512, etc.), I would expect this change to propagate to all of my installations.
Any help or pointers on that would be very greatly appreciated!