QVAC Logo

ocr( )

Performs Optical Character Recognition (OCR) on an image to extract text.

function ocr(params): {
  blockStream: AsyncGenerator<OCRTextBlock[]>;
  blocks: Promise<OCRTextBlock[]>;
  stats: Promise<OCRStats | undefined>;
};

Parameters

NameTypeRequired?Description
paramsOCRClientParamsThe OCR parameters

OCRClientParams

FieldTypeRequired?DefaultDescription
modelIdstringThe identifier of the loaded OCR model
imagestring | BufferImage input as file path (string) or image buffer
optionsOCROptionsOptional OCR options
streambooleanfalseWhether to stream blocks as they're detected

OCROptions

FieldTypeRequired?Description
paragraphbooleanEnable paragraph mode

Returns

object — Object with the following fields:

FieldTypeDescription
blockStreamAsyncGenerator<OCRTextBlock[]>Stream of detected text blocks (active when stream: true)
blocksPromise<OCRTextBlock[]>All detected text blocks (populated when stream: false)
statsPromise<OCRStats | undefined>Performance statistics

OCRTextBlock

FieldTypeDescription
textstringDetected text
bbox[number, number, number, number]Bounding box [x1, y1, x2, y2] (optional)
confidencenumberDetection confidence score (optional)

OCRStats

FieldTypeDescription
detectionTimenumberDetection phase time in ms (optional)
recognitionTimenumberRecognition phase time in ms (optional)
totalTimenumberTotal OCR time in ms (optional)

Example

// Non-streaming mode (default)
const { blocks } = ocr({ modelId, image: "/path/to/image.png" });
for (const block of await blocks) {
  console.log(block.text, block.bbox, block.confidence);
}

// Streaming mode
const { blockStream } = ocr({ modelId, image: imageBuffer, stream: true });
for await (const blocks of blockStream) {
  console.log("Detected:", blocks);
}

On this page