@arcships/light-ocr 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +14 -0
- package/js/index.cjs +204 -0
- package/js/index.d.ts +145 -0
- package/js/index.mjs +4 -0
- package/js/load-native.cjs +80 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @arcships/light-ocr
|
|
2
|
+
|
|
3
|
+
Offline OCR for Node.js applications, powered by PP-OCRv6 Small.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @arcships/light-ocr
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The default PP-OCRv6 Small model is included as a required dependency; no
|
|
10
|
+
runtime model download or postinstall compilation is used.
|
|
11
|
+
|
|
12
|
+
Documentation: https://github.com/arcships/light-ocr
|
|
13
|
+
|
|
14
|
+
License: Apache-2.0
|
package/js/index.cjs
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
|
|
6
|
+
const { loadNative } = require('./load-native.cjs');
|
|
7
|
+
|
|
8
|
+
const DEFAULT_MODEL = 'ppocrv6-small';
|
|
9
|
+
const MODEL_PACKAGE = '@arcships/light-ocr-model-ppocrv6-small';
|
|
10
|
+
const EXPECTED_BUNDLE_ID = 'ppocrv6-small-onnx-20260714.1';
|
|
11
|
+
|
|
12
|
+
class OcrError extends Error {
|
|
13
|
+
constructor(code, message, detail) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = 'OcrError';
|
|
16
|
+
this.code = code;
|
|
17
|
+
if (detail !== undefined && detail !== '') this.detail = detail;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizeNativeError(error) {
|
|
22
|
+
if (error && error.name === 'OcrError') {
|
|
23
|
+
Object.setPrototypeOf(error, OcrError.prototype);
|
|
24
|
+
}
|
|
25
|
+
return error;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function deepFreeze(value) {
|
|
29
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value;
|
|
30
|
+
for (const child of Object.values(value)) deepFreeze(child);
|
|
31
|
+
return Object.freeze(value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function validateSignal(signal) {
|
|
35
|
+
if (
|
|
36
|
+
signal === null ||
|
|
37
|
+
typeof signal !== 'object' ||
|
|
38
|
+
typeof signal.aborted !== 'boolean' ||
|
|
39
|
+
typeof signal.addEventListener !== 'function' ||
|
|
40
|
+
typeof signal.removeEventListener !== 'function'
|
|
41
|
+
) {
|
|
42
|
+
throw new OcrError('invalid_argument', 'signal must be an AbortSignal');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function abortReason(signal) {
|
|
47
|
+
return signal.reason === undefined
|
|
48
|
+
? new DOMException('The operation was aborted', 'AbortError')
|
|
49
|
+
: signal.reason;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function resolveBuiltInBundle(model) {
|
|
53
|
+
if (model !== DEFAULT_MODEL) {
|
|
54
|
+
throw new OcrError(
|
|
55
|
+
'invalid_argument',
|
|
56
|
+
`model must be ${JSON.stringify(DEFAULT_MODEL)}`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
let manifestPath;
|
|
60
|
+
try {
|
|
61
|
+
manifestPath = require.resolve(`${MODEL_PACKAGE}/bundle/manifest.json`);
|
|
62
|
+
} catch (cause) {
|
|
63
|
+
throw new OcrError(
|
|
64
|
+
'package_load_failed',
|
|
65
|
+
`Unable to locate the built-in ${DEFAULT_MODEL} model`,
|
|
66
|
+
`Reinstall ${MODEL_PACKAGE}; ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
let manifest;
|
|
70
|
+
try {
|
|
71
|
+
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
72
|
+
} catch (cause) {
|
|
73
|
+
throw new OcrError(
|
|
74
|
+
'package_load_failed',
|
|
75
|
+
'Unable to read the built-in model manifest',
|
|
76
|
+
cause instanceof Error ? cause.message : String(cause),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (manifest.bundleId !== EXPECTED_BUNDLE_ID) {
|
|
80
|
+
throw new OcrError(
|
|
81
|
+
'package_load_failed',
|
|
82
|
+
'The installed model package is incompatible with this light-ocr release',
|
|
83
|
+
`expected ${EXPECTED_BUNDLE_ID}, received ${String(manifest.bundleId)}`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
return path.dirname(manifestPath);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function resolveCreateOptions(options) {
|
|
90
|
+
if (options === undefined) options = {};
|
|
91
|
+
if (options === null || typeof options !== 'object' || Array.isArray(options)) {
|
|
92
|
+
throw new OcrError('invalid_argument', 'createEngine options must be an object');
|
|
93
|
+
}
|
|
94
|
+
const hasModel = Object.prototype.hasOwnProperty.call(options, 'model');
|
|
95
|
+
const hasBundlePath = Object.prototype.hasOwnProperty.call(options, 'bundlePath');
|
|
96
|
+
if (hasModel && hasBundlePath) {
|
|
97
|
+
throw new OcrError(
|
|
98
|
+
'invalid_argument',
|
|
99
|
+
'model and bundlePath cannot be used together',
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (hasBundlePath) return options;
|
|
103
|
+
const model = hasModel ? options.model : DEFAULT_MODEL;
|
|
104
|
+
const resolved = { ...options, bundlePath: resolveBuiltInBundle(model) };
|
|
105
|
+
delete resolved.model;
|
|
106
|
+
return resolved;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
class OcrEngineImpl {
|
|
110
|
+
#native;
|
|
111
|
+
#closePromise;
|
|
112
|
+
|
|
113
|
+
constructor(nativeEngine) {
|
|
114
|
+
this.#native = nativeEngine;
|
|
115
|
+
this.info = deepFreeze(nativeEngine.info);
|
|
116
|
+
Object.defineProperty(this, 'info', { writable: false, configurable: false });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
recognize(image, options = {}) {
|
|
120
|
+
let signal;
|
|
121
|
+
let nativeOptions;
|
|
122
|
+
try {
|
|
123
|
+
if (options === null || typeof options !== 'object' || Array.isArray(options)) {
|
|
124
|
+
throw new OcrError('invalid_argument', 'recognize options must be an object');
|
|
125
|
+
}
|
|
126
|
+
signal = options.signal;
|
|
127
|
+
if (signal !== undefined) {
|
|
128
|
+
validateSignal(signal);
|
|
129
|
+
if (signal.aborted) return Promise.reject(abortReason(signal));
|
|
130
|
+
}
|
|
131
|
+
nativeOptions = { ...options };
|
|
132
|
+
delete nativeOptions.signal;
|
|
133
|
+
} catch (error) {
|
|
134
|
+
return Promise.reject(normalizeNativeError(error));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let operation;
|
|
138
|
+
try {
|
|
139
|
+
operation = this.#native.recognize(image, nativeOptions);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return Promise.reject(normalizeNativeError(error));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
let settled = false;
|
|
146
|
+
const cleanup = () => {
|
|
147
|
+
if (signal !== undefined) signal.removeEventListener('abort', onAbort);
|
|
148
|
+
};
|
|
149
|
+
const settle = (callback, value) => {
|
|
150
|
+
if (settled) return;
|
|
151
|
+
settled = true;
|
|
152
|
+
cleanup();
|
|
153
|
+
callback(value);
|
|
154
|
+
};
|
|
155
|
+
const onAbort = () => {
|
|
156
|
+
if (settled) return;
|
|
157
|
+
try {
|
|
158
|
+
this.#native.cancel(operation.requestId);
|
|
159
|
+
} catch {
|
|
160
|
+
// Public cancellation still wins. Native teardown owns any remaining work.
|
|
161
|
+
}
|
|
162
|
+
settle(reject, abortReason(signal));
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
operation.promise.then(
|
|
166
|
+
(value) => settle(resolve, value),
|
|
167
|
+
(error) => settle(reject, normalizeNativeError(error)),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
if (signal !== undefined) {
|
|
171
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
172
|
+
if (signal.aborted) onAbort();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
close() {
|
|
178
|
+
if (this.#closePromise === undefined) {
|
|
179
|
+
try {
|
|
180
|
+
this.#closePromise = Promise.resolve(this.#native.close()).catch((error) => {
|
|
181
|
+
throw normalizeNativeError(error);
|
|
182
|
+
});
|
|
183
|
+
} catch (error) {
|
|
184
|
+
this.#closePromise = Promise.reject(normalizeNativeError(error));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return this.#closePromise;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
let binding;
|
|
192
|
+
|
|
193
|
+
async function createEngine(options) {
|
|
194
|
+
try {
|
|
195
|
+
const resolvedOptions = resolveCreateOptions(options);
|
|
196
|
+
if (!binding) binding = loadNative();
|
|
197
|
+
const nativeEngine = await binding.createEngine(resolvedOptions);
|
|
198
|
+
return new OcrEngineImpl(nativeEngine);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
throw normalizeNativeError(error);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = { createEngine, OcrError };
|
package/js/index.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
export type PixelFormat = 'gray8' | 'rgb8' | 'bgr8' | 'rgba8';
|
|
4
|
+
export type DetectionStrategy = 'bounded' | 'upstreamExact';
|
|
5
|
+
export type BuiltInModel = 'ppocrv6-small';
|
|
6
|
+
|
|
7
|
+
export interface DetectionOptions {
|
|
8
|
+
readonly strategy?: DetectionStrategy;
|
|
9
|
+
readonly maxSide?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RawImage {
|
|
13
|
+
readonly data: Uint8Array;
|
|
14
|
+
readonly width: number;
|
|
15
|
+
readonly height: number;
|
|
16
|
+
readonly stride: number;
|
|
17
|
+
readonly pixelFormat: PixelFormat;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ResourceLimits {
|
|
21
|
+
readonly maxWidth: number;
|
|
22
|
+
readonly maxHeight: number;
|
|
23
|
+
readonly maxPixels: number;
|
|
24
|
+
readonly maxDetectionSide: number;
|
|
25
|
+
readonly maxDetectionCandidates: number;
|
|
26
|
+
readonly maxRecognitionBatchSize: number;
|
|
27
|
+
readonly maxRecognitionWidth: number;
|
|
28
|
+
readonly maxTemporaryBytes: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface CreateEngineOptions {
|
|
32
|
+
readonly model?: BuiltInModel;
|
|
33
|
+
readonly bundlePath?: string;
|
|
34
|
+
readonly intraOpThreads?: number;
|
|
35
|
+
readonly interOpThreads?: number;
|
|
36
|
+
readonly recognitionScoreThreshold?: number;
|
|
37
|
+
readonly recognitionBatchSize?: number;
|
|
38
|
+
readonly reducedLimits?: ResourceLimits;
|
|
39
|
+
readonly queueCapacity?: number;
|
|
40
|
+
readonly maxPendingInputBytes?: number;
|
|
41
|
+
readonly detection?: DetectionOptions;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface RecognizeOptions {
|
|
45
|
+
readonly recognitionScoreThreshold?: number;
|
|
46
|
+
readonly recognitionBatchSize?: number;
|
|
47
|
+
readonly includeDiagnostics?: boolean;
|
|
48
|
+
readonly signal?: AbortSignal;
|
|
49
|
+
readonly useTextlineOrientation?: boolean;
|
|
50
|
+
readonly detectionMaxSide?: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface Point { readonly x: number; readonly y: number }
|
|
54
|
+
export interface OcrLine {
|
|
55
|
+
readonly text: string;
|
|
56
|
+
readonly confidence: number;
|
|
57
|
+
readonly box: readonly [Point, Point, Point, Point];
|
|
58
|
+
}
|
|
59
|
+
export type RejectionReason = 'below_score_threshold' | 'empty_decode';
|
|
60
|
+
export interface RejectedLine { readonly line: OcrLine; readonly reason: RejectionReason }
|
|
61
|
+
export interface DiagnosticWarning { readonly code: string; readonly message: string }
|
|
62
|
+
export interface RecognitionBatchShape {
|
|
63
|
+
readonly batchSize: number;
|
|
64
|
+
readonly height: number;
|
|
65
|
+
readonly width: number;
|
|
66
|
+
}
|
|
67
|
+
export interface Diagnostics {
|
|
68
|
+
readonly rejectedLines: readonly RejectedLine[];
|
|
69
|
+
readonly warnings: readonly DiagnosticWarning[];
|
|
70
|
+
readonly detectedCandidates: number;
|
|
71
|
+
readonly acceptedBoxes: number;
|
|
72
|
+
readonly detectionInputWidth: number;
|
|
73
|
+
readonly detectionInputHeight: number;
|
|
74
|
+
readonly recognitionBatchShapes: readonly RecognitionBatchShape[];
|
|
75
|
+
}
|
|
76
|
+
export interface TimingUs {
|
|
77
|
+
readonly total: number;
|
|
78
|
+
readonly inputValidation: number;
|
|
79
|
+
readonly detectionPreprocess: number;
|
|
80
|
+
readonly detectionInference: number;
|
|
81
|
+
readonly detectionPostprocess: number;
|
|
82
|
+
readonly cropAndSort: number;
|
|
83
|
+
readonly recognitionPreprocess: number;
|
|
84
|
+
readonly recognitionInference: number;
|
|
85
|
+
readonly recognitionPostprocess: number;
|
|
86
|
+
}
|
|
87
|
+
export interface OcrResult {
|
|
88
|
+
readonly lines: readonly OcrLine[];
|
|
89
|
+
readonly imageWidth: number;
|
|
90
|
+
readonly imageHeight: number;
|
|
91
|
+
readonly modelBundleId: string;
|
|
92
|
+
readonly timingUs: TimingUs;
|
|
93
|
+
readonly diagnostics?: Diagnostics;
|
|
94
|
+
}
|
|
95
|
+
export interface EngineInfo {
|
|
96
|
+
readonly coreVersion: string;
|
|
97
|
+
readonly modelBundleId: string;
|
|
98
|
+
readonly modelBundleSchemaVersion: string;
|
|
99
|
+
readonly backend: string;
|
|
100
|
+
readonly executionProvider: string;
|
|
101
|
+
readonly capabilities: {
|
|
102
|
+
readonly detection: boolean;
|
|
103
|
+
readonly recognition: boolean;
|
|
104
|
+
readonly textlineOrientation: boolean;
|
|
105
|
+
};
|
|
106
|
+
readonly concurrencyMode: 'serialized_reject_when_busy';
|
|
107
|
+
readonly limits: ResourceLimits & { readonly maxConcurrentCalls: 1 };
|
|
108
|
+
readonly intraOpThreads: number;
|
|
109
|
+
readonly interOpThreads: number;
|
|
110
|
+
readonly detectionStrategy: DetectionStrategy;
|
|
111
|
+
readonly detectionMaxSide: number;
|
|
112
|
+
readonly defaultRecognitionScoreThreshold: number;
|
|
113
|
+
readonly defaultRecognitionBatchSize: number;
|
|
114
|
+
readonly adapter: {
|
|
115
|
+
readonly scheduler: 'dedicated_fifo';
|
|
116
|
+
readonly queueCapacity: number;
|
|
117
|
+
readonly maxPendingInputBytes: number;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type CoreErrorCode =
|
|
122
|
+
| 'invalid_argument' | 'invalid_image' | 'unsupported_pixel_format'
|
|
123
|
+
| 'unsupported_capability' | 'invalid_model_bundle' | 'unsupported_model'
|
|
124
|
+
| 'model_integrity_failed' | 'runtime_initialization_failed' | 'inference_failed'
|
|
125
|
+
| 'postprocess_failed' | 'resource_limit_exceeded' | 'invalid_engine'
|
|
126
|
+
| 'internal_error';
|
|
127
|
+
export type AdapterErrorCode =
|
|
128
|
+
| 'bundle_io_failed' | 'queue_full' | 'environment_closing'
|
|
129
|
+
| 'unsupported_platform' | 'package_load_failed';
|
|
130
|
+
export type OcrErrorCode = CoreErrorCode | AdapterErrorCode;
|
|
131
|
+
|
|
132
|
+
export class OcrError extends Error {
|
|
133
|
+
constructor(code: OcrErrorCode, message: string, detail?: string);
|
|
134
|
+
readonly name: 'OcrError';
|
|
135
|
+
readonly code: OcrErrorCode;
|
|
136
|
+
readonly detail?: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface OcrEngine {
|
|
140
|
+
readonly info: EngineInfo;
|
|
141
|
+
recognize(image: RawImage, options?: RecognizeOptions): Promise<OcrResult>;
|
|
142
|
+
close(): Promise<void>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function createEngine(options?: CreateEngineOptions): Promise<OcrEngine>;
|
package/js/index.mjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
|
|
6
|
+
function candidatePaths() {
|
|
7
|
+
const candidates = [];
|
|
8
|
+
if (process.env.LIGHT_OCR_NODE_BINARY) {
|
|
9
|
+
candidates.push(path.resolve(process.env.LIGHT_OCR_NODE_BINARY));
|
|
10
|
+
}
|
|
11
|
+
candidates.push(path.join(__dirname, 'native', 'light_ocr_node.node'));
|
|
12
|
+
candidates.push(
|
|
13
|
+
path.join(
|
|
14
|
+
__dirname,
|
|
15
|
+
'..',
|
|
16
|
+
'prebuilds',
|
|
17
|
+
`${process.platform}-${process.arch}`,
|
|
18
|
+
'light_ocr_node.node',
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
return candidates;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function adapterError(code, message, detail, cause) {
|
|
25
|
+
const error = new Error(message, cause === undefined ? undefined : { cause });
|
|
26
|
+
error.name = 'OcrError';
|
|
27
|
+
error.code = code;
|
|
28
|
+
if (detail) error.detail = detail;
|
|
29
|
+
return error;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function platformPackage() {
|
|
33
|
+
const key = `${process.platform}-${process.arch}`;
|
|
34
|
+
const packages = {
|
|
35
|
+
'darwin-arm64': '@arcships/light-ocr-darwin-arm64',
|
|
36
|
+
'darwin-x64': '@arcships/light-ocr-darwin-x64',
|
|
37
|
+
'win32-x64': '@arcships/light-ocr-win32-x64',
|
|
38
|
+
};
|
|
39
|
+
if (key === 'linux-x64') {
|
|
40
|
+
const report = process.report?.getReport?.();
|
|
41
|
+
if (report?.header?.glibcVersionRuntime) {
|
|
42
|
+
return '@arcships/light-ocr-linux-x64-gnu';
|
|
43
|
+
}
|
|
44
|
+
throw adapterError(
|
|
45
|
+
'unsupported_platform',
|
|
46
|
+
'light-ocr currently supports Linux x64 with glibc only',
|
|
47
|
+
`${process.platform}-${process.arch}`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const packageName = packages[key];
|
|
51
|
+
if (!packageName) {
|
|
52
|
+
throw adapterError(
|
|
53
|
+
'unsupported_platform',
|
|
54
|
+
`light-ocr does not support ${key}`,
|
|
55
|
+
key,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return packageName;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function loadNative() {
|
|
62
|
+
const candidates = candidatePaths();
|
|
63
|
+
const binary = candidates.find((candidate) => fs.existsSync(candidate));
|
|
64
|
+
if (binary) return require(binary);
|
|
65
|
+
|
|
66
|
+
const packageName = platformPackage();
|
|
67
|
+
try {
|
|
68
|
+
return require(packageName);
|
|
69
|
+
} catch (cause) {
|
|
70
|
+
throw adapterError(
|
|
71
|
+
'package_load_failed',
|
|
72
|
+
`Unable to load ${packageName}`,
|
|
73
|
+
'Reinstall @arcships/light-ocr without --omit=optional and verify that the ' +
|
|
74
|
+
'current platform is supported.',
|
|
75
|
+
cause,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = { loadNative };
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bugs": {
|
|
3
|
+
"url": "https://github.com/arcships/light-ocr/issues"
|
|
4
|
+
},
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@arcships/light-ocr-model-ppocrv6-small": "0.1.0"
|
|
7
|
+
},
|
|
8
|
+
"description": "Offline PP-OCRv6 OCR for Node.js, powered by an embeddable C++ core",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": "^22.0.0 || ^24.0.0"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./js/index.mjs",
|
|
15
|
+
"require": "./js/index.cjs",
|
|
16
|
+
"types": "./js/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"js/",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"NOTICE"
|
|
24
|
+
],
|
|
25
|
+
"homepage": "https://github.com/arcships/light-ocr#readme",
|
|
26
|
+
"keywords": [
|
|
27
|
+
"ocr",
|
|
28
|
+
"offline-ocr",
|
|
29
|
+
"pp-ocrv6",
|
|
30
|
+
"paddleocr",
|
|
31
|
+
"node-api",
|
|
32
|
+
"napi"
|
|
33
|
+
],
|
|
34
|
+
"license": "Apache-2.0",
|
|
35
|
+
"main": "./js/index.cjs",
|
|
36
|
+
"module": "./js/index.mjs",
|
|
37
|
+
"name": "@arcships/light-ocr",
|
|
38
|
+
"optionalDependencies": {
|
|
39
|
+
"@arcships/light-ocr-darwin-arm64": "0.1.0",
|
|
40
|
+
"@arcships/light-ocr-darwin-x64": "0.1.0",
|
|
41
|
+
"@arcships/light-ocr-linux-x64-gnu": "0.1.0",
|
|
42
|
+
"@arcships/light-ocr-win32-x64": "0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public",
|
|
46
|
+
"provenance": true
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/arcships/light-ocr.git"
|
|
51
|
+
},
|
|
52
|
+
"type": "commonjs",
|
|
53
|
+
"types": "./js/index.d.ts",
|
|
54
|
+
"version": "0.1.0"
|
|
55
|
+
}
|