UNPKG

ppu-paddle-ocr

Version:

Blazing-fast and lightweight PaddleOCR library for Node.js and Bun. Perform accurate text detection, recognition, and image deskew with a simple, modern, and type-safe API. Ideal for document processing, data extraction, and computer vision tasks.

105 lines (104 loc) 3.81 kB
import { Canvas } from "ppu-ocv"; import type { PaddleOptions } from "../interface"; import { type RecognitionResult } from "./recognition.service"; export interface PaddleOcrResult { text: string; lines: RecognitionResult[][]; confidence: number; } export interface FlattenedPaddleOcrResult { text: string; results: RecognitionResult[]; confidence: number; } /** * PaddleOcrService - Provides OCR functionality using PaddleOCR models. * To use this service, create an instance and call the `initialize()` method. */ export declare class PaddleOcrService { private options; private detectionSession; private recognitionSession; /** * Creates an instance of PaddleOcrService. * @param options - Configuration options for the service. */ constructor(options?: PaddleOptions); /** * Logs a message if verbose debugging is enabled. */ private log; /** * Fetches a resource from a URL and caches it locally. * If the resource is already in the cache, it loads it from there. */ private _fetchAndCache; /** * Loads a resource from a buffer, a file path, a URL, or a default URL. */ private _loadResource; /** * Initializes the OCR service by loading models and dictionary. * This method must be called before any OCR operations. */ initialize(): Promise<void>; /** * Checks if the service has been initialized with models loaded. */ isInitialized(): boolean; /** * Changes the detection model for the current instance. * @param model - The new detection model as a path, URL, or ArrayBuffer. */ changeDetectionModel(model: ArrayBuffer | string): Promise<void>; /** * Changes the recognition model for the current instance. * @param model - The new recognition model as a path, URL, or ArrayBuffer. */ changeRecognitionModel(model: ArrayBuffer | string): Promise<void>; /** * Changes the text dictionary for the current instance. * @param dictionary - The new dictionary as a path, URL, ArrayBuffer, or string content. */ changeTextDictionary(dictionary: ArrayBuffer | string): Promise<void>; /** * Runs OCR and returns a flattened list of recognized text boxes. * * @param image - The raw image data as an ArrayBuffer or Canvas. * @param options - Options object with `flatten` set to `true`. * @return A promise that resolves to a flattened result object. */ recognize(image: ArrayBuffer | Canvas, options: { flatten: true; dictionary?: string | ArrayBuffer; }): Promise<FlattenedPaddleOcrResult>; /** * Runs OCR and returns recognized text grouped into lines. * * @param image - The raw image data as an ArrayBuffer or Canvas. * @param options - Optional options object. If `flatten` is `false` or omitted, this structure is returned. * @return A promise that resolves to a result object with text lines. */ recognize(image: ArrayBuffer | Canvas, options?: { flatten?: false; dictionary?: string | ArrayBuffer; }): Promise<PaddleOcrResult>; /** * Processes raw recognition results to generate the final text, * grouped lines, and overall confidence. */ private processRecognition; /** * Runs deskew algorithm on the provided image buffer | canvas * * @param image - The raw image data as an ArrayBuffer or Canvas. * @return A promise that resolves deskewed image as Canvas */ deskewImage(image: ArrayBuffer | Canvas): Promise<Canvas>; /** * Releases the onnx runtime session for both * detection and recognition model. */ destroy(): Promise<void>; } export default PaddleOcrService;