feature: add KeyPoints.merge() method#2412
Open
abhijithneilabraham wants to merge 8 commits into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2412 +/- ##
=======================================
Coverage 86% 86%
=======================================
Files 70 70
Lines 10335 10359 +24
=======================================
+ Hits 8878 8902 +24
Misses 1457 1457 🚀 New features to boost your workflow:
|
Contributor
Author
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a KeyPoints.merge() classmethod to the keypoints API, enabling users to combine multiple KeyPoints objects into one (including aligned optional fields and merged data), similar to the existing Detections.merge() workflow.
Changes:
- Implement
KeyPoints.merge()insrc/supervision/key_points/core.py, including validation and keypoint-count consistency checks. - Add a comprehensive parametrized test suite covering happy paths and error cases for merge behavior.
- Document the new feature in
docs/changelog.mdunder Unreleased → Added.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/supervision/key_points/core.py | Adds KeyPoints.merge() implementation and doctest documentation. |
| tests/key_points/test_core.py | Adds parametrized unit tests validating merge behavior and error handling. |
| docs/changelog.md | Adds an Unreleased changelog entry for KeyPoints.merge. |
Contributor
Author
|
@Borda Resolved latest Copilot Comments |
…hijithneilabraham/supervision into abhijith_keypoints_merge_feat
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.
Before submitting
(https://google.github.io/styleguide/pyguide.html#383-functions-and-methods)
Description
Adds KeyPoints.merge(), a classmethod that combines a list of KeyPoints
objects into one, mirroring the Detections.merge() contract:
KeyPoints.empty()
concatenated in input order
same message convention as Detections.merge
mismatched keys raise ValueError
One KeyPoints-specific rule: all non-empty inputs must share the same number
of keypoints per skeleton (the m in xy of shape (n, m, 2)), since skeletons
from different models are not comparable. Mismatched counts raise a ValueError
naming the counts.
One implementation note versus Detections.merge: optional fields are stacked
with np.concatenate(axis=0) instead of np.hstack, because keypoint_confidence
and visible are (n, m) arrays where hstack would concatenate along the wrong
axis.
Type of Change
Motivation and Context
As proposed in #2409: with KeyPoints.with_nms() added in #2338, the natural
workflow is merge then suppress , which means combine skeletons from multiple frames,
crops, or model passes, then dedup. The suppress half exists; the merge half
does not. Today, combining keypoints means concatenating five aligned arrays
by hand.
frames_keypoints = [sv.KeyPoints.from_ultralytics(model(frame)[0]) for frame
in batch]
all_keypoints = sv.KeyPoints.merge(frames_keypoints)
deduped = all_keypoints.with_nms(threshold=0.5)
Closes #2409
Changes Made
placed alongside empty() / is_empty() / with_nms(), reusing the existing
_validate_keypoints_fields and merge_data helpers
field and data merging
the same (input_list, expected_result, exception) structure as the existing
Detections.merge tests: empty list, only-empty inputs, single input, empties
ignored, xy-only inputs, all fields populated (including visible and data),
partial-field mismatch for class_id / visible / detection_confidence,
mismatched keypoint counts, and mismatched data keys
Testing
feature works
Full suite: 2805 passed, 1 skipped. The docstring example runs as a doctest
under the project's --doctest-modules configuration. Every line of the new
method is covered by the new tests, including all three ValueError paths. ruff
check and ruff format --check pass on both changed files; mypy reports no
errors attributed to the changed file.
Google Colab (optional)
Colab link: N/A
Screenshots/Videos (optional)
N/A