2024-01-23-CMake編譯OpenCV-Python
Here is the minimal steps.
Here is my enviornment
- Windows 10
- Visual Studio Build Tools 2019
- CMake 3.29.0-rc1 (use default install setting)
- Gstreamer:(Use default install setting)
MSVC 64-bit (VS 2019, Release CRT)
1.22.10 runtime installer
1.22.10 development installer - Python3.8.10
For example, my gstreamer is install in D disk by default.
- Add System Environment Variable
GSTREAMER_ROOT_X86_64
valueD:\gstreamer\1.0\msvc_x86_64
gstreamer doc - Add enviornment variable
GST_PLUGIN_PATH
valueD:\gstreamer\1.0\msvc_x86_64\lib\gstreamer-1.0
- Add
Path
enviornment variable valueD:\gstreamer\1.0\msvc_x86_64\bin
- re-login the computer
- create a Python venv and use it
pip install --verbose --no-binary opencv-python opencv-python
- os.add_dll_directory() Shoud be add to code since from Python 3.8+, Python will NOT search DLL from PATH enviornment variable.ref
- test code
1 | import os |
步驟
- 安裝msvc2019 build tool
- 安裝python3.10.10
- 安裝gstreamer 1.25.90 runtime installer和development installer 都必須要裝!!
- 設定GSTREAMER_ROOT_X86_64環境變數
gstreamer doc
<gstreamer資料夾>\1.0\msvc_x86_64
- 直接安裝source distributions版本的opencv-python(opencv 4.3.0之後的版本)
pip install --verbose --no-binary opencv-python opencv-python
GST_PLUGIN_PATH或是GST_PLUGIN_SYSTEM_PATH(不一定要加)
gstreamer預設尋找plugin的順序如下
- %HOMEDRIVE%%HOMEFOLDER%/.gstreamer-1.0/plugins
- C:\gstreamer\1.0\x86\lib\gstreamer-1.0
..\lib\gstreamer-1.0 - %GST_PLUGIN_PATH%
<gstreamer資料夾>\1.0\msvc_x86_64\lib\gstreamer-1.0
Path變數
<gstreamer資料夾>\1.0\msvc_x86_64\bin
載入時手動加入dll路徑
os.add_dll_directory()
ref
測試
1 | import os |
gstreamer載入時錯誤檢查
- 用 Dependencies 軟體檢查dll缺少哪一些東西
- 下載後免安裝直接打開DependenciesGui.exe,打開venv\Lib\site-packages\cv2\cv2.pyd
- plugin找不到的錯誤可能是找不到plugin的dll本身,或是找不到plugin所需要的dll
安裝CUDA
CUDA 設定
從4.0之後CUDA相關的功能被移到opencv_contrib,必須要編一opencv_contrib才能啟用CUDA功能
set CMAKE_ARGS="-DWITH_CUDA=ON"
- pip install –verbose –no-binary opencv-contrib-python opencv-contrib-python
(選擇性)G-API
2024-01-23-CMake編譯OpenCV-Python