OCR · Extract text from an image1 / 1

Extract text from an image

Example on GitHub(packages/sdk/examples/ocr-fasttext.ts)

We're starting a new chapter on OCR, and we're going to extract text from an image.

OCR (optical character recognition) extracts printed text from images. The OCR_LATIN model in the SDK handles any Latin-script language. The result is an array of text blocks, each with the recognized string, a bounding box on the image, and a confidence score.

paragraph: false asks for one block per visual line, useful for a UI that highlights one line at a time. With true, the engine merges adjacent lines into paragraphs. You would call it like so:

const { blocks } = ocr({
  modelId,
  image: "./examples/qvac/ocr/input/basic_test.jpg",
  options: { paragraph: false },
});
const result = await blocks;

Each block in result carries text, a bbox (the rectangle in pixel coordinates, for drawing a highlight overlay), and a confidence score:

for (const block of result) {
  console.log(block.text);
  if (block.bbox) console.log(`BBox: [${block.bbox.join(", ")}]`);
  if (block.confidence !== undefined) {
    console.log(`Confidence: ${block.confidence.toFixed(4)}`);
  }
}

paragraph: false returns one block per visual line. Set it to true and the SDK groups lines into paragraphs based on spacing.

Note: the bbox field is in pixel coordinates relative to the input image. We use it to draw highlight overlays or to extract individual words for downstream processing.

Put it to the test

  1. Call ocr({ modelId, image: <path>, options: { paragraph: false } }) and await blocks.
  2. Loop through the blocks and log the text, bbox, and confidence.
index.ts

$ Run your code to see results

$