Opencv-python uses GPU resources--virtual environment installation and compilation of opencv source code

Preparation

Platform and Software

  • Windows10 system
  • Visual Studio 2019:Visual Studio Community 2019
  • Cmake:cmake-3.20.0-rc3-windows-x86_64.msi
  • OpenCV 4.51:opencv-4.5.1.tar.gz
  • OpenCV_contrib 4.5.1:opencv_contrib-4.5.1.tar.gz

NVIDIA driver, CUDA and cuDnn

Select the appropriate driver and CUDA version and the corresponding version of cudnn, and install the win10 environment.

Correspondence between NVIDIA microarchitecture, CUDA and graphics card model

Install and download VS 2019, cmake, opencv and extension module source code

VS 2019 download and install

download link

cmake download and install

download link

opencv and opencv_contrib download

opencv download link
opencv_contrib

Baidu cloud download
extraction code:lujx

Note: Click the Tag in the upper left corner, select version 4.5.4, select the same version for opencv and opencv_contrib

Unzip the files and you are ready

Create opencv_cudaa folder, unzip opencvand opencv_contribunzip it into this folder, and create a build folder in the same directory as opencv. At this point the preparations are complete.
The directory structure is roughly as follows:

|-opencv_cuda
	|--build
	|--opencv_contrib_4.5.4
	|--opencv_4.5.4
		 |---.cache
		 |---其他原有的解压的子文件
	

In order to avoid compilation failure due to file download failure during the first compilation process, you need to copy the .cache folder to the decompressed opencv source folder

Compile OpenCV

CMake compile

  1. Open cmake-guithe software, respectively add where is the source codeand where to build the binariesas opencv源码文件夹andbuild文件夹
  2. First click configure, select vs 2019and x64architecture, click finish, start the first compilation
  3. The compilation process needs to download various dependencies. There is a high probability that it will be stuck due to network problems. Copying the .cache folder directly can save this trouble
  4. (This step is optional) Create a virtual environment, it is recommended to use anaconda, and install numpy in the virtual environment (required for compilation), this step is to install CUDA版本opencv into 虚拟环境it,只安装到宿主机环境不需要执行此步骤
  5. (This step is optional, but the prerequisite for performing this step is that the previous step must be performed) Change a few variables and point the path to the corresponding location of the virtual environment: PYTHON3_EXECUTABLE, PYTHON3_INCLUDE_DIR, PYTHON3_LIBRARY, PYTHON3_NUMPY_INCLUDE_DIRS,PYTHON3_PACKAGES_PATH
  6. After the compilation is complete, enter CUDAand in the Search box fast, and check three configurations: WITH_CUDA , OPENCV_DNN_CUDA,ENABLE_FAST_MATH
  7. The Search box worldwill build_opencv_worldbe checked, and all opencv libraries will be compiled together without the need to add each small module one by one.
  8. Search box BUILD, checkBUILD_opencv_python3
  9. Search in the search box MODULES, in OPENCV_EXTRA_MODULES_RATHan item, add the opencv_contrib4.5.1directorymodules
  10. search box search NON, OPENCV_ENABLE_NONFREE tick
  11. Click the second time configureand wait for the log below to displayconfigure done
  12. Enter in the search box cuda, check it CUDA_FAST_MATH , and CUDA_ARCH_BINchange the computing power content of the graphics card to the computing power of your own graphics card. The corresponding computing power and graphics card model are shown in the picture in Chapter 1. For example, if the graphics card model is and the corresponding computing power is , GTX 1050delete 6.1it For other computing power versions, just keep 6.1it
  13. Click configure again, this time the Configuring done is finally OK, then click Generate, wait a moment for Generating done to appear!
  14. Click Open Project and it will start your Visual Studio.

VS compile

  1. After VS2019 opens the newly compiled project, it will respond for a while. You must wait for all the items displayed in the lower left corner to be loaded before proceeding.

  2. Select Release ``x64, then find CmakeTargetsthe next one ALL_BUILD, right click → "Generate", and then start a long wait... (The notebook i7-9750H compiles for about 65 minutes, for reference only)

  3. 解决方案资源管理器—> CMakeTargets—> INSTALL—> 生成"Then wait again, fortunately this time is very short. At this time, opencv_cuda\build\lib\python3\Releaseyou can see the file under the folder cv2.cp36-win_amd64.pyd(different python versions, the name will be slightly different)

  4. At the same time, in the virtual environment or host environment, you can Lib\site-packagessee cv2the folder under the path

Verify the opencv environment

Use the command line to enter the python environment and execute the code to verify:

c:\users\administrator> python
>>> import cv2
>>> cv2.cuda.getCudaEnabledDeviceCount()
1 # 得到GPU设备数量,即表示opencv的GPU版本已经安装成功

Guess you like

Origin blog.csdn.net/LJX_ahut/article/details/121510097