generated from qber-soft/Ave-React-Template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpaddle-ocr.ts
115 lines (104 loc) · 3.19 KB
/
paddle-ocr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import axios from "axios";
import path from "path";
import fs from "fs";
import childProcess from "child_process";
import { IOcrEngine, IOcrEngineOptions, IOcrResult } from "./base";
import { inspectLog, ErrorEvent } from "../server";
export interface IPaddleOcrResult extends IOcrResult {
detail: Array<IPaddleOcrItem>;
}
// prettier-ignore
export type IPaddleOcrItem = [
[
[number, number],
[number, number],
[number, number],
[number, number]
],
[string, number]
];
export class PaddleOcrEngine implements IOcrEngine {
private options: IOcrEngineOptions;
private ocr: childProcess.ChildProcessWithoutNullStreams;
constructor(options: IOcrEngineOptions) {
this.options = options;
}
async init() {
console.log("try to init paddle ocr engine");
const paddleDir = path.resolve(process.cwd(), "ocr-server");
const exePath = path.resolve(paddleDir, "./PaddleocrAPI.exe");
if (fs.existsSync(paddleDir) && fs.existsSync(exePath)) {
return new Promise((resolve, reject) => {
console.log("paddleDir exists, start ocr server", paddleDir);
const port = this.options.ocrPort;
const ocr = childProcess.spawn(`./ocr-server/PaddleocrAPI.exe`, [`--lang=en`, `--model-dir=.\\model`, `--port=${port}`], { windowsHide: true, detached: false /** hide console */ });
this.ocr = ocr;
ocr.stdout.on("data", (data) => {
const log = data?.toString() ?? "";
const isError = inspectLog(log);
if(isError) {
reject(false);
}
if(log.endsWith("INFO") || log.endsWith("INFO : ") || log === "\r\n" || log.includes("size:") || log.includes("Request ContentType")) {
// ignore them
} else {
console.log(`stdout: ${log}`);
}
if (data.includes("PaddleocrAPI has been started")) {
console.log("ocr server started");
resolve(true);
}
});
ocr.stderr.on("data", (data) => {
const isError = inspectLog(data?.toString());
if(isError) {
reject(false);
}
console.error(`stderr: ${data}`);
});
ocr.on("close", (code) => {
console.log(`ocr server exit: ${code}`);
reject(false);
});
});
} else {
console.log(ErrorEvent.OcrServerNotExist.log);
inspectLog(ErrorEvent.OcrServerNotExist.log);
}
}
async destroy() {
if (this.ocr) {
console.log("exit ocr server process");
process.kill(this.ocr?.pid);
process.exit();
}
}
async recognize(buffer: Buffer): Promise<IOcrResult> {
const base64 = buffer.toString("base64");
let text = "";
try {
const timeout = this.options.timeout;
const port = this.options.ocrPort;
const response = await axios.post(
`http://localhost:${port}/ocr`,
{
image: base64,
},
{ timeout }
);
const result = response.data.image as Array<IPaddleOcrItem>;
result.forEach((item, index) => {
const [box, content] = item;
text += content[0];
if (index !== result.length - 1) {
text += "\n";
}
});
} catch (error) {
console.log(`ocr failed: ${error.message}`);
this.options?.onError(error.message);
} finally {
return { text };
}
}
}