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
2 lines • 980 B
JavaScript
import{existsSync,mkdirSync,readFileSync,writeFileSync}from"fs";import*as os from"os";import*as path from"path";import{fetchArrayBufferWithRetry}from"../utils.js";export let CACHE_DIR=path.join(os.homedir(),".cache","ppu-paddle-ocr");export async function fetchAndCacheResource(url,verbose){let fileName=path.basename(new URL(url).pathname);let cachePath=path.join(CACHE_DIR,fileName);if(existsSync(cachePath)){if(verbose)console.log(`[PaddleOcrService] Loading cached resource from: ${cachePath}`);let buf=readFileSync(cachePath);return buf.buffer.slice(buf.byteOffset,buf.byteOffset+buf.byteLength)}console.log(`[PaddleOcrService] Downloading resource: ${fileName}
`+` Cached at: ${CACHE_DIR}`);if(verbose)console.log(`[PaddleOcrService] Fetching resource from URL: ${url}`);let arrayBuffer=await fetchArrayBufferWithRetry(url);if(!existsSync(CACHE_DIR)){mkdirSync(CACHE_DIR,{recursive:true})}writeFileSync(cachePath,Buffer.from(arrayBuffer));return arrayBuffer}