mac-ocr

repository·develop·Indexed 19 days ago

https://github.com/privatenumber/mac-ocr

A macOS-native CLI and Node.js library for performing OCR on images and PDFs using Apple's Vision framework. It supports text extraction to stdout or files in text, JSON, and JSONL formats, and can generate searchable PDFs by adding an invisible selectable text layer over scanned pages. Features include support for BCP-47 language codes, a high-accuracy neural net mode and a fast character-based mode, and on-device processing for privacy.

Tokens
14.8K
Snippets
48
Records
64
Agent score
61%

What's inside mac-ocr

  1. How OCR strategies and partitioning work

    develop

    When creating searchable PDFs, you can control how the Vision framework analyzes pages using --ocr-strategy.

    • standard: Uses full-page OCR only.
    • partitioned: Recursively splits large pages into smaller regions along their longer axis. This helps recover small text (like form labels) that might be missed during a single full-page pass. This mode may take longer as regions are processed serially.
    • auto (default): Starts with a full-page pass, then runs a partitioned pass only for large pages where text is small or missing.

    Constraints:

    • partitioned mode cannot be combined with --roi (Region of Interest).
    • auto mode skips partitioning if --roi is set.
    # Force partitioned OCR for better accuracy on small text
    mac-ocr searchable-pdf --ocr-strategy partitioned scan.pdf
  2. Manage OCR concurrency and queue limits

    develop

    The ocr() function calls run one at a time. A large Promise.all() burst will not increase throughput; use a serial loop or an application-level concurrency limit instead.

    Queue Limits:

    • The waiting queue accepts up to 512 calls.
    • The queue has a 64 MiB memory budget.
    • If the limit is exceeded, calls reject with a MacOcrError with code: 'queue_capacity_exceeded'.
  3. Compare Accurate vs Fast recognition levels

    develop

    The --fast flag switches between two distinct recognition algorithms. They are not the same algorithm with different speeds; they use different models and have different capabilities.

    Accurate (Default)

    • Approach: Neural network that reasons over whole words, lines, and sentences.
    • Best for: Post-processing files, natural-language prose, rotated/skewed text, and stylized fonts.
    • Trade-offs: Slower, higher memory usage, but much higher accuracy.

    Fast

    • Approach: Traditional OCR locating and recognizing individual characters one at a time.
    • Best for: Real-time capture (e.g., camera frames), scanning short, structured strings like serial numbers or phone numbers.
    • Trade-offs: Faster, lower memory, but weak support for rotated text, stylized fonts, or complex prose.

    Note: The set of supported languages may differ between these two modes. Check mac-ocr languages --fast to see the subset available for the fast path.

  4. Install mac-ocr

    develop

    You can install mac-ocr globally via npm to use it as a command-line tool, or run it on-demand using npx.

    Requirements: macOS 10.15+. The package includes a prebuilt universal binary, so no Xcode or Swift toolchain is required.

    npm install -g mac-ocr

    Or run without installing

    npx mac-ocr receipt.jpg
  5. Recognize text from images and PDFs

    develop

    OCR is the default action for the mac-ocr command. It supports images, multi-page PDFs, stdin, and URLs.

    By default, the output is plain text sent to stdout. You can request structured data using the --format flag:

    • text (default): Plain text.
    • json: Detailed output including bounding boxes, confidence, and page metadata.
    • jsonl: One JSON object per page, streamed (ideal for large PDFs).

    Examples:

    mac-ocr receipt.jpg                 # text → stdout
    mac-ocr page1.png page2.png         # multiple images
    mac-ocr scan.pdf                    # multi-page PDF
    cat screenshot.png | mac-ocr        # stdin
    mac-ocr https://example.com/a.png   # URL (simple GET)
    
    # Structured output
    mac-ocr receipt.jpg --format json
    mac-ocr document.pdf --format jsonl
    mac-ocr receipt.jpg --format json
  6. Write OCR results to files using templates

    develop

    By default, results are sent to stdout. Use -o, --output <dest> to save to files.

    Modes:

    • Fixed path: -o notes.txt (One file, single input only).
    • Directory mode: -o out/ (Creates out/<input>.txt per input).
    • Template mode: -o '[name].md' (Renders a unique filename per input). Note: Always quote template arguments in shells like zsh to avoid glob expansion.

    Template Placeholders:

    • [name]: Input filename without extension.
    • [ext]: Input filename extension without the dot.
    • [dir]: Input directory.
    • [page]: 1-based page number (use this to write one file per page).
    • [pagecount]: Total page count.

    Example:

    # Save recognized text as Markdown files next to the inputs
    mac-ocr scans/*.pdf -o '[name].md'
    mac-ocr scan.pdf -o notes.md
    mac-ocr scans/*.pdf -o '[name].md'
  7. Debug searchable-PDF generation

    develop

    To diagnose OCR accuracy and geometry, set the environment variable MAC_OCR_DEBUG=1.

    What happens in Debug Mode:

    • Visual Overlays: The generated PDF will include visible colored boxes:
      • Red: Accepted line boxes.
      • Blue: Word boxes.
      • Orange: Rejected observations.
    • Sidecar Data: A JSONL file is created next to the output PDF (e.g., lease.pdf $\rightarrow$ lease.jsonl). Each line contains detailed metadata for one page, including OCR geometry, recognition passes, and confidence values.

    Note: Debug mode requires file output and will fail if using -o - (stdout).

  8. Run OCR with the mac-ocr CLI

    develop

    The default action of mac-ocr is to recognize text in images and PDFs. You can pass a single file, multiple files, a URL, or use stdin for input.

    Common usage patterns:

    • Single file: mac-ocr photo.png (outputs text to stdout).
    • Multi-page PDF: mac-ocr scan.pdf (streams text page by page).
    • Multiple images: mac-ocr a.png b.png.
    • Stdin: cat screenshot.png | mac-ocr.
    • URL: mac-ocr https://example.com/img.png (supports simple GET only).
    • Batch output to files: mac-ocr shots/*.png -o '[dir]/[name].txt' (writes a .txt file next to each input image).
    mac-ocr photo.png                       # text → stdout
    mac-ocr scan.pdf                         # multi-page PDF, streamed
    mac-ocr a.png b.png c.png                # multiple images
    cat screenshot.png | mac-ocr             # stdin (auto-detected)
    mac-ocr https://example.com/img.png      # URL (simple GET only)
    mac-ocr shots/*.png -o '[dir]/[name].txt'  # a .txt next to each image
    mac-ocr --format jsonl scans/*.pdf       # streaming JSONL for big jobs
  9. Create a searchable PDF

    develop

    The searchable-pdf subcommand takes a PDF or an image and produces a new PDF that looks identical to the source but includes a selectable and searchable text layer.

    Key Behaviors:

    • Default Output: Writes [name].ocr.pdf next to each input.
    • Merge Mode: Use --merge to combine multiple inputs into a single searchable PDF. The order of the resulting pages follows the order of the arguments provided.
    • Skipping Pages: By default, pages that already contain selectable text are skipped. Use --ocr-all-pages to force OCR on every page.

    Examples:

    # Single file
    mac-ocr searchable-pdf scan.pdf            # writes scan.ocr.pdf
    
    # Multiple files (individual outputs)
    mac-ocr searchable-pdf *.pdf                # writes <name>.ocr.pdf for each
    
    # Merged output
    mac-ocr searchable-pdf --merge -o lease.pdf page1.jpg page2.jpg
    
    # Controlling destination
    mac-ocr searchable-pdf scan.pdf -o out/              # out/scan.ocr.pdf
    mac-ocr searchable-pdf scan.pdf -o '[name]-ocr.pdf'  # scan-ocr.pdf
    mac-ocr searchable-pdf scan.pdf -o - > scan.pdf      # stdout
    mac-ocr searchable-pdf --merge -o lease.pdf page1.jpg page2.jpg
  10. Save recognized text to files

    develop

    Use the -o or --output flag to save recognized text to a file, a directory, or a filename template.

    Templates:

    • [name]: Original filename without extension.
    • [ext]: Original extension.
    • [dir]: Original directory.
    • [page]: Page number.

    Note: When using templates in shells like zsh, wrap the template in quotes to avoid globbing errors.

    Examples:

    # Save a .txt file next to each image
    mac-ocr ~/Screenshots/*.png -o '[dir]/[name].txt'
    
    # Save all recognized text from a PDF to a single file
    mac-ocr scan.pdf -o notes.md
    
    # Save outputs to a specific directory
    mac-ocr receipts/*.pdf -o out/
    mac-ocr ~/Screenshots/*.png -o '[dir]/[name].txt'
  11. Use the mac-ocr CLI for text recognition

    develop

    The mac-ocr CLI provides text recognition (OCR) for images and PDFs. It supports file paths, stdin (via -), and HTTP(S) URLs (capped at 100 MiB). Multiple inputs are processed in order. PDFs are detected by magic bytes and honor EXIF orientation for width, height, and bounding box reporting.

    Basic Usage:

    • Recognize text from a file: mac-ocr image.png
    • Recognize text from a PDF: mac-ocr document.pdf
    • Process multiple files: mac-ocr file1.jpg file2.png
    • Use piped stdin: cat image.png | mac-ocr
    • Use a URL: mac-ocr https://example.com/image.png
    mac-ocr receipt.jpg
    mac-ocr scan.pdf --format jsonl
    mac-ocr a.png b.png c.png
    cat shot.png | mac-ocr
    mac-ocr https://example.com/sign.png