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
!git clone
!mkdir /content/build
%cd /content/build

!cmake -DOPENCV_EXTRA_MODULES_PATH=/content/opencv_contrib/modules  \
       -DWITH_CUDA=ON \
       -DWITH_CUDNN=ON \

!make -j8 install
1 Like