Opencv gpu python install

Install OpenCV GPU with CUDA for Windows 10

You must install OpenCV for GPU if your system allows you. OpenCV library can be used for both CPU and GPU, but if you just install OpenCV by “pip” or “conda” command ( pip install opencv-python ) it will use CPU as a backend by default. Now if you are working on deep learning or video processing project like Object Detection, Social Distance detection, you will face lags in the output video (less frame rate per second), you can fix this lag using GPU if your system has NVIDIA GPU (NVIDIA Graphics card).

It’s easy to install OpenCV for GPU on the Linux machine but it is hard for Windows. That is why I am making this post.

To install OpenCV GPU on windows we have to compile or build the source code of Opencv with CUDA, cuDNN, and Nvidia GPU. To do that we need to use some tools like Visual Studio (C++’s GCC compiler), CMake, etc.

System Requirement to build OpenCV with Cuda windows

Before you start to build OpenCV with Cuda for windows, make sure you have NVIDIA graphics in your system. To know about your graphics:

install opencv gpu

  • Go to “Device Manager” of your system
  • Click on “Display adapters

As you can see I have two graphics cards:

Now visit below Wikipedia page to know your graphics card is eligible for CUDA Toolkit framework or not:

opencv cuda python windows

https://en.wikipedia.org/wiki/CUDA

Now search for your graphics card model name (in my case GTX 1050 Ti) on this page. As you can see my graphics card (GeForce GTX 1050 Ti) is listed on that Wikipedia page. So I can install OpenCV with Cuda for GPU access in my system.

Steps to build OpenCV with Cuda for Windows

Below are the steps we are going to follow to install OpenCV with CUDA for windows operating system.

  1. Uninstall anaconda or python and install fresh python
  2. Install “numpy” and uninstall “opencv-python”, “opencv-contrib-python”
  3. Download & install Visual Studio
  4. Download and install CUDA according to your GPU
  5. Download and install cuDNN according to CUDA
  6. Download & extract Opencv-4.4 Source from Github
  7. Download & extract Opencv-contrib-4.4 from Github
  8. Download & install CMake
  9. Make a new folder
  10. Make changes in CMake file “OpenCVDetectPython.cmake”
  11. Configure Opencv and Opencv-contrib using Cmake
  12. Build The project created by Cmake with Visual Studio
  13. Verify whether installations are correctly done
Читайте также:  Html параметры в адресной строке

Step1 | Uninstall anaconda or python and install fresh python for all user

Follow this step otherwise there can be path issue later.

build opencv with cuda

1.1. Uninstall python or anaconda whatever you are using and install python a fresh python. I have downloaded python 3.9.5
1.2. Check the “system environment variables” make sure older path has been removed and current path is there

Step2 | Install “numpy” and uninstall “opencv-python”, “opencv-contrib-python”

Before compiling make sure “numpy” is installed. Make sure that “opencv-python” and “opencv-contrib-python” is uninstalled and will never be installed again using “pip” in this environment again

2.1. Install “numpy” (pip install numpy)
2.2. Uninstall opencv if you installed anaconda instead fresh Python (pip uninstall opencv-python)
2.3. Uninstall “opencv-contrib-python” if you installed anaconda instead fresh python (pip uninstall opencv-contrib-python)

In my case I have installed fresh python so I skipped step 2.2 and 2.3. I just followed step 2.1.

Step3 | Download & install Visual Studio

3.1. Download Community edition Visual Studio, In my case, I have downloaded Visual Studio 2019: https://visualstudio.microsoft.com/downloads/
3.2. Check on “Desktop development with C++”, and Continue with defaults and click on install

Download and Install Visual Studio 19

Step4 | Download and install CUDA Toolkit according to your GPU

At this point hope you have already checked that your system allows you to install CUDA. See System Requirement section of this article, if you are missing anything.

4.1. Download CUDA from the below link. I have downloaded CUDA Toolkit 10.1
https://developer.nvidia.com/cuda-toolkit-archive

Download CUDA Toolkit

Add CUDA variable path

4.2. Once you downloaded, install CUDA Toolkit (keep everything default)
4.3. Make sure your CUDA_PATH & CUDA_PATH_V10_1 is there, if not then add those paths

Step5 | Download and install cuDNN according to CUDA

5.1 Download cuDNN according to CUDA

To download cuDNN you need to register on the NVIDIA website, then you can download cuDNN: https://developer.nvidia.com/rdp/cudnn-archive

5.1.1. Download and extract cuDNN (version which corresponds to your suitable Cuda version)

I have downloaded CUDA 10.1 so I am going to download cuDNN 7.6.5

Download cuDNN for cuda version

Download cuDNN for windows 10

Note: If you click on a particular version of cuDNN, It will open various cuDNN libraries for the different operating systems. I am using windows 10 so I have downloaded cuDNN Library for Windows 10.

5.2. Install cuDNN in windows

5.2.1. Find CUDA installation folder, In my case: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1

5.2.2. Open downloaded and extracted cuDNN folder

  1. Copy all files (one file in my case) from CuDNN bin folder and paste inside CUDA (installation folder) bin folder
  2. Copy all files (one file in my case) from CuDNN include folder and paste inside CUDA (installation folder) include folder
  3. Copy all files (one file in my case) from CuDNN lib/x64 folder and paste inside CUDA (installation folder) lib/x64 folder
Читайте также:  Anchor Links

By doing that the installation of cuDNN is now finished.

Step6 | Download & extract Opencv-4.4 Source from Github

Before compiling make sure you have installed numpy library.

6.1. Follow this link: https://opencv.org/releases/
6.2. Click on sources to download OpenCV source
6.3. Extract the downloaded folder

Download OpenCV 4.4.0 to build opencv with cudato

Step7 | Download & extract Opencv-contrib-4.4 from Github

We need one extra package which needs to be used along with opencv. This package is called Opencv-contrib.

7.1. Goto link https://github.com/opencv/opencv_contrib/tree/4.4.0 then click on code > download zip
7.2. Extract the downloaded folder

Step8 | Download & install CMake

8.1. Click this link to download CMake: https://cmake.org/download/

Step9 | Make a new folder

This is the folder where we will compile and save the object code. I am giving this folder name as “build”

create folder to build opencv with cuda

9.1. Make a folder named “build”

Step10 | Make changes in CMake file “OpenCVDetectPython.cmake”

If we try to compile OpenCV without any changes by default it will search for the python2 compiler. We should change the “OpenCVDetectPython.cmake” code so that it detects the python3 compiler by default.

10.1. Go inside extracted “opencv-4.4.0” folder
10.2. Find and open file “opencv-4.4.0\cmake\OpenCVDetectPython.cmake

edit code in Cmake

10.3. Replace last code block of that Cmake file with below code:

Previous code :

if(PYTHON_DEFAULT_EXECUTABLE) set(PYTHON_DEFAULT_AVAILABLE "TRUE") elseif(PYTHON2_EXECUTABLE AND PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter set(PYTHON_DEFAULT_AVAILABLE "TRUE") set(PYTHON_DEFAULT_EXECUTABLE "$") elseif(PYTHON3_EXECUTABLE AND PYTHON3INTERP_FOUND) # Use Python 3 as fallback Python interpreter (if there is no Python 2) set(PYTHON_DEFAULT_AVAILABLE "TRUE") set(PYTHON_DEFAULT_EXECUTABLE "$") endif()

Replace with this code :

if(PYTHON_DEFAULT_EXECUTABLE) set(PYTHON_DEFAULT_AVAILABLE "TRUE") elseif(PYTHON3INTERP_FOUND) # Use Python 3 as default Python interpreter set(PYTHON_DEFAULT_AVAILABLE "TRUE") set(PYTHON_DEFAULT_EXECUTABLE "$") elseif(PYTHON2INTERP_FOUND) # Use Python 2 as fallback Python interpreter (if there is no Python 3) set(PYTHON_DEFAULT_AVAILABLE "TRUE") set(PYTHON_DEFAULT_EXECUTABLE "$") endif()

replace code in Cmake file to install opencv for gpu

Step11 | Configure OpenCV and Opencv-contrib using Cmake

Now we need to configure OpenCV by providing paths of CUDA, cuDNN.

11.1. Open Cmake app which we have already installed in Step 8
11.2. Provide path of OpenCV source code which we have already downloaded in Step 6
11.3. Provide path of ‘build’ folder which we have already created in Step 9

Cmake configuration to opencv cuda python windows

11.4. Click on “Configure” button
11.5. Then in Configure window select optional platform as x64
11.6. Click on “finish” button
11.7. Now configure following variables by searching and checking those variable in search tab:

WITH_CUDA — Check it OPENCV_DNN_CUDA — Check it ENABLE_FAST_MATH — Check it OPENCV_EXTRA_MODULES_PATH — Provide path of “modules” directory from “opencv-contrib-4.4.0” directory(Step 7) In my case: "C:\Users\Anindya\Downloads\opencv-contrib-4.4.0\modules"

check WITH_CUDA to build opencv with cuda windows 10check OPENCV_DNN_CUDA to install opencv with cudacheck enable FAST_MATH to install opencv for gpu

11.8. Hit configure button again wait for the “configuration done” output
11.9. Now we need to configure some more variables

CUDA_FAST_MATH — Check it CUDA_ARCH_BIN — 6.1 (This is strictly for my case)

You can visit below Wikipedia page to know your GPU’s arch_bin

Читайте также:  Python pandas append series

You just visit that page and search for your graphics card model number (Check System Requirement section of this page). My Graphics card model is GTX 1050 Ti.

So on that Wikipedia page, I will search for GTX 1050 Ti, and in that row first column value is CUDA_ARCH_BIN value for my graphics card (GTX 1050 Ti). Which is 6.1

11.10. Click configure button again and wait for “configuration done” output
11.11. After that click the Generate button and wait for “Generating done” output

output message of Cmake to build opencv with cuda

11.12. Your configuration and generation of code are done. Now you can close the cmake-gui app

Step12 | Build The project created by Cmake with Visual Studio

Now we need to build and compile the code configured by Cmake in our newly created folder named “build

12.1. Using cd command go to “build” folder
12.2. Type OpenCV.sln and hit enter. it will open Visual Studio for us

Visual Studio error while building CUDA for installing Opencv GPU

12.3. If you are getting error in Visual Studio saying:

12.3.1. In Visual Studio go to Tools > Options
12.3.2. In Options panel expand “Projects and Solutions” section > Web Projects
12.3.3. Uncheck the last option in that window

Fix visual studio error while building opencv with cuda

12.3.4. Click on OK button and restart Visual Studio by following Step 12.1 and 12.2

12.4. Now inside Visual Studio change “debug” mode to “release” Mode

12.5. Inside Visual Studio expand “CMakeTargets” (Located at right)
12.6. Right-click “ALL_BUILD” and click build. This may take around 30 minutes to complete.

12.7. Now right-click “INSTALL” (from same “CMakeTargets”) and click build. This wont take long time.

12.8. Make sure that, there is no error or failed in Step 12.6 and 12.7
12.9. Finally you built and compile opencv with cuda and cuDNN. Now you can utilize your NVIDIA GPU while working with opencv.

Step13 | Verify whether installations are correctly done

To check whether you have sucessfully installed opencv with cuda for windows or not.

13.1. Open the command prompt > type python > hit Enter
13.2. Type the below code in the python console

import cv2 cv2.__version__ cv2.cuda.getCudaEnabledDeviceCount()

Conclusion

I know this is a very long and hectic process to build OpenCV with Cuda to install OpenCV for GPU. But I think it is worth doing this to minimize our day-to-day video analysis time, increase the durability of your system as while doing video analysis with CPU, your CPU utilization will be almost 100% all the time.

Anindya Naskar

Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.

Источник

Оцените статью