# paddle-ocr.js **Repository Path**: Agions/paddle-ocr.js ## Basic Information - **Project Name**: paddle-ocr.js - **Description**: 基于飞桨PaddleOCR的JavaScript封装库,提供浏览器和Node.js环境下的OCR能力。 - **Primary Language**: TypeScript - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-05 - **Last Updated**: 2026-04-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: ocr ## README # PaddleOCR-JS
> 🚀 PaddleOCR JavaScript 封装,支持浏览器和 Node.js 的 OCR 识别 ## 特性 - 📝 **文本识别** - 支持 80+ 语言的中英文识别 - 📊 **表格识别** - 精准识别表格结构,输出 HTML/Markdown/Excel - 🔢 **公式识别** - 识别数学公式,输出 LaTeX/MathML - 📱 **条码识别** - 支持 QR 码、条形码等 10+ 格式 - 🗂️ **布局分析** - 自动识别文档布局结构 - ⚡ **高性能** - 模型缓存、Web Worker 支持 - 🌐 **跨平台** - 浏览器、小程序、Node.js ## 安装 ```bash # npm npm install paddleocr-js # yarn yarn add paddleocr-js # pnpm pnpm add paddleocr-js ``` ## 快速开始 ### 浏览器环境 ```html ``` ### Node.js 环境 ```javascript import { PaddleOCR } from 'paddleocr-js'; import fs from 'fs'; async function main() { const paddleOCR = new PaddleOCR({ language: 'ch', useWasm: true }); await paddleOCR.init(); const imageBuffer = fs.readFileSync('image.jpg'); const result = await paddleOCR.recognize(imageBuffer); console.log('识别结果:', result.textRecognition); } main(); ``` ### ES Modules ```javascript import { PaddleOCR } from 'paddleocr-js'; async function detectText() { const paddleOCR = new PaddleOCR({ modelPath: '/models', useWasm: true, language: 'ch' }); await paddleOCR.init(); const result = await paddleOCR.recognize('https://example.com/image.jpg'); console.log(result); } detectText(); ``` ## 功能示例 ### 文本识别 ```javascript const paddleOCR = new PaddleOCR({ language: 'ch', // 语言: ch, en, fr, de, ja, ko... useWasm: true, // 使用 WebAssembly 加速 enableGPU: false, // 启用 GPU 加速 detectionModel: 'DB', // 检测模型: DB, DB++, EAST recognitionModel: 'CRNN' // 识别模型: CRNN, SVTR }); await paddleOCR.init(); const result = await paddleOCR.recognize(imageSource); // 结果结构 // { // textDetection: [{ box: [...], score: 0.95 }], // textRecognition: [{ text: '识别文本', score: 0.92 }], // duration: { total: 1200, detection: 800, recognition: 400 } // } ``` ### 表格识别 ```javascript const paddleOCR = new PaddleOCR({ enableTable: true, tableOptions: { format: 'html' // 输出格式: html, markdown, excel } }); await paddleOCR.init(); const tableResult = await paddleOCR.recognizeTable(imageSource); // tableResult.html // HTML 表格 // tableResult.markdown // Markdown 表格 // tableResult.excel // Base64 编码的 Excel ``` ### 公式识别 ```javascript const paddleOCR = new PaddleOCR({ enableFormula: true, formulaOptions: { enableLatex: true, enableMathML: false } }); const formulas = await paddleOCR.recognizeFormula(imageSource); // formulas[0].latex // LaTeX 格式: \frac{a}{b} // formulas[0].tex // 原始 TeX ``` ### 条码识别 ```javascript const paddleOCR = new PaddleOCR({ enableBarcode: true }); const barcodes = await paddleOCR.detectBarcodes(imageSource); // barcodes[0].type // 'qr_code', 'code_128'... // barcodes[0].data // 编码内容 ``` ### 批量处理 ```javascript const images = ['img1.jpg', 'img2.jpg', 'img3.jpg']; const batchResult = await paddleOCR.recognizeBatch(images); // batchResult.results // OCRResult 数组 // batchResult.totalDuration // 总耗时 // batchResult.averageDuration // 平均耗时 ``` ### 布局分析 ```javascript const paddleOCR = new PaddleOCR({ enableLayout: true }); const layout = await paddleOCR.analyzeLayout(imageSource); // layout.regions[0].type // 'text', 'title', 'figure', 'table'... // layout.regions[0].box // 区域坐标 // layout.regions[0].content // 区域内容 ``` ## 配置选项 | 选项 | 类型 | 默认值 | 说明 | |------|------|--------|------| | `modelPath` | string | `/models` | 模型路径 | | `useWasm` | boolean | `true` | 使用 WebAssembly | | `useTensorflow` | boolean | `true` | 使用 TensorFlow.js | | `useONNX` | boolean | `false` | 使用 ONNX Runtime | | `language` | string | `ch` | 识别语言 | | `enableTable` | boolean | `false` | 启用表格识别 | | `enableFormula` | boolean | `false` | 启用公式识别 | | `enableBarcode` | boolean | `false` | 启用条码识别 | | `enableLayout` | boolean | `false` | 启用布局分析 | | `enableCache` | boolean | `true` | 启用缓存 | | `maxSideLen` | number | `960` | 最大边长 | | `threshold` | number | `0.3` | 检测阈值 | | `batchSize` | number | `1` | 批处理大小 | | `numThreads` | number | `4` | WASM 线程数 | ## API ### PaddleOCR 主类,提供 OCR 识别功能。 ```typescript class PaddleOCR { constructor(options?: PaddleOCROptions) init(): Promise