UNPKG

ppu-paddle-ocr

Version:

Lightweight, probably the fastest PaddleOCR SDK in TypeScript. Runs anywhere JavaScript runs: Node.js, Bun, Deno, mobile react-native, web browsers, and browser extensions. Docker & CLI supported. The official SDK is browser-only. Accurate text detection

73 lines (72 loc) 2.65 kB
import type { InferenceSession } from "onnxruntime-common"; import type { Box, DebuggingOptions, DetectionOptions, ProcessingEngine } from "../interface.js"; import type { CoreCanvas, PlatformProvider } from "./platform.js"; /** * Result of preprocessing an image for text detection. * * Contains the normalized float tensor, dimensions, and scale factors * needed to map detection output back to original image coordinates. */ export type PreprocessDetectionResult = { /** Normalized float tensor (CHW layout, 3 channels). */ tensor: Float32Array; /** Width of the padded/resized tensor in pixels. */ width: number; /** Height of the padded/resized tensor in pixels. */ height: number; /** Scale factor applied during resize (`resized / original`). */ resizeRatio: number; /** Original image width before preprocessing. */ originalWidth: number; /** Original image height before preprocessing. */ originalHeight: number; }; /** * Service for detecting text regions in images */ export declare class BaseDetectionService { protected readonly options: DetectionOptions; protected readonly debugging: DebuggingOptions; protected readonly session: InferenceSession; protected readonly platform: PlatformProvider; protected readonly engine: ProcessingEngine; private lastDetectionCanvas; constructor(platform: PlatformProvider, session: InferenceSession, options?: Partial<DetectionOptions>, debugging?: Partial<DebuggingOptions>, engine?: ProcessingEngine); /** * Logs a message if verbose debugging is enabled */ protected log(message: string): void; /** * Main method to run text detection on an image * @param image ArrayBuffer of the image or platform-specific Canvas */ run(image: ArrayBuffer | CoreCanvas): Promise<Box[]>; /** * Preprocess an image for text detection */ private preprocessDetection; /** * Run the detection model inference */ private runInference; /** * Process detection results to extract bounding boxes */ private postprocessDetection; /** * Post-process detection using OpenCV contours (v4-compatible, more accurate) */ private postprocessWithOpenCV; /** * Post-process detection using canvas-native region detection */ private postprocessWithCanvasNative; /** * Debug the detection canvas in binary image format (thresholded) */ private debugDetectionCanvas; /** * Debug the bounding boxes by drawing a rectangle onto the original image */ private debugDetectedBoxes; }