Date:

Share:

How to enable NVIDIA CUDA with OpenCV in Arch Linux

Related Articles

Last updated: April 7, 2023


Hello friends, this tutorial is very specific. However, if it helps one person it’s worth writing.

I spent most of the day trying to get OpenCV to use CUDA with mine NVIDIA 4080. Yes really. I found tons of answers that for some reason do not suit me in one way or another. I tried and failed and put things together and made it happen.

So if you have an NVIDIA card and want to use the GPU to work on OpenCV instead of the CPU, you’re in luck. If you have a 4080 or 4090 you can just copy and paste, build and be done. You have to change some things for other cards.

Step 1: Before you start, try it

Now, the first thing I tried was:

sudo pacman -S opencv-cuda

This is an OpenCV binary build with Cuda support, and I haven’t been able to get it to work for one reason or another.

But a lot of people have, so maybe you can. Try installing with pacman, then create a Python file that looks like this:

import cv2

print("OpenCV version:", cv2.__version__)
print("CUDA supported:", cv2.cuda.getCudaEnabledDeviceCount() > 0)

then run

pip install opencv-python

If you run it and see this:

And then you’re done. Close this web page and enjoy.

But since you’re reading this guide, I’m guessing you’ve already tried it and it didn’t work. We are going to compile OpenCV from source and optimize it for the GPU.

Let’s jump in.

Step 2: Install Python 2

Yes, I know it’s a little weird. I have Python 3 installed and that’s what I use every day. But I couldn’t get it to compile without Python 2 being present. You can install it from the AUR; It shouldn’t affect anything you do.

First, add this key:

gpg --keyserver keyserver.ubuntu.com --recv-key 04C367C218ADD4FF

Then clone the AUR

git clone https://aur.archlinux.org/python2.git && cd python2

and install it.

You never know, it might still come in handy. On to the next part.

Step 3: The prerequisites

These are a set of prerequisites for compiling OpenCV from source. Yes, I know python-opencv is there, and we’re replacing it, but it needs to be in place to build your custom version of OpenCV. don’t ask me why

sudo pacman -S base-devel cmake git python python-numpy python-opencv openmpi python-mpi4py boost

Step 5: NVIDIA stuff

If you have an NVIDIA card (like me) this is what you need to install. You may already have this material. Fortunately, you don’t need to compile the NVIDIA stuff by hand.

sudo pacman -S nvidia nvidia-utils nvidia-settings cuda

After that, you are almost ready to compile. Next, you need to generate your CMAKE commands.

Step 6: Find out what you have

If you have an NVIDIA 4080 you can skip this section and copy my code. If not, here’s how to check which video card you’re using:

It shows I’m running a GeForce RTX 4080. Great! I will need this information.

Find the graphics card you are using. If it’s an NVIDIA card, you’ll find out Which CUDA version are you running here.

If you don’t see your card, you may be able to Find it here.

You need to find out which version of CUDA your card supports. I have a 4080, so I needed 8.9. We will see why this is relevant in the next section.

Note: You will need to know which version of CUDA your card supports.

Step 7: Grab the OpenCV Repos

Here are the two repositories you will need:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

We will link the contribution folder in our CMAKE configuration.

I do all my source building in a ~/src folder. But here is the layout I have:

/src/opencv
/src/opencv_contrib

Then I create a “build” folder:

And this is the folder where I build OpenCV. It’s an “out of source build” and works well for me. You can delete everything if something doesn’t work and run CMAKE otherwise.

Step 8: CMAKE flags

Next, you need to set your CMAKE flags. Here is the type of setup I built:

OpenCV with:

  • CUDA stuff
  • Some OpenCV examples
  • There are no documents
  • There are no tests
  • OpenCL
  • FFmpeg
  • Stuff built just for my graphics card.

So I’ll walk you through my CMAKE build with the latest version at the end.

First the initial command. I want it to be a release build, in /usr/local:

cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local 

This is just to help it find the OpenMP library.

-D CMAKE_PREFIX_PATH=/usr/lib  

Add our additional modules:

-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules  

And of course, enable CUDA.

This part is special for my NVIDIA card (RTX 4080). Again you can find Which CUDA version should you target here

-D CUDA_ARCH_BIN=8.9  
-D ARCH=sm_89  
-D gencode=arch=compute_89,code=sm_89  

Next, my configuration for OpenCV. I’m optimizing as much as I can, adding OpenCL and FFMPEG, and not building any documentation or tests. Feel free to tweak these however you like.

-D ENABLE_FAST_MATH=ON  
-D CUDA_FAST_MATH=ON  
-D WITH_CUBLAS=ON  
-D WITH_CUFFT=ON  
-D WITH_NVCUVID=ON  
-D WITH_OPENMP=ON  
-D BUILD_EXAMPLES=ON  
-D BUILD_DOCS=OFF  
-D BUILD_PERF_TESTS=OFF  
-D BUILD_TESTS=OFF  
-D WITH_TBB=ON  
-D WITH_IPP=ON  
-D WITH_NVCUVID=ON  
-D WITH_CSTRIPES=ON  
-D WITH_OPENCL=ON  
-D WITH_FFMPEG=ON  
-D CMAKE_PREFIX_PATH=/usr/lib  

It’s just a CPU optimization flag. It’s probably not even necessary.

-D CMAKE_CXX_FLAGS="-Ofast"  

Finally, you grab OpenCV:

Here is my final CMAKE command:

cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local 
-D CMAKE_PREFIX_PATH=/usr/lib  
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules  
-D WITH_CUDA=ON  
-D CUDA_ARCH_BIN=8.9  
-D ARCH=sm_89  
-D gencode=arch=compute_89,code=sm_89  
-D ENABLE_FAST_MATH=ON  
-D CUDA_FAST_MATH=ON  
-D WITH_CUBLAS=ON  
-D WITH_CUFFT=ON  
-D WITH_NVCUVID=ON  
-D WITH_OPENMP=ON  
-D BUILD_EXAMPLES=ON  
-D BUILD_DOCS=OFF  
-D BUILD_PERF_TESTS=OFF  
-D BUILD_TESTS=OFF  
-D WITH_TBB=ON  
-D WITH_IPP=ON  
-D WITH_NVCUVID=ON  
-D WITH_CSTRIPES=ON  
-D WITH_OPENCL=ON  
-D WITH_FFMPEG=ON  
-D CMAKE_PREFIX_PATH=/usr/lib  
-D CMAKE_CXX_FLAGS="-Ofast"  
../opencv

It’s time to rock and roll. Note, you can also use cmake-gui to configure this:

I decided not this time because I’d rather edit a shell script and repeat it. However, the GUI is nice.

Step 9: Build it!

If you’ve selected everything and there are no errors in your CMAKE configuration, it’s time to build. A simple command:

If things are going well, you should see this:

Now it’s time to install!

And now, you should see this:

It’s great. Great party time.

Finally you need to add the new OpenCV to your Python Path:

export PYTHONPATH="/usr/local/lib/python3.10/site-packages:$PYTHONPATH"

Note, for me it was python3.10. It may be different for you. You are done! Let’s make sure it works.

Step 10: Test the installation

Now, create a Python file that includes this:

import cv2

print("OpenCV version:", cv2.__version__)
print("CUDA supported:", cv2.cuda.getCudaEnabledDeviceCount() > 0)

And you should see this:

Good times!

Summary

Like I said, I spent most of the day on it. It was a huge pain, so I put it together so you don’t have to suffer like I did. I hope this helps!

Bookmark my blog because I have many more OpenCV Stuff here!

Happy coding, and see you in the next tutorial.

– Jeremy

I have a lot OpenCV demos On GitHub you can download and run. I build them on mine Computer Vision live broadcast

Questions, comments? Yell at me!


Source

Popular Articles