构建 cv2 后,cv2 未导入

发布于 2025-01-15 11:56:20 字数 563 浏览 0 评论 0原文

所以我使用 这个 指南几乎完全“完全”遵循它。但是当我尝试 import cv2 时,我收到此错误:

ModuleNotFoundError: No module named 'cv2'

我害怕使用 pip 来安装 OpenCV,担心它会安装基于 CPU 的 OpenCV。我需要做什么才能让 python 看到我手动构建的?也许与简历不在路径中有关?

仅供参考,我没有从我使用 Microsoft 商店的网站安装 python。我安装了Python 3.10

So I manually built OpenCV using this guide and followed it almost exactly "to the T" besides using a newer version of CUDA and CUDNN. But when I try to import cv2 I get this error:

ModuleNotFoundError: No module named 'cv2'

I'm afraid to use pip to install OpenCV afraid it will install it's CPU based OpenCV. What do I need to do to get python to see I manually built? Maybe something to do with CV not being in the PATH?

Just FYI I did not install python from the site I used the Microsoft store. And I installed Python 3.10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转瞬即逝 2025-01-22 11:56:20

这不是在 Windows 10 上的 anaconda 环境上构建支持 cuda 的 open-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 0

This is not an official manual to build open-cv with cuda support on anaconda envioroment on windows 10 - I hope it'll help. Also, at the end I also added stubs for open-cv (after manual build there is no auto complete inside the IDE, adding the stubs will fix it)

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()

Output of the function:

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 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文