Build OpenCV (Windows, CUDA) #19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GitHub Actions Workflow: Build opencv-python with CUDA support on Windows | |
| # | |
| # This workflow compiles opencv-python from source with CUDA enabled on a | |
| # GitHub-hosted Windows runner. The resulting Python wheel is then uploaded | |
| # as a build artifact. | |
| # | |
| # This is a complex and long-running process. It is configured to run only | |
| # on manual trigger (workflow_dispatch). | |
| name: Build OpenCV (Windows, CUDA) | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| runs-on: windows-latest | |
| defaults: | |
| run: | |
| shell: pwsh | |
| steps: | |
| - name: 📥 Checkout code | |
| uses: actions/checkout@v4 | |
| - name: 🏗️ Install Build Dependencies | |
| run: | | |
| echo "Installing CMake and Ninja for building..." | |
| choco install -y cmake ninja | |
| - name: 🐍 Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.13' | |
| - name: 📦 Install Python Build Requirements | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install numpy wheel | |
| - name: Setup MSBuild.exe | |
| uses: microsoft/[email protected] | |
| - name: Setup NASM | |
| uses: ilammy/setup-nasm@v1 | |
| - name: 🔧 Install NVIDIA CUDA Toolkit | |
| run: | | |
| echo "Downloading CUDA Toolkit..." | |
| $cuda_installer_url = "https://developer.download.nvidia.com/compute/cuda/12.1.1/network_installers/cuda_12.1.1_windows_network.exe" | |
| # Use the runner's temp directory, which is guaranteed to exist. | |
| $installer_path = Join-Path $env:RUNNER_TEMP "cuda_installer.exe" | |
| curl -L -o $installer_path $cuda_installer_url | |
| echo "Installing CUDA Toolkit silently..." | |
| # Use Start-Process for robust execution in PowerShell. | |
| # The arguments must be passed as a single string. | |
| $arguments = "-s nvcc_12.1 cudart_12.1" | |
| Start-Process -FilePath $installer_path -ArgumentList $arguments -Wait -NoNewWindow | |
| echo "Adding CUDA to PATH..." | |
| $cuda_path = "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1" | |
| echo "CUDA_PATH=$cuda_path" | Out-File -FilePath $env:GITHUB_ENV -Append | |
| echo "$cuda_path\bin" | Out-File -FilePath $env:GITHUB_PATH -Append | |
| echo "$cuda_path\lib\x64" | Out-File -FilePath $env:GITHUB_PATH -Append | |
| shell: pwsh | |
| - name: Clone opencv-python repository | |
| run: | | |
| git clone --depth 1 --recursive https://github.com/opencv/opencv-python.git | |
| pip install numpy packaging scikit-build cmake==3.24.2 | |
| # - CUDA_ARCH_BIN: Specifies the CUDA architectures to build for. | |
| # - CUDA_ARCH_PTX: Specifies the virtual architecture for forward compatibility. | |
| # TODO: Set correct PTX versions below. | |
| # TODO: Validate the built wheel can be installed and run. | |
| # TODO: Slim down the modules we build to only the ones that DVR-Scan needs to run: | |
| # https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html#tutorial_config_reference_general_modules | |
| - name: 🛠️ Build opencv-python with CUDA | |
| run: | | |
| cd opencv-python | |
| $CMAKE_ARGS = '-D CMAKE_BUILD_TYPE=RELEASE -D BUILD_SHARED_LIBS=OFF -D WITH_CUDA=ON -D CUDA_ARCH_BIN="6.0;6.1;7.0;7.5" -D CUDA_ARCH_PTX=7.5 -D OPENCV_ENABLE_NONFREE=ON -D ENABLE_LTO=ON -D CPU_DISPATCH="AVX,AVX2" -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules' | |
| $ENABLE_CONTRIB = 1 | |
| pip wheel . --verbose | |
| shell: pwsh | |
| - name: 📤 Upload Artifact | |
| uses: actions/[email protected] | |
| with: | |
| name: opencv-python-cuda | |
| path: opencv-python/opencv_python*.whl |