Skip to content

Build OpenCV (Windows, CUDA) #22

Build OpenCV (Windows, CUDA)

Build OpenCV (Windows, CUDA) #22

# 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.12'
- name: 📦 Install Python Build Requirements
run: |
python -m pip install --upgrade pip
pip install numpy wheel
- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild@v1.1
- name: Setup NASM
uses: ilammy/setup-nasm@v1
- name: 🏗️ Setup Visual Studio Developer Command Prompt
run: |
$vcvars_path = "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
$lines = Invoke-Expression "& `"$vcvars_path`" && set"
$lines | ForEach-Object {
if ($_ -match "=") {
$name, $value = $_.Split("=", 2)
echo "$name=$value" | Out-File -FilePath $env:GITHUB_ENV -Append
}
}
shell: pwsh
- name: Cache CUDA Toolkit Installer
id: cache-cuda-installer
uses: actions/cache@v3
with:
path: ./.cache/cuda_installer.exe
key: cuda-installer-12.1.1
- name: 🔧 Install NVIDIA CUDA Toolkit
run: |
$installer_path = "./.cache/cuda_installer.exe"
if (-not (Test-Path $installer_path)) {
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"
New-Item -Path (Split-Path $installer_path) -ItemType Directory -Force
curl -L -o $installer_path $cuda_installer_url
} else {
echo "CUDA Toolkit installer found in cache."
}
echo "Installing CUDA Toolkit silently..."
$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: Cache opencv-python repository
id: cache-opencv-python
uses: actions/cache@v3
# TODO: Need to pin this to a version.
with:
path: opencv-python
key: opencv-python-master
- name: Clone opencv-python repository
if: steps.cache-opencv-python.outputs.cache-hit != 'true'
run: |
git clone --depth 1 --recursive https://github.com/opencv/opencv-python.git
- name: Install opencv-python dependencies
run: |
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/upload-artifact@v4.6.0
with:
name: opencv-python-cuda
path: opencv-python/opencv_python*.whl