Skip to main content

sportsbd: a 4-class 3D CNN-based shot boundary detection toolkit for sports videos.

Project description

sportsbd

sportsbd: a 4-class 3D CNN-based shot boundary detection toolkit for sports videos.

This library provides a simple Python and command-line interface around a 3D CNN model (r2plus1d_18) for detecting shot boundaries in sports videos. The model predicts four classes:

  • hard – hard cuts
  • fadein – fade-in boundaries
  • logo – logo / overlay-based transitions
  • NaN – no boundary

Installation

Prerequisites:

  • Python 3.11.14 (recommended, tested and working)
  • PyTorch >= 2.3.0 for MPS Conv3D support (PyTorch 2.9.1 recommended)
  • TorchVision >= 0.18.0 (TorchVision 0.24.1 recommended)
  • NumPy 1.26.4 (tested version)

Recommended setup (tested and working with MPS Conv3D):

# Python 3.11.14 (use pyenv, conda, or your preferred Python version manager)
python --version  # Should show 3.11.14

# Install PyTorch 2.9.1 and TorchVision 0.24.1
pip install torch==2.9.1 torchvision==0.24.1

# Or install latest compatible versions
pip install torch torchvision

# Then install sportsbd
pip install sportsbd

Then install sportsbd:

From PyPI:

pip install sportsbd

Or from source:

Option 1: Install as a package (recommended)

git clone https://github.com/mehdih7/sportsbd.git
cd sportsbd
git lfs install  # Install Git LFS (required for model weights)
git lfs pull     # Download model weights
pip install .

Option 2: Install dependencies only

git clone https://github.com/mehdih7/sportsbd.git
cd sportsbd
git lfs install
git lfs pull
pip install -r requirements.txt
# Then use: python -m sportsbd.cli or add sportsbd/ to PYTHONPATH

For development:

pip install -r requirements-dev.txt
pip install -e .  # Install in editable mode

Quickstart

Python API – run inference on a video

from sportsbd import load_model, run_video_inference, get_available_device

# Load model (auto-detects best device: cuda > mps > cpu)
model = load_model("data/models/best.pt")

# Or explicitly specify device
model = load_model("data/models/best.pt", device="mps")  # Apple Silicon
# model = load_model("data/models/best.pt", device="cuda")  # NVIDIA GPU
# model = load_model("data/models/best.pt", device="cpu")   # CPU

# Run inference on a video (auto-detects device if not specified)
detections = run_video_inference(
    video_path="video.mp4",
    checkpoint_path="data/models/best.pt",
    threshold=0.7,
    stride=4,
    t_frames=16,
    fps=25,
)

# Detections is a list of dicts with:
# - 'frame_idx': frame index
# - 'timestamp_ms': timestamp in milliseconds
# - 'confidence': maximum boundary class probability
# - 'class_probs': per-class probabilities
print(f"Found {len(detections)} shot boundaries")

CLI – run inference on a video

sportsbd infer \
  --video video.mp4 \
  --checkpoint data/models/best.pt \
  --threshold 0.7 \
  --stride 4 \
  --t-frames 16 \
  --fps 25 \
  --out detections.json

Note: The --device option is optional. If not specified, sportsbd automatically detects the best available device (CUDA > MPS > CPU).

Important: Since the r2plus1d_18 model uses 3D convolutions, you should use a PyTorch build with MPS support if you want to run on Apple Silicon GPUs. The configuration below has been tested and confirmed to work with MPS Conv3D:

  • Python 3.11.14
  • PyTorch 2.9.1
  • TorchVision 0.24.1
  • NumPy 1.26.4

Supported devices:

  • cuda – NVIDIA GPU (Linux/Windows) - supports Conv3D
  • mps – Apple Silicon GPU (macOS) - supports Conv3D with PyTorch >= 2.3.0
  • cpu – CPU fallback - always works

Model checkpoints

Model weights should be placed in the data/models/ directory. The repository includes a pre-trained model at data/models/best.pt (358MB).

There are two ways to obtain the weights:

  • Option 1 – Clone the repository (Git LFS):

    git clone https://github.com/mehdih7/sportsbd.git
    cd sportsbd
    git lfs install
    git lfs pull  # Downloads data/models/best.pt
    
  • Option 2 – Download via Python helper: After installing sportsbd from PyPI:

    from sportsbd import download_model, load_model, run_video_inference
    
    # Download the pre-trained model to data/models/best.pt (default location)
    checkpoint_path = download_model()
    
    # Load model
    model = load_model(checkpoint_path)
    
    # Run inference as usual
    detections = run_video_inference(
        video_path="video.mp4",
        checkpoint_path=checkpoint_path,
        threshold=0.7,
        stride=4,
        t_frames=16,
        fps=25,
    )
    

Internally, sportsbd expects a PyTorch checkpoint (.pt/.pth) containing at least:

  • state_dict – model weights compatible with torchvision.models.video.r2plus1d_18
  • config – a dictionary with at least:
    • MODEL_NAME – model architecture name (defaults to "r2plus1d_18")
    • NUM_CLASSES – number of output classes (defaults to 4)

You can use any path to your checkpoint file - the examples use data/models/best.pt as a convention.

Development

To run tests:

pip install -e ".[dev]"
pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sportsbd-0.1.1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sportsbd-0.1.1-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file sportsbd-0.1.1.tar.gz.

File metadata

  • Download URL: sportsbd-0.1.1.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for sportsbd-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1afc4a3769c52df80c37a44c23a93cf3367015724d5858d7ffa9bd636c184f39
MD5 e6581936dc79f4be88b7aa815c93288c
BLAKE2b-256 df7a8d3cec2573f8f4577d7da237580feebe611b7e12e1b97730b1dce7968b9d

See more details on using hashes here.

File details

Details for the file sportsbd-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sportsbd-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for sportsbd-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f39d14587d50f8549c0895f16c7dae83dfcf08cd9b43a91a7684fbb76fdb28bc
MD5 06c23c60f36b712fe26a134a77b247d0
BLAKE2b-256 6f9876eae1b522dea0d263052d043995d53325c0f6aaa124fa7ce7bf500277e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page