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.
94 lines (93 loc) • 2.77 kB
TypeScript
import * as ort from "onnxruntime-node";
import { Canvas } from "ppu-ocv";
import type { Box, DebuggingOptions, DetectionOptions } from "../interface";
/**
* Result of preprocessing an image for text detection
*/
export interface PreprocessDetectionResult {
tensor: Float32Array;
width: number;
height: number;
resizeRatio: number;
originalWidth: number;
originalHeight: number;
}
/**
* Service for detecting text regions in images
*/
export declare class DetectionService {
private readonly options;
private readonly debugging;
private readonly session;
private static readonly NUM_CHANNELS;
constructor(session: ort.InferenceSession, options?: Partial<DetectionOptions>, debugging?: Partial<DebuggingOptions>);
/**
* Logs a message if verbose debugging is enabled
*/
private log;
/**
* Main method to run text detection on an image
* @param image ArrayBuffer of the image or Canvas
*/
run(image: ArrayBuffer | Canvas): Promise<Box[]>;
/**
* Atomic method to run image deskewing
* @param image ArrayBuffer of the image or Canvas
*/
deskew(image: ArrayBuffer | Canvas): Promise<Canvas>;
/**
* Runs a lightweight detection pass to determine the average text skew angle.
* Uses multiple methods to robustly calculate skew from all detected text regions.
* @param canvas The input canvas.
* @returns The calculated skew angle in degrees.
*/
private calculateSkewAngle;
/**
* Preprocess an image for text detection
*/
private preprocessDetection;
/**
* Calculate dimensions for resizing the image
*/
private calculateResizeDimensions;
/**
* Create a padded canvas from the resized image
*/
private createPaddedCanvas;
/**
* Convert an image to a normalized tensor for model input
*/
private imageToTensor;
/**
* Run the detection model inference
*/
private runInference;
/**
* Convert a tensor to a canvas for visualization and processing
*/
private tensorToCanvas;
/**
* Process detection results to extract bounding boxes
*/
private postprocessDetection;
/**
* Extract boxes from contours
*/
private extractBoxesFromContours;
/**
* Apply padding to a rectangle
*/
private applyPaddingToRect;
/**
* Convert coordinates from resized image back to original image
*/
private convertToOriginalCoordinates;
/**
* Debug the detection canvas in binary image format (thresholded)
*/
private debugDetectionCanvas;
/**
* Debug the bounding boxes by drawinga rectangle onto the original image
*/
private debugDetectedBoxes;
}