Repository navigation
perf(annotators,smoother): ROI-only mask/color/heatmap blending, strip halo, zero-mask-copy trace, and bounded smoother masks - #3039
Open
Acedia11 wants to merge 6 commits into
Conversation
…p halo, zero-mask-copy trace, and bounded smoother masks
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #3039 +/- ##
=======================================
Coverage 95% 95%
=======================================
Files 84 84
Lines 15503 15602 +99
=======================================
+ Hits 14772 14867 +95
- Misses 731 735 +4 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved findings identify annotation-output regressions, unbounded trace-cache growth, and shared mutable smoother data that can corrupt retained history.
6 open issues
Preserve oriented-box coordinates when reconstructing confirmed tracks · New Evict pruned tracker IDs from the smoothing cache · New Preserve heatmap normalization order for exact hue output · New Preserve color parity for non-block-aligned image widths · New Deep-copy object-dtype data to isolate smoothed detections · New Copy compact mask storage when returning independent tracks · New
What changed in this PR
This PR reduces allocation and processing costs in Supervision’s annotators and detection smoother, aiming to preserve rendering and smoothing behavior.
Changes:
- Restricts mask painting and blending to relevant regions or non-empty strips.
- Caches heatmap colors and trace splines, skips unused mask copies, and releases superseded smoother masks.
- Adds focused regression tests and a changelog entry.
| File | Description |
|---|---|
tests/detection/tools/test_smoother.py |
Tests historical mask release and dense output-mask isolation. |
tests/annotators/test_core.py |
Tests halo strips, trace updates, heatmap lookup colors, and offscreen anchors. |
src/supervision/detection/tools/smoother.py |
Limits retained masks and replaces per-track deep copies with selective copying. |
src/supervision/annotators/core.py |
Adds region-based blending, strip processing, and heatmap and spline caches. |
docs/changelog.md |
Documents the performance optimizations. |
Review effort: Balanced
| if detections.class_id is not None | ||
| else None | ||
| ), | ||
| tracker_id=detections.tracker_id[is_confirmed], |
| spline_points = unique_xy.astype(np.int32) | ||
| else: | ||
| spline_points = unique_xy.astype(np.int32) | ||
| spline_cache[tracker_id] = (xy_key, spline_points) |
Comment on lines
+2819
to
+2820
| hue_scale = (self.low_hue - self.top_hue) / max_val | ||
| heat_hue = (self.low_hue - heat_mask * hue_scale).astype(np.uint8) |
Comment on lines
+2827
to
+2830
| # Colorize and blend only the tight bounding ROI of nonzero heat via the | ||
| # cached 256-entry HSV(h, 255, 255) -> BGR table. | ||
| lut = _heatmap_hsv_to_bgr_lut() | ||
| heat_bgr_roi = lut[heat_hue[hy1:hy2, hx1:hx2]] |
| confidence = np.mean(np.array(confidences), axis=0) if confidences else None | ||
|
|
||
| data = { | ||
| key: value.copy() if isinstance(value, np.ndarray) else deepcopy(value) |
Comment on lines
+285
to
+286
| if copy_mask and isinstance(mask, np.ndarray): | ||
| mask = mask.copy() |
…OBB trace anchors, spline cache eviction, and isolated smoother copies
This branch has not been deployed
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.

Description
Eliminates redundant full-frame allocations, blends, mask copies, and spline recomputations across high-traffic annotators and
DetectionsSmoother, preserving exact pixel outputs and smoothing behavior on both OpenCV and pure-NumPy fallback backends:_paint_masks_by_areascanned entire(H, W)frames per dense mask instead of each mask's tight bounding box;MaskAnnotatormade a redundantscene_roi.copy();ColorAnnotatorcloned and blended the full scene instead of the detections' union ROI.HaloAnnotatorallocated full-framefloat32blend buffers even on rows with zero halo pixels.HeatMapAnnotatorallocated full-framefloat64masks, ran full-frameHSV→BGRconversions, and blended every frame pixel instead of active heatmap pixels.TraceAnnotatorcopied full detection masks into track history even whenposition != Position.CENTER_OF_MASS, and recomputed splines on every frame.DetectionsSmootherretained full dense masks on every historical entry in each track deque (even though only the latest entry's mask is emitted) and rancopy.deepcopyper track.Motivation and Context
Measured standalone against
develop(without OpenCV installed; Python 3.12, NumPy 2.3.1, median wall-time &tracemallocpeak MiB):develop)HeatMapAnnotator(50 dets, 1080p)TraceAnnotatorw/ masks (50 dets, 1080p)MaskAnnotator(50 masks, 1080p)ColorAnnotator(50 boxes, 1080p)HaloAnnotator(50 masks, 1080p)DetectionsSmootherw/ masks (30 tracks, 480p)Changes Made
src/supervision/annotators/core.py:_paint_masks_by_area; removed redundantscene_roi.copy()inMaskAnnotator.ColorAnnotator.HaloAnnotatorfor both BGR and BGRA scenes.TraceAnnotatorwhenposition != Position.CENTER_OF_MASSand cache per-track smooth splines.uint8ROI accumulation, cached 256-entry exactHSV(h, 255, 255)→BGRLUT via activecv2.cvtColor, and active-pixel ROI blending inHeatMapAnnotator.src/supervision/detection/tools/smoother.py:deepcopyofDetections.tests/annotators/test_core.pyandtests/detection/tools/test_smoother.py.Testing
Without OpenCV:
pytest tests/annotators tests/detection/tools/test_smoother.py --doctest-modules src/supervision/annotators/core.py src/supervision/detection/tools/smoother.py— 512 passedWith OpenCV (
opencv-python-headless):pytest tests/annotators/test_core.py tests/detection/tools/test_smoother.py— 381 passedruff check,ruff format,codespell,check_doctest_fences.py, andmypy— all passed cleanly on changed lines.Added/updated tests, and the full suite passes locally
Updated docs (docstrings / mkdocs entry) for new or changed public API
Added a changelog entry at the top of
docs/changelog.md(skip for lint/type/format-only or pure doc changes)Additional Notes
_heatmap_hsv_to_bgr_lut()runs the activecv2.cvtColoronce on a 256-pixel strip so outputs remain bit-for-bit aligned with both OpenCV and fallback backends.