基于 https://github.com/hiroi-sora/PaddleOCR-json 的node.js api
npm install paddleocrjsonOCR.postMessage与OCR.flush区别开.
OCR.flush返回的数据一致.
npm包管理.
ts编译.
iconv-lite包.
npm install paddleocrjson
`
`js
const OCR = require('paddleocrjson');
const OCR = require('paddleocrjson/es5'); // ES5
const ocr = new OCR('PaddleOCR-json.exe', [/ '-port=9985', '-addr=loopback' /], {
cwd: './PaddleOCR-json',
}, / debug /true);
ocr.flush({ image_path: 'path/to/test/img' })
.then((data) => console.log(data));
.then(() => ocr.terminate());
// debug = true
ocr.stdout.on('data', (chunk) =>{
console.log(chunk.toString());
});
ocr.stderr.on('data', (data) =>{
console.log(data.toString());
});
ocr.on('exit', (code) =>{
console.log('exit code: ', code);
});
`
$3
详见PunchlY/PaddleOCR-json-node-api/test.
api
$3
OCR是worker_threads.Worker的派生类.
#### new OCR(path, args, options, debug)
`js
const ocr = new OCR('PaddleOCR_json.exe', [], {
cwd: './PaddleOCR-json',
}, false);
`
args详见Node.js child_process.spawn与hiroi-sora/PaddleOCR-json#配置参数说明.
options详见Node.js child_process.spawn.
#### OCR.flush(obj)
`js
ocr.flush({
/ image_path: null, // -image_path=clipboard /
image_path: 'path/to/test/img',
// limit_side_len: 960,
// ...
});
`
ocr.flush返回的是Promise对象.
obj详见hiroi-sora/PaddleOCR-json/blob/main/docs/详细使用指南.md#配置参数
如果要识别剪贴板,请传入{ image_path: null }(obj.image_dir设置为null).
#### 其他
你可以用worker_threads.Worker的api来监听或操作OCR实例.
例如:
`js
ocr.on('init', (pid, addr, port) => {
console.log('OCR init completed.');
console.log('pid:', pid);
console.log('addr:', addr);
console.log('port:', port);
});
ocr.on('message', (data) => {
console.log(data);
// { code: ..., message: ..., data: ... }
});
`
License
`
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2023 PunchlY
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
1. You just DO WHAT THE FUCK YOU WANT TO.
``