Skip to content

fix(annotators): keep labels wider than the frame at its left edge - #2940

Merged
Borda merged 5 commits into
roboflow:developfrom
sohailshk:fix/snap-boxes-wider-than-frame
Nov 11, 2026
Merged

Borda merged 5 commits into
roboflow:developfrom
sohailshk:fix/snap-boxes-wider-than-frame

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

Description

LabelAnnotator(smart_position=True) and RichLabelAnnotator(smart_position=True) move each label box into the frame with snap_boxes, which promises to prioritize the top and left edges. A label box that starts outside the frame is moved to x=0 even when it is wider than the frame, but one that starts inside it and runs past the right edge was shifted left by the whole overflow, so a box wider than the frame ended up with a negative x1 and lost the first characters of its text. The same happened for tall boxes at the bottom edge.

import numpy as np
from supervision.annotators.utils import snap_boxes

xyxy = np.array([[-20, 10, 350, 50], [10, 10, 400, 50], [10, 10, 40, 300]])
snap_boxes(xyxy, (320, 240))
# before: [[  0.  10. 370.  50.]   starts outside the frame: kept at the left edge
#          [-70.  10. 320.  50.]   starts inside the frame: pushed past the left edge
#          [ 10. -50.  40. 240.]]  same on the vertical axis
# after:  [[  0.  10. 370.  50.]
#          [  0.  10. 390.  50.]
#          [ 10.   0.  40. 290.]]

With this change the shift for a box crossing the right or bottom edge stops at the left or top edge, so a box that cannot fit keeps its left (top) edge visible and only its end is cut.

Motivation and Context

On a small frame (thumbnail, crop) a long label with smart_position=True showed its tail instead of its beginning. Boxes that fit the frame, and boxes starting outside it, are unchanged.

Changes Made

  • snap_boxes: limit the right and bottom shifts so x1 and y1 stay at or above 0.
  • Tests: TestSnapBoxes covers the existing shifts and the two new cases (wider and taller than the frame, starting inside it). snap_boxes had no unit test in tests/, only its doctest.
  • Changelog entry under Unreleased.

Testing

  • New tests on develop: 2 failed, 3 passed (assert False on the two boxes starting inside the frame). With this change: 5 passed.

  • pytest tests/annotators tests/key_points src/supervision/annotators: 855 passed, 1 skipped.

  • pre-commit run --files <changed files>: ruff, ruff-format, docformatter, mdformat, mypy and codespell pass.

  • Full suite on the branch: pytest tests src -> 7301 passed, 35 skipped.

  • Added/updated tests, and the full suite passes locally

  • Updated docs (docstrings / mkdocs entry) for new or changed public API

  • Added a changelog entry in docs/changelog.md under Unreleased (skip for lint/type/format-only or pure doc changes)

Additional Notes

snap_boxes is only called from _adjust_labels_in_frame; the keypoint VertexLabelAnnotator does not use it.

Arabic and Hebrew lines in RichLabelAnnotator are right-aligned in their box, so for those a label wider than the frame now shows its end, as it already did when the box started left of the frame; keeping their start would need per-label alignment in snap_boxes, left out here.

snap_boxes shifts an overflowing label box back into the frame, and the shift for a box crossing the right or bottom edge ignored that the box could be larger than the frame, so a label starting inside it ended up past the left edge and lost its first characters. A box that cannot fit now stops at the top-left edge, as boxes starting outside it already did.
@sohailshk
sohailshk requested a review from SkalskiP as a code owner November 11, 2026 04:29
@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (64886e2) to head (327b3d7).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2940   +/-   ##
=======================================
  Coverage       95%     95%           
=======================================
  Files           83      83           
  Lines        14440   14443    +3     
=======================================
+ Hits         13685   13688    +3     
  Misses         755     755           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda requested a balanced review from Copilot November 11, 2026 11:01
@Borda Borda added the bug Something isn't working label Nov 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the oversized-label placement fix.

No open issues 🎉

What changed in this PR

Fixes smart label placement in Supervision so labels wider or taller than the frame remain anchored at the left or top edge, rather than shifting beyond it.

Changes:

  • Caps rightward-overflow and bottom-overflow corrections in snap_boxes, preserving box dimensions.
  • Adds regression tests for oversized boxes alongside existing edge-shifting behavior.
  • Documents the fix for LabelAnnotator and RichLabelAnnotator with smart_position=True.
File Description
tests/​annotators/​test_utils.py Adds five parametrized cases covering edge shifts and oversized boxes.
src/​supervision/​annotators/​utils.py Limits overflow shifts to keep left and top coordinates nonnegative.
docs/​changelog.md Adds an Unreleased entry describing the label-placement fix.

Review effort: Balanced

Borda and others added 3 commits November 11, 2026 14:37
Changes:
- Integrate the target branch's OpenCV fallback arithmetic and CSV column validation changes with their tests.
- Preserve the label-positioning and target changelog entries when resolving their collision.

Impact:
- The PR branch includes current target fixes while retaining its label-positioning change and release notes.

Verification:
- Focused annotator utility tests: 88 passed.
- Reviewed the complete staged merge and git diff --cached --check passed.

Residual limits:
- Review findings for RTL labels and changelog wording remain to be selected and addressed.

---

Co-authored-by: Codex <[email protected]>
Changes:
- Align shaped right-to-left text to the visible frame edge when smart-positioned label boxes are oversized.
- Add Arabic, Hebrew and mixed-direction rendering regressions and correct the changelog description.

Impact:
- Oversized right-to-left labels keep their leading text visible while left-to-right box placement remains unchanged.

Verification:
- Full pytest coverage gate: 6,665 passed, 281 skipped.
- Selected-file pre-commit checks, type check and diff check passed.
- Independent source challenge found no remaining concrete defect.

Residual limits:
- Extreme padding and fonts or platforms beyond the tested configuration were not exhaustively checked.

---

Co-authored-by: Codex <[email protected]>
…er-than-frame

# Conflicts:
#	docs/changelog.md
@sohailshk

Copy link
Copy Markdown
Contributor Author

Thanks for the RTL fix, @Borda. I merged develop again to clear the changelog conflict (both entries kept, this one first); tests/annotators passes (374).

@Borda
Borda merged commit ba33330 into roboflow:develop Nov 11, 2026
40 checks passed
Borda added a commit that referenced this pull request Nov 11, 2026
…2940)

snap_boxes shifts an overflowing label box back into the frame, and the shift for a box crossing the right or bottom edge ignored that the box could be larger than the frame, so a label starting inside it ended up past the left edge and lost its first characters. A box that cannot fit now stops at the top-left edge, as boxes starting outside it already did.

* docs(changelog): link the PR number
* fix(annotators): preserve leading text in oversized RTL labels

---------

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Codex <[email protected]>
avanish-garg added a commit to avanish-garg/supervision that referenced this pull request Nov 16, 2026
…nchor jump

The smart_position entry for roboflow#2940 already ships under 0.31.7; remove its copy from Unreleased. Document that an empty mask's box-center anchor can differ from the centroid in neighbouring frames.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants