🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

esearch-ocr

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esearch-ocr

paddleocr models run on onnx

7.0.0
latest
npm
Version published
Maintainers
0
Created
Source

eSearch-OCR

本仓库是 eSearch的 OCR 服务依赖

支持本地 OCR(基于 PaddleOCRv4)

特性

  • 文字检测
  • 文字识别
  • 简单的段落合并
  • 轻量,仅需要引入 onnx,gizp 后 10kB
  • 支持浏览器(esm)、node(CommonJS) 和 Electron
  • 完善的类型提示
  • 可简单分析背景色和文字颜色

PaddleOCR License

基于onnxruntime的 web runtime,使用 wasm 运行,未来可能使用 webgl 甚至是 webgpu。

部分模型已打包:Releases 4.0.0,由 paddleOCR 官方的模型转换而来。

模型文字: 中(简体)英混合、中文繁体、英文、韩文、日文、泰卢固文、卡纳达文、泰米尔文、拉丁文、阿拉伯字母、斯拉夫字母、梵文字母。

使用

npm i esearch-ocr onnxruntime-web

web

import * as ocr from "esearch-ocr";
import * as ort from "onnxruntime-web";
const ocr = require("esearch-ocr");
const ort = require("onnxruntime-node");

[!IMPORTANT] 需要手动安装 onnxruntime(onnxruntime-node 或 onnxruntime-web,视平台而定),并在init参数中传入ort 这样设计是因为 web 和 electron 可以使用不同的 ort,很难协调,不如让开发者自己决定

浏览器或 Electron 示例

await ocr.init({
    detPath: "ocr/det.onnx", // det指识别模型,如果上面提到的文字包没有,那就用中英混合的det(在ch.zip里)。
    recPath: "ocr/rec.onnx",
    dic: "abcdefg...", // 在模型压缩包中的txt文件,需要传入里面的内容而不是路径
    ort,
});

const url = "data:image/png;base64,..."; // 还支持 HTMLImageElement | HTMLCanvasElement | ImageData
ocr.ocr(url)
    .then((result) => {}) // 见下面的解释
    .catch((e) => {});

或者

const localOCR = await ocr.init({
    detPath: "ocr/det.onnx",
    recPath: "ocr/rec.onnx",
    dic: "abcdefg...",
    ort,
});

localOCR.ocr(/*像上面ocr.ocr一样调用*/);

这在需要多次运行 ocr 时非常有用

node.js 示例,需要安装canvas

演示项目

init type

{
    ort: typeof import("onnxruntime-web");
    detPath: string;
    recPath: string;
    dic: string; // 文件内容,不是路径
    dev?: boolean;
    imgh?: number;
    detRatio?: number; // 缩放,小于1 越小越快,但准确率也会下降一点
    canvas?: (w: number, h: number) => any; // 用于node
    imageData?: any; // 用于node
}

对于返回的值

type resultType = {
    text: string;
    mean: number;
    box: BoxType; // ↖ ↗ ↘ ↙
    style: { bg: color; text: color }; // rgb数组,表示背景颜色和文字颜色,在简单移除文字时非常有用
}[];

{
    src: resultType; // 每个视觉行,rec输出
    columns: {
        // 分栏,如左右分栏
        src: resultType;
        outerBox: BoxType;
        parragraphs: {
            src: resultType;
            parse: resultType[0];
        }
        [];
    }
    [];
    parragraphs: resultType; // 聚合了columns的每个段落
}

合并的文字可以使用

result.parragraphs.map((item) => item.text).join("\n");

除了 ocr 函数,还有det函数,可单独运行,检测文字坐标;rec函数,可单独运行,检测文字内容。具体定义可看类型提示。

模型

paddle 预测库

模型需要转换为 onnx 才能使用:Paddle2ONNX在线转换

调试

使用 Electron 来调试,既有可视化也要onnxruntime-node的本地性能。

Keywords

ocr

FAQs

Package last updated on 11 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts