UNPKG

ppu-paddle-ocr

Version:

A lightweight, type safe, PaddleOCR implementation in Bun/Node.js for text detection and recognition in JavaScript environments.

97 lines (96 loc) 3.47 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 * * This service can be used either as a singleton or as separate instances * depending on your application needs. */ export declare class PaddleOcrService { private static instance; private options; private detectionSession; private recognitionSession; /** * Create a new PaddleOcrService instance * @param options Optional configuration options */ constructor(options?: PaddleOptions); /** * Logs a message if verbose debugging is enabled */ private log; /** * Initialize the OCR service by loading models * @param overrideOptions Optional parameters to override the constructor options */ initialize(overrideOptions?: Partial<PaddleOptions>): Promise<void>; /** * Get or create the singleton instance of PaddleOcrService * @param options Configuration options for the service * @returns A promise resolving to the singleton instance * @example * const service = await PaddleOcrService.getInstance({ * verbose: true, * detectionModelPath: './models/myDetection.onnx' * }); */ static getInstance(options?: PaddleOptions): Promise<PaddleOcrService>; /** * Check if the service is initialized with models loaded */ isInitialized(): boolean; /** * Change models in the singleton instance * @param options New configuration options */ static changeModel(options: Partial<PaddleOptions>): Promise<PaddleOcrService>; /** * Create a new instance instead of using the singleton * This is useful when you need multiple instances with different models * @param options Configuration options for this specific instance */ static createInstance(options?: PaddleOptions): Promise<PaddleOcrService>; /** * 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; }): 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; }): Promise<PaddleOcrResult>; /** * Processes raw recognition results to generate the final text, * grouped lines, and overall confidence. */ private processRecognition; /** * Releases the onnx runtime session for both * detection and recognition model. */ destroy(): Promise<void>; } export default PaddleOcrService;