Skip to content

Implement GPU acceleration support across optical flow, face detection, and pose estimation#330

Merged
alexarje merged 6 commits intomasterfrom
copilot/implement-gpu-support
Apr 13, 2026
Merged

Implement GPU acceleration support across optical flow, face detection, and pose estimation#330
alexarje merged 6 commits intomasterfrom
copilot/implement-gpu-support

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

GPU (CUDA) acceleration was only available for pose estimation. This adds opt-in GPU support to dense/sparse optical flow and face blurring, with automatic CPU fallback when CUDA is unavailable.

Changes

Shared utility

  • _utils.py: Added get_cuda_device_count() — wraps cv2.cuda.getCudaEnabledDeviceCount() safely, returns 0 on failure. Exported from package root.

GPU-accelerated functions

  • flow.dense(use_gpu=True): Uses cv2.cuda.FarnebackOpticalFlow when available. Pre-allocates two cv2.cuda_GpuMat objects and swaps references each frame to avoid per-frame GPU memory allocations.
  • flow.sparse(use_gpu=True): Uses cv2.cuda.SparsePyrLKOpticalFlow with the same GpuMat reuse strategy for both frame buffers and tracked point arrays.
  • blur_faces(use_gpu=True) / CenterFace(use_gpu=False): Sets DNN_BACKEND_CUDA / DNN_TARGET_CUDA on the ONNX network when CUDA is detected.
  • pose(device='gpu'): Refactored to use the shared get_cuda_device_count() instead of inline logic.

All GPU paths fall back to CPU with an informative message if cv2.cuda.FarnebackOpticalFlow / SparsePyrLKOpticalFlow are absent (require opencv-contrib built with CUDA) or no CUDA devices are found.

Usage

import musicalgestures as mg

# Check GPU availability
print(mg.get_cuda_device_count())  # 0 on CPU-only machines

mg_video = mg.MgVideo("video.mp4")
mg_video.flow.dense(use_gpu=True)   # GPU Farneback, falls back to CPU
mg_video.flow.sparse(use_gpu=True)  # GPU LK, falls back to CPU
mg_video.blur_faces(use_gpu=True)   # GPU ONNX inference, falls back to CPU

Copilot AI linked an issue Apr 13, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add GPU support for all functions Implement GPU acceleration support across optical flow, face detection, and pose estimation Apr 13, 2026
Copilot AI requested a review from alexarje April 13, 2026 16:06
…ve imports to module level, add blur_faces and pose GPU tests
@alexarje alexarje marked this pull request as ready for review April 13, 2026 17:24
@alexarje alexarje merged commit b34b9f7 into master Apr 13, 2026
1 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement GPU support

2 participants