Experience sharing of win10 building opencv-gpu and anaconda-python environment from scratch

Introduction

Ordinary opencv's videocapture is too slow to read video, and it takes up a lot of cpu resources. When writing inference scripts, cuda and corresponding operators need to be used to accelerate, so there is a need to build a gpu version of opencv. At the same time, because the main language is python , the gpu -related interface of opencv needs to be installed in python. Since most of the related constructions are carried out under linux on the Internet, there are almost none under win , so it is recorded here to help future generations.

preparation and steps

Hard requirements:
an Nvidia graphics card that supports CUDA, and the experimental environment is RTX3060.
Bring your own proxy tool, so that cmake cannot download some needed things due to network problems when building.
If you can uninstall it, uninstall the built-in python, or you don’t need to uninstall it.
Please uninstall the original opencv python package.
win10 64-bit system.

Content to be downloaded:
1. For the installation package of Anaconda , just download the latest version from the official website.
2. For the source code of opencv and opencv-contrib , you need to select the same version in tags, that is, the version, and then click the green button to download the entire compressed package. Here I choose 4.5.2.
insert image description here
insert image description here
3. The cmake installation package , download address , select cmake-3.23.2-windows-x86_64.msi under Windows x64 Installer.
4. For the installation package of visual studio 2017/2019/2022 , 19 and 22 are recommended here, and 17 will stop supporting sooner or later according to the warning. In addition, when downloading the old version of visual studio, you need to log in to your Microsoft account, please bring your own.
5. The cuda installation package and the cudnn tool set are not discussed in detail here. Search by yourself. It should be noted that the version of cudnn should correspond to cuda. ​​I installed 11.6.

Steps:
1. Install anaconda . Here, you should check the add path to variable during the installation process, which is to add it to the environment variable. You can directly enter python on the command line to call up the corresponding environment (I am python3.9.12 here). In addition, you need to pay attention to whether you have installed other versions of the python environment .
Open anaconda and search for the libprotobuf package.
insert image description here
If so, uninstall it!
If so, uninstall it!
If so, uninstall it!

Because cmake will generate a protobuf by default and this is in conflict, and various errors such as LNK1104 and C1189 will be reported later in the vs build!
insert image description here

2. Unzip opencv and opecv-contrib , put them in the same directory, and create a new folder build as the cmake build directory. The advantage of this is clear.
insert image description here
3. Install cmake , also remember to check Add environment variable, remember to type cmake on the command line after installation to see if it is recognized, and the installation is successful if it is recognized.
insert image description here

4. Install visual studio . When selecting modules, you only need to select the C++ module, and the others are not used.
insert image description here
5. Install cuda and copy the contents of cudnn to the corresponding directory (if you are not sure about the directory, please refer to my previous article on configuring cudnn). The reason why this part is placed behind vs is because the Nsight series components will be installed into vs for configuration when cuda is installed.

6. Open cmake-gui , which is to install the icon on your desktop, select the directory you want to build (that is, where you want to build it) and the source code directory (that is, opencv).
insert image description here
Then click configure, and wait patiently for it to download all the things. If the network is not good and an error is reported, please see the mandatory requirements.
insert image description here
After the first configure, a lot of red fields will pop up for you to configure. Let's search for python first:
insert image description here
check the opencv_python3_version option, and check the python3 path below.
insert image description here
Here is the key point, if it is a new computer that has not installed python, it will be automatically configured as the default python path of anaconda, yes, if you have installed other python, you can either manually configure it, or use pip to upgrade numpy (it will be locked to the python environment where numpy is located). In addition, if your anaconda has created multiple environments, please check carefully whether it is installed in the environment you want.

Then search for world and check opencv_world.
insert image description here
Search for extra, the configuration directory is the directory where opencv_contrib is located.
insert image description here
Search conf, change debug and release to only release.
insert image description here
Search for math and tick all the boxes.
insert image description here
Search for cuda, check all the items that can be ticked according to the picture, click configure after ticking here , and some options will appear, such as cuda_arch_bin , which must be configured.
insert image description here
There are many version numbers in cuda_arch_bin at the beginning, which correspond to different graphics cards. My 3060 corresponds to 8.0, and the corresponding version can be viewed on the corresponding
insert image description here
page (ctrl+f search on the page directly) .

7. Open the generated opencv.sln. insert image description here
Opening VS for the first time may ask you to log in to your account or something, just skip it. Then you don't have to wait for the entire project to be loaded, just right-click INSTALL and select Generate.
insert image description here
If the project does not report an error but only a warning, after a long wait of 1-2 hours, congratulations on your success, as follows:
insert image description here
8. Verify.

##打开命令行输入python调起环境后输入以下代码验证
import cv2
from cv2 import cuda
cuda.printCudaDeviceInfo(0)

If everything is OK, the information of your graphics card will be printed out, indicating that you have succeeded.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43945848/article/details/125177533