Skip to content

xushengfeng/eSearch-OCR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Dec 18, 2024
a4a99b6 · Dec 18, 2024
May 13, 2024
Aug 6, 2022
Dec 18, 2024
Dec 18, 2024
Oct 8, 2024
Jul 9, 2023
Jul 9, 2023
Dec 18, 2024
Aug 13, 2024
Dec 18, 2024
Aug 13, 2024
Sep 27, 2024
May 12, 2024

Repository files navigation

eSearch-OCR

本仓库是 eSearch的 OCR 服务依赖

支持本地 OCR(基于 PaddleOCRv4)

PaddleOCR License

paddle 预测库

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

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

部分模型已打包:Releases

在 js 文件下可以使用 electron 进行调试

使用

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",
    recPath: "ocr/rec.onnx",
    dic: "abcdefg...",
    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
    cv?: any;
}

对于返回的值

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函数,可单独运行,检测文字内容。具体定义可看类型提示。