feat/shared foundation for analytics (PR 1) - #59
Open
AlexBodner wants to merge 2 commits into
Open
Conversation
Add class/team ids, text drawing helper, team lock helpers, and core pitch homography fitting. Example main.py imports shared class ids; legacy modes unchanged. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Pull request overview
This PR lays shared “foundation” utilities for the soccer analytics pipeline by centralizing detection/team IDs, adding reusable drawing and team-lock helpers, and introducing a core pitch homography fitting module.
Changes:
- Centralize soccer detection class IDs and team IDs in
sports/configs/soccer.py, and update the soccer example to import them. - Add clip-level team “lock” helpers in
sports/common/team.pyto stabilize team assignments by tracker ID majority vote. - Introduce
sports/common/homography.py(RANSAC-capable homography fit + helpers) andsports/common/draw.py(text-with-shadow helper).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| sports/configs/soccer.py | Adds shared detection class IDs and team ID constants. |
| sports/common/team.py | Adds team-lock utilities keyed by tracker IDs. |
| sports/common/homography.py | Adds pitch keypoint normalization + homography fitting utilities. |
| sports/common/draw.py | Adds a reusable OpenCV text shadow drawing helper. |
| examples/soccer/main.py | Switches example constants to import shared IDs from config. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+117
to
+134
| def lock_teams_by_tracklet_majority( | ||
| frames: list[tuple[int, sv.Detections]], | ||
| ) -> dict[int, int]: | ||
| """Lock one team per tracker id using a majority shirt-colour vote. | ||
|
|
||
| Args: | ||
| frames: Sequence of ``(frame_idx, detections)`` pairs from a clip. | ||
|
|
||
| Returns: | ||
| Mapping of ``tracker_id`` to locked team id (0 or 1). | ||
| """ | ||
| votes: dict[int, list[int]] = {} | ||
| for _, dets in frames: | ||
| if dets.tracker_id is None or dets.data is None: | ||
| continue | ||
| team = np.asarray( | ||
| dets.data.get("team", np.full(len(dets), TEAM_NONE)), dtype=int | ||
| ) |
Comment on lines
+143
to
+147
| locked: dict[int, int] = {} | ||
| for tid, vals in votes.items(): | ||
| counts = np.bincount(np.asarray(vals, dtype=int), minlength=2) | ||
| locked[tid] = int(np.argmax(counts)) | ||
| return locked |
Comment on lines
+150
to
+154
| def apply_team_lock( | ||
| team_arr: np.ndarray, | ||
| tracker_id: np.ndarray | None, | ||
| team_lock: dict[int, int], | ||
| ) -> np.ndarray: |
Comment on lines
+174
to
+176
| def relock_detection_teams( | ||
| dets: sv.Detections, team_lock: dict[int, int] | ||
| ) -> sv.Detections: |
Comment on lines
+39
to
+52
| if use_ransac and len(src) >= 4: | ||
| m_inv, _ = cv2.findHomography( | ||
| dst, src, cv2.RANSAC, ransacReprojThreshold=ransac_thresh | ||
| ) | ||
| if m_inv is not None: | ||
| try: | ||
| self.m = np.linalg.inv(m_inv) | ||
| return | ||
| except np.linalg.LinAlgError: | ||
| pass | ||
| m, _ = cv2.findHomography(src, dst) | ||
| if m is None: | ||
| raise ValueError("Homography matrix could not be calculated.") | ||
| self.m = m |
This was referenced Jul 15, 2026
AlexBodner
marked this pull request as ready for review
July 16, 2026 13:26
5 tasks
Addresses Copilot review on typing vs python_requires and RANSAC fallback. Co-authored-by: Cursor <cursoragent@cursor.com>
Collaborator
Author
|
Addressed Copilot notes: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Add class/team ids, text drawing helper, team lock helpers, and core pitch homography fitting. Example main.py imports shared class ids; legacy modes unchanged.
Stack
These soccer analytics PRs merge in order: #59 → #60 → #61 → #63 → #64 → #65 → #66 → #67. Each later PR targets the previous branch.
Demo
No new example mode in this PR (shared library foundation only). Mode demo renders start in PR 2 — DIRECTION. All preview clips: soccer-analytics-pr-demos release.
Type of Change
Testing
Checklist