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

1 lines 2.3 kB
export function calculateResizeDimensions(originalWidth,originalHeight,maxSideLength){let resizeW=originalWidth;let resizeH=originalHeight;let ratio=1;if(Math.max(resizeH,resizeW)>maxSideLength){ratio=maxSideLength/(resizeH>resizeW?resizeH:resizeW);resizeW=Math.round(resizeW*ratio);resizeH=Math.round(resizeH*ratio)}return{width:resizeW,height:resizeH,ratio}}export function applyPaddingToRect(rect,maxWidth,maxHeight,paddingVertical,paddingHorizontal){let verticalPadding=Math.round(rect.height*paddingVertical);let horizontalPadding=Math.round(rect.height*paddingHorizontal);let x=rect.x-horizontalPadding;let y=rect.y-verticalPadding;x=Math.max(0,x);y=Math.max(0,y);let rightEdge=Math.min(maxWidth,rect.x+rect.width+horizontalPadding);let bottomEdge=Math.min(maxHeight,rect.y+rect.height+verticalPadding);let width=rightEdge-x;let height=bottomEdge-y;return{x,y,width,height}}export function convertToOriginalCoordinates(rect,resizeRatio,originalWidth,originalHeight){let scaledX=rect.x/resizeRatio;let scaledY=rect.y/resizeRatio;let scaledWidth=rect.width/resizeRatio;let scaledHeight=rect.height/resizeRatio;let x=Math.max(0,Math.round(scaledX));let y=Math.max(0,Math.round(scaledY));let width=Math.min(originalWidth-x,Math.round(scaledWidth));let height=Math.min(originalHeight-y,Math.round(scaledHeight));return{x,y,width,height}}export function extractBoxesFromContours(contours,width,height,resizeRatio,originalWidth,originalHeight,minBoxArea,paddingVertical,paddingHorizontal){let boxes=[];contours.iterate((contour)=>{let rect=contours.getRect(contour);if(rect.width*rect.height<=minBoxArea){return}let paddedRect=applyPaddingToRect(rect,width,height,paddingVertical,paddingHorizontal);let finalBox=convertToOriginalCoordinates(paddedRect,resizeRatio,originalWidth,originalHeight);if(finalBox.width>5&&finalBox.height>5){boxes.push(finalBox)}});return boxes}export function extractBoxesFromRegions(regions,originalWidth,originalHeight){let boxes=[];for(let region of regions){const{bbox}=region;let box={x:Math.max(0,bbox.x0),y:Math.max(0,bbox.y0),width:bbox.x1-bbox.x0,height:bbox.y1-bbox.y0};if(box.x+box.width>originalWidth){box.width=originalWidth-box.x}if(box.y+box.height>originalHeight){box.height=originalHeight-box.y}if(box.width>5&&box.height>5){boxes.push(box)}}return boxes}