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 • 1.6 kB
JavaScript
export class WebPlatformProvider{pathSeparator="/";ort=ort;createCanvas(_width,_height){let canvas=document.createElement("canvas");canvas.width=_width;canvas.height=_height;canvas.getContext("2d",{willReadFrequently:true});return canvas}isCanvas(image){return!!(image instanceof HTMLCanvasElement||typeof OffscreenCanvas!=="undefined"&&image instanceof OffscreenCanvas||image&&typeof image.getContext==="function")}async loadResource(source,defaultUrl){if(source instanceof ArrayBuffer){return source}let sourceToLoad=typeof source==="string"?source:defaultUrl;let response=await fetch(sourceToLoad);if(!response.ok){throw new Error(`Failed to fetch resource from ${sourceToLoad}`)}return response.arrayBuffer()}async saveDebugImage(_canvas,_filename,_outputDir){return Promise.resolve()}canvas={prepareCanvas:(image)=>CanvasProcessor.prepareCanvas(image),createProcessor:(canvas)=>new CanvasProcessor(canvas),getToolkit:()=>CanvasToolkit.getInstance()}}import*as ort from"onnxruntime-web";import{CanvasProcessor,CanvasToolkit}from"ppu-ocv/canvas-web";if(typeof window!=="undefined"&&!ort.env.wasm.wasmPaths){ort.env.wasm.wasmPaths="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.26.0/dist/"}export async function isWebGpuAvailable(){if(typeof navigator==="undefined")return false;let nav=navigator;if(!nav.gpu||typeof nav.gpu.requestAdapter!=="function")return false;try{let adapter=await nav.gpu.requestAdapter();return adapter!==null&&adapter!==undefined}catch{return false}}export async function getDefaultWebExecutionProviders(){if(await isWebGpuAvailable()){return["webgpu","wasm"]}return["wasm"]}