CMake配置带GPU加速的OpenCV时无法识别Python3组件
编译带GPU加速的OpenCV时CMake无法识别Python3组件问题
我严格按照教程步骤尝试通过CMake在笔记本上编译安装带GPU加速的OpenCV,但CMake始终无法识别Python3的相关组件(包括解释器、库、numpy及安装路径),已尝试重新安装所有必要软件及其他解决方案,均未奏效。
操作步骤
- 安装anaconda、cmake、VS 2019 16版、CUDA及cuDNN;
- 从GitHub下载opencv-4.8.0及opencv_contrib-4.8.0;
- 在包含上述两个文件夹的
opencv_gpu目录内创建build文件夹; - 在CMake中配置,指定opencv-4.8.0为源码,VS 2019 16版为生成器;
- 勾选
WITH_CUDA、OPENCV_DNN_CUDA、ENABLE_FAST_MATH参数,并设置OPENCV_EXTRA_MODULES_PATH为D:\opencv_gpu\opencv_contrib-4.8.0\modules; - 重新配置后勾选
CUDA_FAST_MATH,将CMAKE_CONFIGURATION_TYPES设置为仅Release,CUDA_ARCH_BIN设置为8.6(我的GPU为NVIDIA RTX 3050,计算能力为8.6); - 执行Configure→Generate;
- 运行命令
cmake --build "D:\opencv_gpu\build" --target INSTALL --config Release; - 完成操作。
配置时的警告信息
每次配置都会出现以下警告,我认为是后续错误的诱因:
Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules are removed. Run "cmake --help-policy CMP0148" for policy details. Use the cmake_policy command to set the policy and suppress this warning. Call Stack (most recent call first): cmake/OpenCVDetectPython.cmake:64 (find_host_package) cmake/OpenCVDetectPython.cmake:280 (find_python) CMakeLists.txt:649 (include) This warning is for project developers. Use -Wno-dev to suppress it.
最终错误现象
操作完成后,尝试导入cv2时出现如下错误:
Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'cv2'
配置输出对比
预期输出(含Python3相关信息)
. . . -- OpenCl: YES(NVD3D11) -- Include path: D:/opencv_gpu/opencv-4.8.0/3rdparty/i -- Link libraries: Dynamic load -- Python 3: -- Interpreter: D:/anaconda3/python.exe (ver 3.10.9) -- Libraries: {path to directory} -- numpy: {path to directory} -- packages path: {path to directory} -- Python (for build): D:/anaconda3/python.exe . . .
实际输出(缺少Python3相关内容)
. . . -- OpenCl: YES(NVD3D11) -- Include path: D:/opencv_gpu/opencv-4.8.0/3rdparty/i -- Link libraries: Dynamic load -- Python (for build): D:/anaconda3/python.exe . . .
补充信息
- CUDA安装验证:执行
nvcc -V输出如下:
nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2021 NVIDIA Corporation Built on Wed_Jul_14_19:47:52_Pacific_Daylight_Time_2021 Cuda compilation tools, release 11.4, V11.4.100 Build cuda_11.4.r11.4/compiler.30188945_0
- numpy版本:执行
pip install --upgrade numpy后显示已安装最新版本:
Requirement already satisfied: numpy in d:\anaconda3\lib\site-packages (1.25.1)
内容的提问来源于stack exchange,提问作者Dylan Mac
相关产品推荐
相关产品推荐

