Can I use opencv-python with GPU?

Hi everybody,
I’n new in using OpenCV. Lately, I joined a big project where they process some images by using opencv-python. I’m trying to optimize this code since it consumes all the CPU memory and I’d like to perform the processing on the GPU (we have an AMD Radeon RX Vega, but we can also upgrade to an NVIDIA) by keeping the code on python.
I’ve read that opencv-python is a CPU-only OpenCV package. How could I pass the processing to the GPU? Is there a way?

the default, binary python cv2 install (e.g. from pypi) does not have any CUDA support.

however, most opencv functions are opencl optimized, and you can access them using cv2.UMat out of the box.

if you do need CUDA, you will have to build your cv2 from src
(this will ofc need nvidia hw and a CUDA sdk installed)

i’ve done this on colab
(low hanging fruit, everything you need is already there)
and it was as easy as:

!git clone https://github.com/opencv/opencv
!git clone https://github.com/opencv/opencv_contrib
!mkdir /content/build
%cd /content/build

!cmake -DOPENCV_EXTRA_MODULES_PATH=/content/opencv_contrib/modules  \
       -DBUILD_SHARED_LIBS=OFF \
       -DBUILD_TESTS=OFF \
       -DBUILD_PERF_TESTS=OFF \
       -DBUILD_EXAMPLES=OFF \
       -DWITH_OPENEXR=OFF \
       -DWITH_CUDA=ON \
       -DWITH_CUBLAS=ON \
       -DWITH_CUDNN=ON \
       -DOPENCV_DNN_CUDA=ON \
       /content/opencv

!make -j8 install
1 Like