Install OpenCV 4.5.1 with CUDA and cuDNN acceleration, with GStreamer and Python 3 bindings on PopOS 20.10

OpenCV is awesome. OpenCV + CUDA = More awesome. OpenCV + CUDA + cuDNN (deep neural networks) = MIND BLOWN.

What is cuDNN?

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers.

Compiling from source is always a pain. I’m here to make your life easier. Today I’m going to show you how to build and install OpenCV 4.5.1 with CUDA and cuDNN acceleration, with GStreamer and Python 3 bindings on PopOS 20.10. Since PopOS is based on Ubuntu, the instructions will work on that as well.

LET’S DO THIS.

Prerequisites

My workstation specs are:

AMD Ryzen 3950X.

NVIDIA RTX 2080TI.

Fresh install of PopOS 20.10 with NVIDIA drivers.

Check your GPUs CUDA version support

Go to NVIDIA’s website to check your which version of CUDA it supports:

https://developer.nvidia.com/cuda-gpus

Make a note of the version number as we’ll need this information when we compile OpenCV. For my RTX 2080TI, the CUDA version is 7.5.

Update PopOS

Install CUDA and cuDNN libraries

Installing cuDNN will also install matching CUDA version.

If for some reason CUDA was not installed, you can install it with:

Download cuDNN source files from NVIDIA.

These source files are needed to compile OpenCV.

https://developer.nvidia.com/rdp/cudnn-download

The file you want is: cudnn-11.1-linux-x64-v8.0.5.39.tgz

Put the file in your home directory and we’ll move these into the right place:

Make the appropriate directories:

Copy the extracted files to the folders we created:

Create Python virtual environment

Install virtual environment tools:

Add the appropriate entries to bashrc:

Create a virtual environment called CV:

Install numpy in our virtual environment as it will be need to compile OpenCV:

Install dependencies

Prepare OpenCV source files

Make our working folder in home:

Clone OpenCV into our working directory:

Clone OpenCV libraries:

Make build folder inside the OpenCV folder:

Configure and build OpenCV

Make sure you have the right GPU version number from earlier and change this line to match your GPU CUDA version:

Configure the build:

If the configuration completed without errors, we can build it. Change the number to the number of cpu threads to use. I have a 16 core, 32 thread CPU so I use 30 threads to leave some cpu headroom for me to watch YouTube videos while its building:

Let’s install!

Final setup

Let’s check to see if the OpenCV Python module is installed in the right path:

We’ll rename the module and link it into our virtual environment:

Lets create a symbolic link to our virtual environment:

Sanity check

Check with python:

You are done. Now go make stuff!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store