Skip to content

[ryoku] ocr: enhance preprocessing, indentation preservation, and notification actions - #901

Closed
Ronin-CK wants to merge 6 commits into
Ryoku-dev:unstable-devfrom
Ronin-CK:main
Closed

Ronin-CK wants to merge 6 commits into
Ryoku-dev:unstable-devfrom
Ronin-CK:main

Conversation

@Ronin-CK

@Ronin-CK Ronin-CK commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Enhance the OCR tool with in-memory contrast/polarity preprocessing, multiline code indentation preservation, edge-artifact pruning, and interactive notification actions.

What changed

Refactors ryoku-cmd-ocr to improve text recognition accuracy and add focused, interactive post-capture utilities:

  • In-Memory Preprocessing: Adaptive scaling (up to 400% for micro-crops), contrast stretching, sharpening, and automatic polarity inversion so Tesseract always receives dark text on light ground.
  • Fast Barcode / QR Detection: Pre-checks crops using zbarimg with quiet-zone padding and polarity passes before running Tesseract.
  • Code & Table Formatting: Custom TSV word-geometry parsing in awk that preserves leading indentation spaces and column gaps, while filtering out dragged edge-cut fragments.
  • Interactive Actions: Actionable notification buttons for URLs (Open Link), math expressions (evaluated via ryoku-cmd-calc), and plain text (Search, Translate).
  • Clipboard Integration: Uses ryoku-shell clip-copy when the daemon is running, with graceful fallback to wl-copy.

Attribution & Upstream:
Both this OCR pipeline and the previously contributed image search feature originate from my project QuickSnip. Attribution or a credit mention in the changelog / docs would be greatly appreciated!

Area

ryoku

How it was tested

  • Verified on Arch Linux / Hyprland with Tesseract 5.x and ImageMagick 7.
  • Tested small region grabs (<30px height) to verify adaptive upscaling and contrast stretching.
  • Tested dark-mode terminal and editor crops to confirm automatic polarity inversion and clean text extraction.
  • Tested multiline indented code blocks (Python, Bash) to verify whitespace indentation and multi-space gap preservation.
  • Tested QR code scans and math evaluation expressions (24 * 7).
  • Verified that slurp cancellation (Escape) exits silently with code 130.
  • Passed bash -n with zero syntax warnings.

Checklist

  • One logical change, with a clear [area] scope: summary commit subject.
  • Matching CHANGELOG.md updated in the area I touched.
  • The git hooks pass locally; I did not use --no-verify.
  • Lua parses (luac -p), shell scripts pass bash -n, and QML passes qmllint where applicable.
  • No duplicated config, no dead code, no commented-out code, no em-dash.
  • Docs updated if behavior or layout changed.

RetriggerView in GreptileConfidence Score: 4/5

Fix the link-triggered exit and satisfy the repository's release-note and dead-code requirements before merging.

Fix All in Claude CodeFindings

  1. P1 Some links stop OCR ▶
  2. P2 Angle brackets are escaped incorrectly ▶
  3. P2 OCR release notes are missing ▶
  4. P2 Unused OCR data remains ▶
Fix with agent prompt
### Issue 1
ryoku/shell/scripts/ryoku-cmd-ocr:258
**Some links stop OCR.** For recognized text such as `https://example.com/page)`, `grep` finds no opening parenthesis and returns 1. With `pipefail` and `set -e`, the assignment exits the script before it copies the text or shows notification actions. Allow zero matches when counting parentheses.

### Issue 2
ryoku/shell/scripts/ryoku-cmd-ocr:312-314
**Angle brackets are escaped incorrectly.** Bash 5.2 enables `patsub_replacement` by default, so an unescaped `&` in the replacement expands to the matched character. These lines turn `<` into `<lt;` and `>` into `>gt;`, rather than valid entities. Escape the replacement ampersands so captured code appears correctly in notifications.

### Issue 3
ryoku/shell/scripts/ryoku-cmd-ocr:2-3
**OCR release notes are missing.** This adds user-facing OCR features, but the PR only changes the script and its commits have no `Note:` trailers. Repository rules require a matching-area `CHANGELOG.md` update and a plain-language `Note: New: ...` or `Note: Fixed: ...` trailer. Add both so users can find these changes.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

### Issue 4
ryoku/shell/scripts/ryoku-cmd-ocr:183-186
**Unused OCR data remains.** The parser assigns `img_w` and `img_h` but never reads them. It also fills unused `top` and `conf` arrays, with the same pattern elsewhere. Repository rules forbid dead code. Remove these unused values so the parser only keeps data it uses.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Improves OCR image preparation, text spacing, QR detection, and notification actions. The follow-up keeps balanced parentheses in links and adds notification text escaping.

  • OCR prepares images and keeps more of their text layout.
  • OCR notifications offer actions that match the recognized content.

Reviews (2) · Last reviewed commit: "Update ryoku-cmd-ocr" · Reviewed by Greptile

Enhance OCR script with additional features like interactive notifications, QR code detection, and improved image processing.

@neur0map neur0map left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔴 Changes requested — 0 critical and 4 major issue(s): see the inline comments and the walkthrough for fixes and an agent-ready prompt.

# the clipboard. Self-contained: grim + slurp + tesseract + wl-copy. Optional
# --file IMAGE skips the region grab and recognizes an existing image.
# Select a screen region, OCR with tesseract, and copy recognized text to clipboard.
# Supports in-memory preprocessing, code indentation preservation, and notification actions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟡 minor · Maintainability · fix: low effort

This user-facing OCR change has no matching area CHANGELOG.md update in the PR. Add an entry covering the changed recognition and notification behavior.

Reply here to discuss, say it's fixed, or explain why it's intended — no mention needed.

Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
@neur0map

neur0map commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

🦊 mika's review

Incremental review: new commits since the last checkpoint.

Summary

  • Preprocess crops and detect QR codes and barcodes before OCR
  • Preserve text layout and support configurable OCR languages
  • Add notification actions for links, calculations, search, and translation

Change diagram

flowchart LR
  Input["Image or screen region"] --> Scan["QR and barcode detection"]
  Scan -->|No code| Preprocess["Image preprocessing"]
  Preprocess --> OCR["Tesseract and layout reconstruction"]
  Scan -->|Code found| Result["Recognized content"]
  OCR --> Result
  Result --> Clipboard["Clipboard"]
  Result --> Actions["Notification actions"]
Loading

Estimated review effort: 2/5 · risk: medium

📁 Walkthrough details

ryoku/shell/scripts/ryoku-cmd-ocr

  • Improves recognition with image preprocessing, polarity fallback, and optional QR and barcode detection.
  • Reconstructs indentation and column gaps, with raw and single-line output options.
  • Adds configurable language selection and contextual actions for copied results.
    • Adds language resolution from CLI, environment, or configuration.
    • Scales, sharpens, and normalizes images before Tesseract processing.
    • Uses TSV word geometry to reconstruct spacing and indentation.
    • Detects links, math expressions, and colors for contextual notifications.
    • Offers link opening, answer copying, web search, and translation actions.

Findings

Severity Category Fix effort File Issue
🟠 major correctness low ryoku/shell/scripts/ryoku-cmd-ocr Possible Bug — With set -euo pipefail, grep -o '(' returns status 1 when a URL has no opening parenthesis. A recognized URL ending in ), such as https://example.com/foo), therefore exits before copying text or showing the notif…
🟡 minor correctness medium ryoku/shell/scripts/ryoku-cmd-ocr Possible Bug — These characters are valid at the end of a URL, including a QR payload, so stripping them can open a different address. For example, https://example.com/search?q=why? loses its final question mark. Preserve exact decod…
🟡 minor correctness medium ryoku/shell/scripts/ryoku-cmd-ocr Possible Bug — The Search and Translate actions silently produce an empty query when python3 is unavailable because the conversion error is suppressed. Check for the dependency or use an available URL encoder before offering these ac…
🟡 minor maintainability low ryoku/shell/scripts/ryoku-cmd-ocr Maintainability — The new OCR options and notification actions are user-facing, but this PR does not update the area changelog. Add a matching CHANGELOG.md note so users can discover the changes.

⚡ Quick fixes by effort

Low effort (apply from the diff):

  • ryoku/shell/scripts/ryoku-cmd-ocr:257 — Possible Bug — With set -euo pipefail, grep -o '(' returns status 1 when a URL has no opening parenthesis. A recognized URL ending in ), such as https://example.com/foo), therefore exits before copying text or showing the notif…
  • ryoku/shell/scripts/ryoku-cmd-ocr:23 — Maintainability — The new OCR options and notification actions are user-facing, but this PR does not update the area changelog. Add a matching CHANGELOG.md note so users can discover the changes.

Medium effort (a function-level change):

  • ryoku/shell/scripts/ryoku-cmd-ocr:256 — Possible Bug — These characters are valid at the end of a URL, including a QR payload, so stripping them can open a different address. For example, https://example.com/search?q=why? loses its final question mark. Preserve exact decod…
  • ryoku/shell/scripts/ryoku-cmd-ocr:360 — Possible Bug — The Search and Translate actions silently produce an empty query when python3 is unavailable because the conversion error is suppressed. Check for the dependency or use an available URL encoder before offering these ac…
🤖 Prompt for AI agents (click, copy, paste into Claude/Codex)
Fix the following issues found by mika-agent on Ryoku-dev/ryoku#901 (branch main):

- ryoku/shell/scripts/ryoku-cmd-ocr:257-259 — Possible Bug (major severity, low effort): With `set -euo pipefail`, `grep -o '('` returns status 1 when a URL has no opening parenthesis. A recognized URL ending in `)`, such as `https://example.com/foo)`, therefore exits before copying text or showing the notif…
- ryoku/shell/scripts/ryoku-cmd-ocr:256-256 — Possible Bug (minor severity, medium effort): These characters are valid at the end of a URL, including a QR payload, so stripping them can open a different address. For example, `https://example.com/search?q=why?` loses its final question mark. Preserve exact decod…
- ryoku/shell/scripts/ryoku-cmd-ocr:360-366 — Possible Bug (minor severity, medium effort): The Search and Translate actions silently produce an empty query when `python3` is unavailable because the conversion error is suppressed. Check for the dependency or use an available URL encoder before offering these ac…
- ryoku/shell/scripts/ryoku-cmd-ocr:23-33 — Maintainability (minor severity, low effort): The new OCR options and notification actions are user-facing, but this PR does not update the area changelog. Add a matching `CHANGELOG.md` note so users can discover the changes.

For each: verify it is real in the current code, apply the smallest correct fix, and run the repo's tests. Skip anything already resolved and say why.

🧪 No relevant tests detected in the diff.

🚦 Merge recommendation: changes_required


🦊 Generated by Mika — the Ryoku org's self-hosted AI code reviewer. Machine findings: verify before merging.

@neur0map neur0map added area:ryoku Ryoku project area type:chore Maintenance work labels Nov 18, 2026
@neur0map

neur0map commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Thanks for the work here, the commit subjects need an [area] label, so reword 02fba0d, d588b8f, 1e2d8b8 and 3 more

@Ronin-CK
Ronin-CK changed the base branch from main to unstable-dev November 18, 2026 10:48
@Ronin-CK Ronin-CK closed this Nov 18, 2026
@Ronin-CK Ronin-CK reopened this Nov 20, 2026

@neur0map neur0map left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔴 Changes requested — 0 critical and 1 major issue(s): see the inline comments and the walkthrough for fixes and an agent-ready prompt.

Comment on lines +36 to +45
-h|--help)
cat <<EOF
Usage: ryoku-cmd-ocr [OPTIONS]

tmp="$input_file"
if [[ -z $tmp ]]; then
for c in grim slurp; do need "$c"; done
tmp="$(mktemp "${XDG_RUNTIME_DIR:-/tmp}/ryoku-ocr.XXXXXX.png")"
trap 'rm -f "$tmp"' EXIT
fi
Options:
--file IMAGE Recognize existing image instead of selecting a screen region
-s, --single-line Flatten newlines into a single line
-r, --raw Preserve raw OCR text without smart neighbor/edge filtering
-l, --lang LANG Specify OCR language (e.g. eng, eng+hin)
-h, --help Show this help message

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟡 minor · Maintainability · fix: low effort

This changes user-facing OCR behavior and adds options and notification actions, but the PR does not update the area CHANGELOG.md. Add a matching note so users can discover the changes.

Reply here to discuss, say it's fixed, or explain why it's intended — no mention needed.

Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved

@neur0map neur0map left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔴 Changes requested — 0 critical and 2 major issue(s): see the inline comments and the walkthrough for fixes and an agent-ready prompt.

Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Refactor OCR processing logic to simplify text handling and remove unnecessary checks.

@neur0map neur0map left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔴 Changes requested — 0 critical and 1 major issue(s): see the inline comments and the walkthrough for fixes and an agent-ready prompt.

Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
Comment thread ryoku/shell/scripts/ryoku-cmd-ocr Resolved
@neur0map
neur0map self-requested a review November 20, 2026 16:58

@neur0map neur0map left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔴 Changes requested — 0 critical and 1 major issue(s): see the inline comments and the walkthrough for fixes and an agent-ready prompt.

Comment on lines +23 to +33
-r|--raw)
raw_mode=true
shift
;;
-s|--single-line)
single_line=true
shift
;;
-l|--lang)
[[ -n ${2:-} ]] || { notify-send -u critical "$app" "Missing language"; exit 2; }
cli_lang="$2"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟡 minor · Maintainability · fix: low effort

The new OCR options and notification actions are user-facing, but this PR does not update the area changelog. Add a matching CHANGELOG.md note so users can discover the changes.

Reply here to discuss, say it's fixed, or explain why it's intended — no mention needed.

is_url=false
url_target=""
if [[ $text =~ ^(https?://[^[:space:]]+|www\.[^[:space:]]+)$ ]]; then
clean_url="$(sed -E 's/[.,;:!?>"'\''\x60]+$//' <<< "$text")"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟡 minor · Possible Bug · fix: medium effort

These characters are valid at the end of a URL, including a QR payload, so stripping them can open a different address. For example, https://example.com/search?q=why? loses its final question mark. Preserve exact decoded URLs rather than removing valid URL characters.

Reply here to discuss, say it's fixed, or explain why it's intended — no mention needed.

Comment on lines +257 to +259
while [[ $clean_url == *")" ]]; do
open_count="$(grep -o '(' <<< "$clean_url" | wc -l)"
close_count="$(grep -o ')' <<< "$clean_url" | wc -l)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟠 major · Possible Bug · fix: low effort

With set -euo pipefail, grep -o '(' returns status 1 when a URL has no opening parenthesis. A recognized URL ending in ), such as https://example.com/foo), therefore exits before copying text or showing the notification. Make the count commands tolerate zero matches.

Reply here to discuss, say it's fixed, or explain why it's intended — no mention needed.

Comment on lines +360 to +366
search)
q=$(python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$text" 2>/dev/null || true)
xdg-open "https://www.google.com/search?q=$q" >/dev/null 2>&1 &
;;
translate)
q=$(python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$text" 2>/dev/null || true)
xdg-open "https://translate.google.com/?text=$q" >/dev/null 2>&1 &

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟡 minor · Possible Bug · fix: medium effort

The Search and Translate actions silently produce an empty query when python3 is unavailable because the conversion error is suppressed. Check for the dependency or use an available URL encoder before offering these actions.

Reply here to discuss, say it's fixed, or explain why it's intended — no mention needed.

if [[ $text =~ ^(https?://[^[:space:]]+|www\.[^[:space:]]+)$ ]]; then
clean_url="$(sed -E 's/[.,;:!?>"'\''\x60]+$//' <<< "$text")"
while [[ $clean_url == *")" ]]; do
open_count="$(grep -o '(' <<< "$clean_url" | wc -l)"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Some links stop OCR. For recognized text such as https://example.com/page), grep finds no opening parenthesis and returns 1. With pipefail and set -e, the assignment exits the script before it copies the text or shows notification actions. Allow zero matches when counting parentheses.

Prompt To Fix With AI
This is a comment left during a code review.
Path: ryoku/shell/scripts/ryoku-cmd-ocr
Line: 258

Comment:
**Some links stop OCR.** For recognized text such as `https://example.com/page)`, `grep` finds no opening parenthesis and returns 1. With `pipefail` and `set -e`, the assignment exits the script before it copies the text or shows notification actions. Allow zero matches when counting parentheses.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Comment on lines +312 to +314
s="${s//&/&amp;}"
s="${s//</&lt;}"
s="${s//>/&gt;}"

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 Angle brackets are escaped incorrectly. Bash 5.2 enables patsub_replacement by default, so an unescaped & in the replacement expands to the matched character. These lines turn < into <lt; and > into >gt;, rather than valid entities. Escape the replacement ampersands so captured code appears correctly in notifications.

Prompt To Fix With AI
This is a comment left during a code review.
Path: ryoku/shell/scripts/ryoku-cmd-ocr
Line: 312-314

Comment:
**Angle brackets are escaped incorrectly.** Bash 5.2 enables `patsub_replacement` by default, so an unescaped `&` in the replacement expands to the matched character. These lines turn `<` into `<lt;` and `>` into `>gt;`, rather than valid entities. Escape the replacement ampersands so captured code appears correctly in notifications.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Comment on lines +2 to +3
# Select a screen region, OCR with tesseract, and copy recognized text to clipboard.
# Supports in-memory preprocessing, code indentation preservation, and notification actions.

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 OCR release notes are missing. This adds user-facing OCR features, but the PR only changes the script and its commits have no Note: trailers. Repository rules require a matching-area CHANGELOG.md update and a plain-language Note: New: ... or Note: Fixed: ... trailer. Add both so users can find these changes.

Rule Used: RYOKU COMMIT/PR CONVENTIONS (enforced by .githooks in every Ryoku repo; --no-verify is forbidden). Subjects: \[area\] scope: imperative summary, area one of global|installation|system|ryoku|docs|test|tooling|release (shell changes use [global]); 72 c... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: ryoku/shell/scripts/ryoku-cmd-ocr
Line: 2-3

Comment:
**OCR release notes are missing.** This adds user-facing OCR features, but the PR only changes the script and its commits have no `Note:` trailers. Repository rules require a matching-area `CHANGELOG.md` update and a plain-language `Note: New: ...` or `Note: Fixed: ...` trailer. Add both so users can find these changes.

**Rule Used:** RYOKU COMMIT/PR CONVENTIONS (enforced by .githooks in every Ryoku repo; --no-verify is forbidden). Subjects: `\[area\] scope: imperative summary`, area one of global|installation|system|ryoku|docs|test|tooling|release (shell changes use \[global\]); 72 c... ([source](https://app.greptile.com/-/switch-tenant?tenant=ryoku&next=%2Fcustom-context%3Fmemory%3Dc885c516-9406-453f-9659-5f5e48e9ae72))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

Comment on lines +183 to +186
NR == 2 && $1 == "1" {
img_w = $9 + 0
img_h = $10 + 0
}

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 Unused OCR data remains. The parser assigns img_w and img_h but never reads them. It also fills unused top and conf arrays, with the same pattern elsewhere. Repository rules forbid dead code. Remove these unused values so the parser only keeps data it uses.

Rule Used: RYOKU VISION. Ryoku is a hand-built Linux distribution on Arch (plus CachyOS, Void, Fedora, NixOS editions): a finished Wayland desktop (Hyprland or niri with the Ryoku Quickshell UI), a guided installer, and the system definition that reproduces bot... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: ryoku/shell/scripts/ryoku-cmd-ocr
Line: 183-186

Comment:
**Unused OCR data remains.** The parser assigns `img_w` and `img_h` but never reads them. It also fills unused `top` and `conf` arrays, with the same pattern elsewhere. Repository rules forbid dead code. Remove these unused values so the parser only keeps data it uses.

**Rule Used:** RYOKU VISION. Ryoku is a hand-built Linux distribution on Arch (plus CachyOS, Void, Fedora, NixOS editions): a finished Wayland desktop (Hyprland or niri with the Ryoku Quickshell UI), a guided installer, and the system definition that reproduces bot... ([source](https://app.greptile.com/-/switch-tenant?tenant=ryoku&next=%2Fcustom-context%3Fmemory%3D97738e49-32ea-4f61-9b4b-7961db42ac7e))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

@Ronin-CK Ronin-CK closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ryoku Ryoku project area type:chore Maintenance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants