因此,我使用OpenCV指南手动构建了这,除了使用新版本的CUDA和CUDNN之外,它几乎完全遵循了“到T”。但是,当我尝试import cv2时,我会得到以下错误:
ModuleNotFoundError: No module named 'cv2'我害怕使用pip来安装OpenCV,害怕它会安装基于CPU的OpenCV。要让python看到我手动构建,我需要做什么?也许是因为简历没在路上?
只是FYI我没有从我使用微软商店的站点安装python。我安装了Python3.10
发布于 2022-03-20 10:22:32
这不是一本官方手册,可以在windows 10上的anaconda环境下使用cuda支持构建开放式cv--我希望它会有所帮助。此外,最后我还为open-cv添加了存根(手动构建之后,IDE内部没有自动完成,添加存根将修复它)。
import cv2
import numpy as np
from timeit import default_timer as timer
def cuda_on_gpu_test():
"""
opencv cuda installation on WINDOWS 10:
youtube https://www.youtube.com/watch?v=YsmhKar8oOc&ab_channel=TheCodingBug
** all links for download in the link
** in brackets: my specific installation
* install anaconda
* vs studio 19 with "desktop development C++" and "python development"
* Cuda toolkit (cuda toolkit 10.2)
* cuDNN - version that match the cuda toolkit above (cuDnn v7.6.5 for Cuda 10.2)
* archive: https://developer.nvidia.com/rdp/cudnn-archive
* copy all to cuda (bin to bin, lib to lib, include to include)
* minimum 7.5
* CMake
* openCV source (4.5.2)
* openCV contrib - same version as openCV above (4.5.2)
* place openCV and openCV contrib in the same folder and extract both
1 build a new python env anywhere(conda(base or custom), normal py, venv...)
2 set 4 system environment variables:
let PY_PATH be you python dir (conda env, normal py, venv...)
go to PATH and add the next 4:
* PY_PATH # for the python.exe
* PY_PATH/Scripts # for pip...
* PY_PATH/libs # for python36.lib file
* PY_PATH/include # h files
2b pip install numpy (if you want tf, 1.19.5)
3 open CMake gui
* source code: location of openCV source
* create 2 new dirs in the level of openCV source called build and install
* where to build: location of build folder created above
* configure, set generator x64 and finish
* click grouped checkbox and look in PYTHON3
should have EXECUTABLE, INCLUDE_DIR, LIBRARY, NUMPY_INCLUDE, PACKAGES paths filled according to PY_PATH
look at the output and search for OpenCV modules. if python3 in Unavailable - don't continue. it should be
in the "To be built"
* extra round of flags:
WITH_CUDA
BUILD_opencv_dnn
OPENCV_DNN_CUDA
ENABLE_FAST_MATH
BUILD_opencv_world
BUILD_opencv_python3 # should already be on
OPENCV_EXTRA_MODULES_PATH -> set to openCV contrib on modules folder
* hit configure - in the output you should see your CUDA and cuDNN details
CUDA detected: x.y (10.2)
Found CUDNN path ...
* extra round of flags:
CUDA_FAST_MATH
CUDA_ARCH_BIN -> go to https://en.wikipedia.org/wiki/CUDA and look for your GPU. find it's Compute
capability (version). (my GPU is gtx 1050. version 6.1)
remove all versions other than you GPU version. (i left only 6.1)
CMAKE_INSTALL_PREFIX -> place install path from above (you create this dir with build above)
CMAKE_CONFIGURATION_TYPES -> remove Debug and keep only Release
* hit configure
* hit generate
close CMAKE gui
4 go to build folder and look for OpenCV.sln
* goto solution explorer->CMakeTargets-> right click ALL_BUILD and hit build # should take 10-30 mins
* if done with no errors, right click INSTALL and build.
* if no errors, openCV cuda is ready
5 validation: open terminal and write python (should work due to step 2)
import cv2
print(cv2.__version__)
print(cv2.cuda.getCudaEnabledDeviceCount()) # should be >=1
cuda_on_gpu_test() # run this function
6 cleanup
* delete all build folder except build/lib # maybe for future use ?
* delete both zips and extracted open cv and open cv contrib
7 after checking on pycharm, open cv with GPU support should work but no autocomplete.
pip install mypy
stubgen -m cv2 -o ENV_PATH\Lib\site-packages\cv2
# if the stubgen fails, run as administrator
# rename cv2.pyi created to __init__.pyi
# all done
e.g:
stubgen -m cv2 -o C:\\Users\\GiladEiniKbyLake\\.conda\\envs\\cv_cuda\\Lib\\site-packages\\cv2
# a file was created at C:\\Users\\GiladEiniKbyLake\\.conda\\envs\\temp\\Lib\\site-packages\\cv2\\cv2.pyi
# rename cv2.pyi to __init__.pyi and have the file:
C:\\Users\\GiladEiniKbyLake\\.conda\\envs\\temp\\Lib\\site-packages\\cv2\\__init__.pyi
:return:
"""
print(cv2.getVersionString())
gpus = cv2.cuda.getCudaEnabledDeviceCount()
print('\t{} GPU devices detected'.format(gpus))
if gpus > 0:
npTmp = np.random.random((1024, 1024)).astype(np.float32)
npMat1 = np.stack([npTmp, npTmp], axis=2)
npMat2 = npMat1
cuMat1 = cv2.cuda_GpuMat()
cuMat2 = cv2.cuda_GpuMat()
cuMat1.upload(npMat1)
cuMat2.upload(npMat2)
# start_time = time.time()
start_time = timer()
# noinspection PyUnresolvedReferences
cv2.cuda.gemm(cuMat1, cuMat2, 1, None, 0, None, 1)
print("\t\tCUDA --- %s seconds ---" % (timer() - start_time))
# start_time = time.time()
start_time = timer()
cv2.gemm(npMat1, npMat2, 1, None, 0, None, 1)
print("\t\tCPU --- %s seconds ---" % (timer() - start_time))
return
def main():
cuda_on_gpu_test()
return
if __name__ == '__main__':
main()职能产出:
C:\Users\GiladEiniKbyLake\.conda\envs\MVS_cuda_3_6\python.exe D:/workspace/codeBin/wizzi_utils_test/cv_cuda_test_no_wu.py
4.5.2
1 GPU devices detected
CUDA --- 0.2076618 seconds ---
CPU --- 2.2205587999999996 seconds ---
Process finished with exit code 0https://stackoverflow.com/questions/71535649
复制相似问题