# Can I use opencv-python with GPU?

**URL:** <https://forum.opencv.org/t/can-i-use-opencv-python-with-gpu/8947>\
**Category:** Python\
**Tags:** gpu\
**Created:** [May 28, 2022, 12:37pm UTC](https://forum.opencv.org/t/can-i-use-opencv-python-with-gpu/8947 "2022-05-28T12:37:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![titaniumwhite](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/titaniumwhite/32/5331_2.png) [@titaniumwhite](https://forum.opencv.org/u/titaniumwhite)\
**Post date:** [May 28, 2022, 12:37pm UTC](https://forum.opencv.org/t/can-i-use-opencv-python-with-gpu/8947/1 "2022-05-28T12:37:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 28, 2022, 2:30pm UTC](https://forum.opencv.org/t/can-i-use-opencv-python-with-gpu/8947/2 "2022-05-28T14:30:58Z")

</div>

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:

```auto
!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

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 8, 2024, 1:22pm UTC](https://forum.opencv.org/t/can-i-use-opencv-python-with-gpu/8947/3 "2024-02-08T13:22:33Z")

</div>


