Skip to content

Translate: draw the translation over an image - #19032

Merged
danielhanchen merged 3 commits into
mainfrom
translate-image-overlay
Nov 23, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
translate-image-overlay

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Related to #18666

Summary

When an image is opened on the Translate page and Tesseract read its text, the output pane gets a new "Show on image" action. It opens a dialog that paints the translation over the image, Google Lens style: each text block Tesseract found is covered with the colour sampled around it, and the translated text is drawn in its place, auto-sized to the block, in black or white for contrast. "Download image" saves the result as a PNG (sign.jpg translated into French saves as sign.fr.png). The plain text translation is unchanged and stays the main result.

Backend (opt-in, default byte-identical)

  • POST /api/rag/extract-text takes a new optional form field boxes (default false).
  • With boxes=true and a one-page image that Tesseract read, the response gains blocks: [{text, lines, box: [x0, y0, x1, y1]}], the box as fractions of the image. The blocks come from the same OCR text page as the text (page.get_text("dict") next to the existing page.get_text("text")), so OCR runs once and text is identical to today.
  • Without the field, or when a vision model loaded in Chat read the image (no boxes there), or for multi-page TIFFs, the response is exactly as before.

Frontend

  • Translate asks for boxes only for images a browser canvas can draw (png, jpg, webp, gif, bmp). The action shows only while the text box still holds that image's text and the translation is current.
  • The block texts are translated with the numbered batching the subtitle path already uses (translateTexts, factored out of translateSubtitles, which now calls it with identical behaviour), through the same model setup as the main translation (prepareTranslation, factored out of runTranslation).
  • Drawing lives in translate-image-overlay.ts (no app imports): per-channel median of a ring around each box (sampled before any painting), WCAG contrast pick, word wrap that breaks unspaced scripts between characters, and a binary search for the largest font that fits.
  • New strings are in all 25 locales.

Scoped out, and why

  • Vision-model reads: the loaded vision model returns text only, so there are no boxes and the action does not appear. Images keep today's text result.
  • Without Tesseract: no blocks come back, so nothing changes and no new error is shown.
  • TIFF: browsers other than Safari cannot draw it on a canvas, and multi-page TIFFs are pages rather than one image.
  • Per-line layout and styling: the translation is reflowed inside each block and centred, with one font. Matching the original font, rotation or curved text is left out.

Tests

  • Backend: tests/test_rag_extract_text_boxes.py: boxes=true returns Tesseract's blocks; the default call returns the same three keys and uses the old OCR call; a vision-model read gives no blocks; block parsing (lines joined, empty blocks dropped, boxes clamped to the page) and an OCR failure (no text, no blocks) against a fake PyMuPDF page; and a real Tesseract run on a drawn image, skipped unless TESSDATA_PREFIX has eng.traineddata. All 6 pass locally with real Tesseract data (PyMuPDF 1.27.2.3's built-in engine). tests/test_rag_extract_text.py passes except test_damaged_document_is_422_and_cleaned_up[broken.docm], which fails the same way on main in this environment.
  • Frontend: tests/translate-image-overlay.test.ts covers the median sampling, contrast, wrapping (including CJK), font fitting, block validation, file naming, one numbered batch for the block texts, and the page wiring. Translate tests 94/94, full npm test green, npm run typecheck clean, npm run i18n:check:strict passes, eslint reports nothing new on the touched files.
  • The drawing code ran in headless Chromium on a test image: the cream card and the blue background each kept their colour, the original text was covered, and the translation came out in black on the card and white on the blue.

Not verified here

  • A full Studio run with a real translation model (no GPU on this host). The dialog reuses the same model setup and request code as the main translation.

Before / after

A sign image opened on Translate, read by Tesseract and translated into French with Hy-MT2 1.8B in a real Studio (CPU). Both sides read and translate the text the same way; after, the result also has a Show on image action:

page before and after

The Show on image dialog (right) next to the plain result (left):

overlay

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T09:12:57.337806Z 48d6391 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Nov 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7d34e119ec

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +110 to +113
let best = {
size: low,
lines: wrapWords(text, width, (s) => measure(s, low)),
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle text that cannot fit at the minimum font size

For small image text boxes or translations substantially longer than the original, the initial 4px layout can already exceed the available height, but it is returned even when every iteration fails the fit check. For example, a 50×10px box with a three-line translation returns a layout 14.4px tall. Because drawTranslatedBlocks draws those lines without clipping, the translated text spills outside the covered area and can overlap neighboring text or be cut off at the image edge. Handle the no-fitting-size case explicitly rather than returning an unchecked layout.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 5200ce8: when nothing fits even at 4px, only the lines the box holds are kept, the last ending in an ellipsis, and every block is drawn clipped to its covered box, so text never spills onto neighbours (covered by a test).

…an ellipsis

A translation that does not fit even at the smallest size now keeps only the
lines the box holds, ending in an ellipsis, and every block is drawn clipped
to its covered box, so it never spills onto neighbouring text.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5200ce8b7e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +472 to +475
return translateTexts(options.texts, complete, {
signal: options.signal,
onPhase: options.onPhase,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Split oversized OCR blocks before translating them

When Tesseract groups a long paragraph into one block, this path sends the entire block to the model: translateTexts uses subtitleBatches, which limits groups but never splits an individual text. A 3,200-character block therefore produces one 3,200-character request, whereas the main translation splits it into requests of at most 1,000 characters. This bypasses the safeguard documented in translate-text.ts for Hy-MT2 dropping paragraphs from long passages, so the image overlay can silently omit content even when the plain-text translation succeeds. Split oversized blocks with splitForTranslation and recombine their translations before drawing.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 48d6391: blocks longer than one passage are split with splitForTranslation and rejoined, shorter ones keep the numbered batches (test asserts no request over 1,000 characters).

A block over one passage (1,000 characters) went to the model whole, which
Hy-MT2 can answer with paragraphs missing; it is now split and rejoined like
any text, while shorter blocks keep the numbered batches.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 23, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 48d6391440

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 23, 2026
@danielhanchen
danielhanchen merged commit b7e8430 into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the translate-image-overlay branch November 23, 2026 09:49
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…apped sidebar glass frame flag (#19042)

## Summary

Two checks are red on `main` itself, so every PR rebased onto it
inherits them (`Core` on all three transformers / TRL legs, `Repo tests
(CPU, rest)`, `Repo tests (CPU, studio)` and `Frontend build + bundle
sanity`). Both reproduce on a clean `main` checkout.

| Failing check | Introduced by | Which side was wrong | Fix |
|---|---|---|---|
|
`tests/test_python39_compatibility.py::test_studio_evaluated_unions_do_not_grow`
(`36 studio files now evaluate PEP 604 unions on the floor, against 35
recorded`) | #19032 added
`studio/backend/tests/test_rag_extract_text_boxes.py` | The new file.
`def _tessdata() -> str \| None` is evaluated at definition time, which
fails on the Python floor. The ratchet is right not to grow. | `from
__future__ import annotations`, as the ratchet asks. |
|
`tests/studio/test_appearance_coverage.py::test_every_appearance_setting_is_measured_or_allowlisted`
(`<html> flag 'data-sidebar-glass-frame' switches on costly CSS ... but
no gate mode measures it`) | #18530 added the `data-sidebar-glass-frame`
flag and its CSS | Neither the flag nor the guard: the coverage entry
was missing. The flag is set only when the sidebar is pinned under the
custom window titlebar (`shouldUseCustomWindowTitlebar()`: Tauri on
Windows / Linux), so the Chromium gate cannot reach it. | Map it in
`ROOT_FLAGS` to an `allow:` reason, the same way `tauri` and
`client-windows` are handled. |

## Testing

- `tests/test_python39_compatibility.py`,
`tests/studio/test_appearance_coverage.py`,
`studio/backend/tests/test_rag_extract_text_boxes.py`: 18 passed, 1
skipped (both guards failed on `main` without this change).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant