Skip to content

fix(ocr): sort boxes within a row left-to-right#1190

Merged
msluszniak merged 1 commit into
mainfrom
@ms/fix-ocr-row-sort
May 27, 2026
Merged

fix(ocr): sort boxes within a row left-to-right#1190
msluszniak merged 1 commit into
mainfrom
@ms/fix-ocr-row-sort

Conversation

@msluszniak
Copy link
Copy Markdown
Member

@msluszniak msluszniak commented May 26, 2026

Description

This is potentially a blocker for demo app recordings for promotion purposes.

useOCR().forward(image) returned boxes top-to-bottom but boxes on the same visual row fell back to the earlier descending-side-length ordering — reading as "by probability" rather than reading order.

After the final Y-sort in groupTextBoxes, group consecutive boxes whose top-Y differs by less than half the mean text height and sort each group left-to-right by top-X.

Single-comparator soft-Y-equality is intentionally avoided — A~B, B~C with A far from C violates strict weak ordering and is UB under std::sort.

Introduces a breaking change?

  • Yes
  • No

Type of change

  • Bug fix (change which fixes an issue)
  • New feature (change which adds functionality)
  • Documentation update (improves or adds clarity to existing documentation)
  • Other (chores, tests, code style improvements etc.)

Tested on

  • iOS
  • Android

Testing instructions

  1. Run the computer-vision demo → OCR screen.
  2. Pick a photo with multiple short tokens on the same line (e.g. Price 12.99 USD).
  3. Inspect the results list — tokens within a row now come out left-to-right.

Related issues

Closes #1159

groupTextBoxes returned text boxes top-to-bottom but boxes on the same
visual row fell back to the earlier descending-side-length ordering,
which reads as "by probability" rather than reading order.

Adds a second pass: after sorting by top-Y, group consecutive boxes
whose top-Y differs by less than half the mean text height and sort
each group left-to-right by top-X.

A single comparator with soft Y-equality is intentionally avoided —
A~B, B~C with A>>C breaks strict weak ordering and is UB under
std::sort.

Closes #1159
@msluszniak msluszniak self-assigned this May 26, 2026
@msluszniak msluszniak added the bug fix PRs that are fixing bugs label May 26, 2026
@msluszniak msluszniak requested a review from chmjkb May 27, 2026 07:40
@msluszniak msluszniak merged commit bad4c37 into main May 27, 2026
4 checks passed
@msluszniak msluszniak deleted the @ms/fix-ocr-row-sort branch May 27, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix PRs that are fixing bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OCR: boxes within a row are not sorted left-to-right

2 participants