win10 vs2019 编译 opencv4.7 + pyhton3 +cuda
20201017更新
前置条件
vs2019
也就是所谓的vc16. 要检测是否安装了完整的C++功能和cmake支持,用vs 的installer可以选上。
否则cmake的configure都会报错。
参考 https://blog.csdn.net/diaodaa/article/details/106122943
1 下载源码
https://github.com/opencv/opencv/archive/master.zip
https://github.com/opencv/opencv_contrib/archive/master.zip
2 cmake configure
右键用管理员权限启动cmake。否则configure会报各种错误,包括找不到Build_opencv_world选项等等等奇怪的错误。
设置源码的路径和编译出的vc++ 工程路径(.sln)。 简单用源码路径内build就好。没有会新建。
第二个路径有歧义 是build文件夹 而不是 build之后的binary
开启迷信上网,因为config过程中可能就会下载一些文件
点击configure
选上
BUILD_opencv_world
设置真正的build之后的binary路径
EXECUTABLE_OUTPUT_PATH 比如 D:/lib/opencv-4.7
NONFREE必须开,否则即使下载的opencv_contrib也不包含sift等等nonfree库了
注意路径中的分割线,必须是linux的
D:/opencv/4.7/opencv_contrib-master/modules
cuda:
参考 https://www.jianshu.com/p/aa8455fcc672
选上
WITH_CUDA
WITH_CUDNN
修改 CUDA_ARCH_BIN
这个针对不同计算能力的GPU,编译不同版本,但是这部分特别慢,只能单线程编译。
只编译自己用到的,或者可能部署用到的就可以。
参考 https://www.cnblogs.com/xuanmanstein/p/10082831.html
在这里查看 显卡能力 https://developer.nvidia.com/cuda-gpus
我能用到的显卡基本也就是
GeForce GTX 1080 Ti | 6.1 |
GeForce GTX 1080 | 6.1 |
GeForce GTX 1070 | 6.1 |
GeForce GTX 1060 | 6.1 |
GeForce GTX 980M | 5.2 |
所以只保留这2个就可以啦。
MKL lapack
矩阵计算依赖blas,lapack。 opencv2.x时代是包含的,现在不包含了,需要自己外部安装。
lapack和 openblas在win下编译都依赖mingw,和vc八字不合,导致opencv 的 OpenCVFindLAPACK.cmake 产生的测试代码无法编译。
openblas LAPACK(OpenBLAS): Can't build LAPACK check code. This LAPACK version is not supported.
win下lapack最简单的或者唯一的就是mkl。
现在intel mkl免费了,只需要注册就可以下载 https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-download.html
现在叫oneAPI了,而且可以不安装intel的C++编译器。只下载安装mkl和tbb。非常方便,体积1.1G,安装后8G
在cmake里只需要设置MKL_ROOT_DIR 其他都会自动检测
TBB
要下载github的onetbb源码, https://github.com/oneapi-src/oneTBB 而不是 和mkl一起的那个tbb。那个缺少include 和cmake,opencv不认?
总之,这样
其他
编译python 无法打开文件“python38_d.lib”
编译debug的时候,产生,发现即使重新安装python时选择下载debug文件,仍然会提示缺这个,主要是Cmake依赖的python lib文件默认就是release版的。
所以只能硬改。
用管理员权限启动vscode或者其他文本编辑器
打开文件夹 C:\Program Files\Python38\include
pyconfig.h
276行,把python38_d.lib改成python38.lib
/* For an MSVC DLL, we can nominate the .lib files used by extensions */ #ifdef MS_COREDLL # if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN) /* not building the core - must be an ext */ # if defined(_MSC_VER) /* So MSVC users need not specify the .lib file in their Makefile (other compilers are generally taken care of by distutils.) */ # if defined(_DEBUG) # pragma comment(lib,"python38.lib") # elif defined(Py_LIMITED_API) # pragma comment(lib,"python3.lib") # else # pragma comment(lib,"python38.lib") # endif /* _DEBUG */ # endif /* _MSC_VER */ # endif /* Py_BUILD_CORE */ #endif /* MS_COREDLL */
319行
#ifdef _DEBUG //# define Py_DEBUG #endif
object.h
59行
/* Py_DEBUG implies Py_REF_DEBUG. */ #if defined(Py_DEBUG) && !defined(Py_REF_DEBUG) // #define Py_REF_DEBUG #endif
保存。
在vs里再次生成 “ALL_BUILD”,就OK了
基本上多点几次Configure,直到不报错就OK。
如果报warning:
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install directory Call Stack (most recent call first)
去掉这个编译选项,应该没啥用
python3路径现在已经能自动识别
python3的路径一个一个补上:注意library 很迷惑,有python3.lib和 .a的。注意区别
其他要注意的也就是这些了,那个OPENCV_PYTHON3_VERSION没啥用,选上好像就编译cv2.pyd不过了。保持空就可以。
改host下载文件
参考 https://zhuanlan.zhihu.com/p/337422756
即使迷信上网,也无法下载。发现是dns的问题
ping raw.githubusercontent.com
得到实际ip地址199.232.68.133
用管理员权限打开vscode,打开C:\Windows\System32\drivers\etc\host
添加
199.232.68.133 raw.githubusercontent.com
保存
1 需要迷信下载的缓存文件1:
参考 https://www.cnblogs.com/huluwa508/p/10142718.html
打开 D:/opencv/opencv-4.5.0/build/CMakeDownloadLog.txt
如果正常,应该看到:
#use_cache "D:/opencv/opencv-4.5.0/.cache"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_IPPICV_HASH_3rdparty_ippicv_ippicv_2020_win_intel64_20191018_general_zip"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_ADE_HASH_3rdparty_ade_v0_1_1f_zip"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_dll"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_64_dll"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_ffmpeg_version_cmake"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_bgm_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_bgm_bi_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_bgm_hd_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_binboost_064_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_binboost_128_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_binboost_256_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_BOOSTDESC_HASH_downloads_xfeatures2d_boostdesc_lbgm_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_48_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_64_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_80_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_XFEATURES2D_VGG_HASH_downloads_xfeatures2d_vgg_generated_120_i"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_DATA_HASH_testdata_cv_face_face_landmark_model_dat"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_NVIDIA_OPTICAL_FLOW_HASH_3rdparty_NVIDIAOpticalFlowSDK_1_0_Headers_79c6cee80a2df9a196f20afd6b598a9810964c32_zip"
如果有墙,则看到各种错误和网址,形如:
#do_copy "ffmpeg_version.cmake" "ad57c038ba34b868277ccbe6dd0f9602" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/a66a24e9f410ae05da4baeeb8b451912664ce49c/ffmpeg/ffmpeg_version.cmake" "D:/soft/dev/opencv/opencv-master/build/3rdparty/ffmpeg"
#missing "D:/soft/dev/opencv/opencv-master/build/3rdparty/ffmpeg/ffmpeg_version.cmake"
根据 下载地址依次下载,保存到制定地点
建立D:/opencv/opencv-4.5.0/.cache
1 ippicv
保存到
D:/opencv/opencv-4.5.0/.cache/ippicv/879741a7946b814455eee6c6ffde2984-ippicv_2020_win_intel64_20191018_general.zip
2 ade
https://github.com/opencv/ade/archive/v0.1.1f.zip
保存为
D:/opencv/opencv-4.5.0/.cache/ade/b624b995ec9c439cbc2e9e6ee940d3a2-v0.1.1f.zip
3 ffmpeg
创建 D:/opencv/opencv-4.5.0/build/3rdparty/ffmpeg
下载 3个文件 不需要改md5码
4 xfeatures2d
D:\soft\dev\opencv\4.5\downloads\opencv-4.5.0\build\downloads\xfeatures2d
下载
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i
https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i
5 testdata
创建
D:/opencv/opencv-4.5.0/build/testdata/cv/face/
下载
https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat
6 nvidia_optical_flow
创建
D:/opencv/opencv-4.5.0/.cache/nvidia_optical_flow
下载
https://github.com/NVIDIA/NVIDIAOpticalFlowSDK/archive/79c6cee80a2df9a196f20afd6b598a9810964c32.zip
改名
ca5acedee6cb45d0ec610a6732de5c15-79c6cee80a2df9a196f20afd6b598a9810964c32.zip
#------------------------------------
20200826 更新
opencv 4.4 cuda 10.2
小坑仍然若干(迷信下载,python),但总体平顺多了。漫长的编辑 2小时。
遇到下载不了的,就迷信上网:
ffmpeg ippicv xfeatures2d保存到
/build/downloads 相应文件夹里
face_landmark_model.dat 下载到
/build/testdata\cv\face
具体下载参考下面
20191231更新:
FFMPEG: Download: ffmpeg_version.cmake
Try 1 failed
cd 到
XXX\opencv-master\.cache\ffmpeg
发现
3 Generate 编译安装
然后Generate,然后不要直接在cmake里Open Project,
在开始菜单,在vs2019图标上右键, 更多->以管理员身份运行 (否则,如果python安装在C盘,win10权限管理会让python安装失败)
在vs解决方案管理器
解决方案OpenCV->CMakeTargets
"ALL_BUILD" 右键"生成",经过漫长编译,如果正常,应该全都编译成功。之前因为python编译不过,卡了半天。
“INSTALL” “生成”,(debug 也生成一下 难免要调试)
此时产生
opencv-4.5.0\build\install 文件夹
把
D:\opencv\opencv-4.5.0\build\install\x64\vc16\bin
添加到系统环境变量。
保证opencv_world450.dll能被 搜索到(这个直接300M了。。。)。
4 备用 手动安装opencv_python
但C:\Program Files\Python38\Lib\site-packages下仍然没有看见cv2.pyd
想起应该是win10加强了C盘权限的管理,进命令行都得用管理员权限了。
那么:直接用管理员权限启动PowerShell,cd到之前设置的 opencv的build路径(我给设置了D:\soft\dev\opencv\build)
cd D:\soft\dev\opencv\build\python_loader
python setup.py develop
要用develop来安装,不是常用的install否则会报错
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
https://github.com/opencv/opencv/issues/13202
如果C:\Program Files\Python38\Lib\site-packages还是没有cv2.pyd
那就去
D:\opencv\opencv-4.4.0\build\lib\python3\Release
下
把cv2.cp38-win_amd64.pyd 手工改名成 cv2.pyd
然后复制到
C:\Program Files\Python38\Lib\site-packages
5 检查确认安装成功
在python环境里查看信息
>>> import cv2
>>> print(cv2.getBuildInformation())
General configuration for OpenCV 4.5.0 =====================================
Version control: unknownExtra modules:
Location (extra): D:/opencv/opencv_contrib-4.5.0/modules
Version control (extra): unknownPlatform:
Timestamp: 2020-10-28T07:34:00Z
Host: Windows 10.0.19042 AMD64
CMake: 3.18.4
CMake generator: Visual Studio 16 2019
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/MSBuild.exe
MSVC: 1927CPU/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 (17 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 (31 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (7 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKXC/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe (ver 19.27.29112.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:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /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:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MDd /Zi /Ob0 /Od /RTC1
C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.27.29110/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:precise /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:precise /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:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/lib/x64
3rdparty dependencies:OpenCV modules:
To be built: aruco 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 world xfeatures2d ximgproc xobjdetect xphoto
Disabled: -
Disabled by dependency: -
Unavailable: alphamat cnn_3dobj cvv freetype hdf java js julia matlab ovis python2 python2 sfm viz
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: YESWindows RT support: NO
GUI:
Win32 UI: YES
VTK support: NOMedia I/O:
ZLib: build (ver 1.2.11)
JPEG: build-libjpeg-turbo (ver 2.0.5-62)
WEBP: build (ver encoder: 0x020f)
PNG: build (ver 1.6.37)
TIFF: build (ver 42 - 4.0.10)
JPEG 2000: build (ver 2.3.1)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YESVideo I/O:
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (58.91.100)
avformat: YES (58.45.100)
avutil: YES (56.51.100)
swscale: YES (5.7.100)
avresample: YES (4.0.0)
GStreamer: NO
DirectShow: YES
Media Foundation: YES
DXVA: YESParallel framework: Concurrency
Trace: YES (with Intel ITT)
Other third-party libraries:
Intel IPP: 2020.0.0 Gold [2020.0.0]
at: D:/opencv/opencv-4.5.0/build/3rdparty/ippicv/ippicv_win/icv
Intel IPP IW: sources (2020.0.0)
at: D:/opencv/opencv-4.5.0/build/3rdparty/ippicv/ippicv_win/iw
Lapack: NO
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)NVIDIA CUDA: YES (ver 11.1, CUFFT CUBLAS)
NVIDIA GPU arch: 61
NVIDIA PTX archs:cuDNN: YES (ver 8.0.3)
OpenCL: YES (NVD3D11)
Include path: D:/opencv/opencv-4.5.0/3rdparty/include/opencl/1.2
Link libraries: Dynamic loadPython 3:
Interpreter: C:/Program Files/Python39/python.exe (ver 3.9)
Libraries: C:/Program Files/Python39/libs/python39.lib (ver 3.9.0)
numpy: C:/Program Files/Python39/lib/site-packages/numpy/core/include (ver 1.19.2)
install path: C:/Program Files/Python39/Lib/site-packages/cv2/python-3.9Python (for build): C:/Program Files/Python39/python.exe
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NOInstall to: D:/opencv/opencv-4.5.0/build/install
-----------------------------------------------------------------
编译完成。
然后就能又在py上使用shift了