Skip to content

ocr ​

Read the text in an image. readText and readTextBlocks run macOS Vision on the machine and never touch the network. readTextWithModel sends the image to a model provider and asks for the text.

ts
import { readText, readTextBlocks } from "std::ocr"

// The text, one line per recognized line:
const text = readText("invoice.png") with approve

// Each line with its confidence and position:
const blocks = readTextBlocks("invoice.png") with approve

The local functions return a Failure on any platform that is not macOS. They never fall back to a model provider, because an approval that named a local read must not become an upload. For offline OCR on Linux or Windows, install @agency-lang/tesseract-local.

Types ​

BoundingBox ​

Normalized to 0..1 with the origin at the top left of the image, so y grows downward.

ts
/** Normalized to 0..1 with the origin at the top left of the image, so
`y` grows downward. */
export type BoundingBox = {
  x: number;
  y: number;
  width: number;
  height: number
}

(source)

TextBlock ​

One recognized line.

ts
/** One recognized line. */
export type TextBlock = {
  text: string;
  confidence: number;
  box: BoundingBox
}

(source)

Effects ​

std::ocr ​

ts
@alwaysUnder(dir)
effect std::ocr {
  dir: string;
  filename: string
}

(source)

std::ocrCloud ​

ts
effect std::ocrCloud {
  requestedProvider: string;
  configuredModel: string;
  dir: string;
  filename: string
}

(source)

Functions ​

readTextBlocks ​

ts
readTextBlocks(
  path: string,
  language: string = "",
  fast: boolean = false,
): Result<TextBlock[]> raises <std::ocr>

Read text from an image with confidence scores and bounding boxes. Uses macOS Vision locally; requires macOS and does not upload the image.

Parameters:

NameTypeDefaultDescription
pathstringPath to a PNG, JPEG, GIF, or WebP image.
languagestring""Recognition language such as "en-US", or empty for automatic selection.
fastbooleanfalseUse fast recognition instead of accurate recognition.

Returns: Result<TextBlock[]>

Throws: std::ocr

Needs: fileRead, fileWrite, subprocess

(source)

readText ​

ts
readText(
  path: string,
  language: string = "",
  fast: boolean = false,
): Result<string>

Read text from an image as a string with one recognized line per line. Uses macOS Vision locally; requires macOS and does not upload the image.

Parameters:

NameTypeDefaultDescription
pathstringPath to a PNG, JPEG, GIF, or WebP image.
languagestring""Recognition language such as "en-US", or empty for automatic selection.
fastbooleanfalseUse fast recognition instead of accurate recognition.

Returns: Result<string>

Throws: std::ocr

Needs: fileRead, fileWrite, subprocess

(source)

readTextWithModel ​

ts
readTextWithModel(
  path: string,
  model: string = "",
  provider: string = "",
  prompt: string = "",
): Result<string> raises <std::ocrCloud>

Send an image to a vision model to transcribe its text. The result may contain invented or misread text; use readText for local OCR on macOS.

Runs in a separate conversation thread, so the image and transcription instructions do not remain in the caller's conversation. Check the result against the image when exact transcription matters.

Parameters:

NameTypeDefaultDescription
pathstringPath to a PNG, JPEG, GIF, or WebP image.
modelstring""Model to use, or empty for the run's default.
providerstring""Override the provider derived from the model name.
promptstring""Additional transcription instructions, such as names or jargon.

Returns: Result<string>

Throws: std::ocrCloud

Needs: fileRead, llm

(source)