Click to play the full video on Youtube:
CourtVision (formerly Basketball-CV) is an all-in-one computer vision toolkit for basketball analytics, transforming raw video footage into actionable data and visualizations. Whether you’re a coach, analyst, researcher, or enthusiast, CourtVision helps you:
- Automatically detect and track game objects (players, ball, hoops, scoreboard).
- Classify players into teams via jersey-color zero-shot classification.
- Compute and visualize possession, passes, interceptions, speed, and distance.
- Generate an overhead tactical map using homography and pose estimation.
- Annotate and export richly detailed video outputs for presentations or further analysis.
From the court to your screen—see the game like never before.
- Object Detection & Tracking
- YOLOv8x for high-accuracy detection of 7 classes: Ball, Clock, Hoop, Overlay, Player, Referee, Scoreboard.
- ByteTrack maintains consistent player IDs across frames for longitudinal analysis.
- Team Assignment
- CLIP-based zero-shot jersey-color classification to label team membership.
- Cache assignments and reset every 50 frames to handle jersey occlusions.
- Ball Possession & Event Detection
- Ball acquisition determined by containment ratio and distance heuristics.
- Pass and interception detection by comparing successive holders and team IDs (65–70% accuracy).
- Tactical Overhead View
- Pose model keypoints validate court landmarks.
- Homography maps player foot positions into a 2D tactical court inset.
- Suppression of invalid keypoints ensures robustness.
- Player Metrics
- Cumulative distance traveled and smoothed speed (km/h) per player.
- Interpolation fills missing detections for continuous tracking.
- Visualization Modules (
drawers/)- BallTracksDrawer, PlayerTracksDrawer, TacticalViewDrawer, SpeedAndDistanceDrawer, PassInterceptionsDrawer, TeamBallControlDrawer.
- Modular design lets you pick & choose overlays.
- Stub Caching
- Save intermediate results (detections, tracks, assignments) to disk, skipping reprocessing on reruns.
project_root/
├── models/ # Trained model weights (YOLO, CLIP)
├── utils/ # Core helpers: video IO, geometry, stubs
│ ├── video_utils.py # read_video, save_video
│ ├── geometry_utils.py # bbox center, width, distance
│ └── stub_utils.py # save_stub, read_stub
├── ball_tracker.py # YOLO-based ball detection + interpolation
├── player_tracker.py # YOLO + ByteTrack for players
├── team_assigner.py # CLIP-based jersey classification
├── tactical_view_converter.py # Pose validation & homography mapping
├── drawers/ # Visualization components
│ ├── ball_tracks_drawer.py
│ ├── player_tracks_drawer.py
│ ├── tactical_view_drawer.py
│ ├── speed_distance_drawer.py
│ ├── pass_interceptions_drawer.py
│ └── team_ball_control_drawer.py
├── stubs/ # Cached pickle files
├── input_videos/ # Raw game footage
├── output_videos/ # Annotated exports
└── README.md # You are here
This section outlines every major step in building CourtVision:
- YOLO Architecture: Selected YOLOv8x for state-of-the-art detection speed and accuracy.
- Ultralytics Integration: Leveraged their API for streamlined model loading and inference.
- Initial Testing: Ran base YOLOv8x on basketball footage—detected players & ball out-of-the-box.
- Transfer Learning: Fine-tuned YOLOv8x on custom-labeled basketball dataset via RoboFlow.
- RoboFlow: Annotated images, trained models in Google Colab (free GPU), exported weights offline.
- Detection Classes: Defined 7 classes: Ball, Clock, Hoop, Overlay, Player, Referee, Scoreboard.
- Tracking Layer: Added ByteTrack to assign persistent IDs to detected players.
- Video Processing: Built video I/O utilities (
cv2.VideoCapture,VideoWriter). - Supervision Library: Simplified conversion between Ultralytics detections and trackers.
- Stub Caching: Cached detections, tracks, and team assignments to accelerate development loops.
- Data Interpolation: Used
pandas.interpolate()andbfill()to fill detection gaps for smooth tracking. - Team Classification: Employed CLIP zero-shot classification for jersey-color-based team labeling.
- Cache Reset Strategy: Cleared jersey assignment cache every 50 frames to handle occlusions & new players.
- Ball Acquisition Logic: Calculated containment ratio and minimum distance to determine possessor.
- Possession Visualization: Drew translucent triangles to indicate current ball controller.
- Pass & Interception Detection: Identified events by comparing successive possession holders & team IDs.
- Pose Model for Court Keypoints: Detected court landmarks to establish homography reference points.
- Homography & Tactical View: Transformed camera view to overhead court diagram inset in video frames.
- Player Position Mapping: Projected player foot positions into tactical view coordinates.
- Distance Calculation: Converted pixel movements to meters using court scale and Euclidean distance.
- Speed Estimation: Computed km/h over sliding windows of frames for smooth speed values.
- Final Annotation: Combined trackers, metrics, and tactical view overlays into annotated video outputs.
- Clone this repo and install dependencies.
- Download or train your YOLO & CLIP models into
models/. - Prepare a folder under
input_videos/with your footage. - Environment Create a virtual environment.
python3 -m venv .venv
source .venv/bin/activate- Install Install dependencies.
pip install opencv-python numpy pandas ultralytics supervision transformers torch Pillow- Run Run the main script.
python main.py- Explore the annotated videos in
output_videos/and visual modules indrawers/.
MIT License © Manan Gulati

