构建带有CUDA的OpenCV时出现”无法打开包括文件:’Python.h'”的错误。

huangapple go评论61阅读模式
英文:

Building OpenCV with CUDA gives "Cannot open include file: 'Python.h' "

问题

最近我想要构建支持CUDA的OpenCV,但每次尝试构建时都会遇到这个错误:

F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Python.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_highgui.cpp) [F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Python.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.cpp) [F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Python.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_numpy.cpp) [F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Python.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_util.cpp) [F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Python.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_conve

其他信息如下:

  • Python版本为3.9

  • OpenCV从GitHub拉取于2023年2月17日,版本为4.7.0

  • OpenCV Contrib也从GitHub拉取,版本为4.x

  • 我还尝试安装了Visual Studio 2022的Build Tools

  • 使用Visual Studio 2022和CMake,CMake配置如下:

    General configuration for OpenCV 4.7.0 =====================================
      Version control:               unknown
    
      Extra modules:
        Location (extra):            F:/OpenCV/openCVGPU/opencv_contrib-4.x/modules
        Version control (extra):     unknown
    
      Platform:
        Timestamp:                   2023-02-17T14:01:27Z
        Host:                        Windows 10.0.19044 AMD64
        CMake:                       3.24.2
        CMake generator:             Visual Studio 17 2022
        CMake build tool:            C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe
        MSVC:                        1933
        Configuration:               Release
        ...
    

    确保所有Path环境变量都已设置。请帮我解决构建错误吗?

英文:

recently I wanted to build OpenCV to run with CUDA support, but every time when I try to build it it gives me this error:

F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Pyt
hon.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_highg
ui.cpp) [F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Pyt
hon.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.cpp)
[F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Pyt
hon.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_numpy
.cpp) [F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Pyt
hon.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_util.
cpp) [F:\Build\modules\python3\opencv_python3.vcxproj]
F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2.hpp(20,10): fatal  error C1083: Cannot open include file: 'Pyt
hon.h': No such file or directory (compiling source file F:\OpenCV\openCVGPU\opencv-4.7.0\modules\python\src2\cv2_conve
  • Python Version is 3.9
  • OpenCV is pulled from GitHub at 17/02/2023 - Version 4.7.0
  • OpenCV Contrib is also pulled from Github - Version 4.x
  • I also tried to install Build Tools for Visual Studio 2022

Am using Visual Studio 2022 with Cmake, the Cmake configuration is:

General configuration for OpenCV 4.7.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            F:/OpenCV/openCVGPU/opencv_contrib-4.x/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2023-02-17T14:01:27Z
    Host:                        Windows 10.0.19044 AMD64
    CMake:                       3.24.2
    CMake generator:             Visual Studio 17 2022
    CMake build tool:            C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe
    MSVC:                        1933
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (18 files):         + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (34 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (8 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe  (ver 19.33.31630.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MD /O2 /Ob2 /DNDEBUG 
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MDd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /MP   /MD /O2 /Ob2 /DNDEBUG 
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /MP /MDd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO 
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL 
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          cudart_static.lib nppc.lib nppial.lib nppicc.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:F:/CUDA/lib/x64 -LIBPATH:F:/CUDA/lib
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode world xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    -
    Disabled by dependency:      -
    Unavailable:                 alphamat cvv freetype hdf java julia matlab ovis python2 python2 sfm viz
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.13)
    JPEG:                        build-libjpeg-turbo (ver 2.1.3-62)
      SIMD Support Request:      YES
      SIMD Support:              NO
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   F:/Build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                F:/Build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)

  NVIDIA CUDA:                   YES (ver 11.8, CUFFT CUBLAS FAST_MATH)
    NVIDIA GPU arch:             75
    NVIDIA PTX archs:            75

  cuDNN:                         YES (ver 8.5.0)

  OpenCL:                        YES (NVD3D11)
    Include path:                F:/OpenCV/openCVGPU/opencv-4.7.0/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/python.exe (ver 3.10.9)
    Libraries:                   C:/Users/Rufo123/AppData/Local/Programs/Python/Python310/libs/python310.lib (ver 3.10.9)
    numpy:                       C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/numpy/core/include (ver )
    install path:                C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/cv2/python-3.10

  Python (for build):            C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/python.exe

  Java:                          
    ant:                         NO
    JNI:                         C:/Program Files/Eclipse Adoptium/jdk-17.0.3.7-hotspot/include C:/Program Files/Eclipse Adoptium/jdk-17.0.3.7-hotspot/include/win32 C:/Program Files/Eclipse Adoptium/jdk-17.0.3.7-hotspot/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    F:/Build/install

I also made sure that all Environment Variables in Path are set.
Could you please help me with the build error?

(Also note, that I am using Windows 10)

答案1

得分: 0

在经过多次尝试和错误之后,我终于成功构建了它,并解决了import cv2不起作用的问题。

首先,我注意到CMake不知何故分配了错误版本的Python。我使用的是Python 3.9.11。在这里:

Python 3:
Interpreter: C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/python.exe (版本 3.10.9)
Libraries: C:/Users/Rufo123/AppData/Local/Programs/Python/Python310/libs/python310.lib (版本 3.10.9)
numpy: C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/numpy/core/include (版本)
install path: C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/cv2/python-3.10

重置缓存后,现在看起来是这样的:

Python 3:
Interpreter: C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/python.exe (版本 3.9.11)
Libraries: C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/libs/python39.lib (版本 3.9.11)
numpy: C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/lib/site-packages/numpy/core/include (版本 1.24.2)
install path: C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/cv2/python-3.9

在这之后,构建成功,没有出现错误。但是还有一个小问题,在尝试import cv2时出现了以下错误:

ImportError: 导入 cv2 时加载 DLL 失败:找不到指定的模块

所以我更新了系统环境变量(PATH和PYTHONPATH),并包括了我能想到的一切:

PATH:

f:\Build\lib\python3\Release\
f:\Build\
f:\Build\install\x64\vc17\bin\
F:\CUDA\bin
F:\CUDA\libnvvp

PYTHONPATH:

f:\Build\python_loader\
f:\Build\bin\Release\

在这之后,错误稍微改变了,并且我注意到现在Python实际上可以看到构建的OpenCV库。

所以我进行了大量的谷歌搜索,这篇帖子帮助了我:importerror-dll-load-failed-while-importing-cv2-the-specified-module-could-not-be-found

然后,我编辑了python_loader/cv2中的config.py文件:

import os

BINARIES_PATHS = [
    'F:/Build/bin/Release',
    os.path.join('f:\Build\install', 'x64\vc17\bin'),
    os.path.join(os.getenv('CUDA_PATH', 'c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8'), 'bin')
] + BINARIES_PATHS

所以经过一番努力,我认为**'CUDA_PATH'** 变量在系统环境变量中缺失

还要感谢@Brian 指引我找到正确的方向。

英文:

Hi so after a lot of trial and error, I finally built it successfully and also resolved import cv2 not working.

So First of all I noticed that Cmake somehow assigned wrong version of python
I am using 3.9.11. Here:

  Python 3:
Interpreter:                 C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/python.exe (ver 3.10.9)
Libraries:                   C:/Users/Rufo123/AppData/Local/Programs/Python/Python310/libs/python310.lib (ver 3.10.9)
numpy:                       C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/numpy/core/include (ver )
install path:                C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/cv2/python-3.10

After reseting cache now it looks like this:

  Python 3:
Interpreter:                 C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/python.exe (ver 3.9.11)
Libraries:                   C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/libs/python39.lib (ver 3.9.11)
numpy:                       C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/lib/site-packages/numpy/core/include (ver 1.24.2)
install path:                C:/Users/Rufo123/AppData/Local/Programs/Python/Python39/Lib/site-packages/cv2/python-3.9

After this, the build succeded without errors. But there was another catch, after trying to import cv2. This error occured:

ImportError: DLL load failed while importing cv2: The specified module could not be found

So I Updated System Environment Variables (PATH and PYTHONPATH) and included everything that came to my mind:
PATH:

f:\Build\lib\python3\Release\
f:\Build\
f:\Build\install\x64\vc17\bin\
F:\CUDA\bin
F:\CUDA\libnvvp

PYTHONPATH:

f:\Build\python_loader\
f:\Build\bin\Release\

After this the error slightly changed and I noticed that now, the python can actually see the built OpenCV Library.

So I googled a lot and this post helped me: importerror-dll-load-failed-while-importing-cv2-the-specified-module-could-not-be-found

And I edited the config.py in python_loader/cv2

import os

BINARIES_PATHS = [
    'F:/Build/bin/Release',
    os.path.join('f:\Build\install', 'x64\vc17\bin'),
    os.path.join(os.getenv('CUDA_PATH', 'c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8'), 'bin')
] + BINARIES_PATHS

So after all I think that 'CUDA_PATH' variable vas missing from System Environment Variables.

Also thanks to @Brian for pointing me in the right direction.

huangapple
  • 本文由 发表于 2023年2月18日 03:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定