@exige/web-scanner-sdk 0.2.17 → 0.2.23
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/README.md +20 -17
- package/dist/assets/document-worker.js +1 -1
- package/dist/assets/document-worker.js.map +1 -1
- package/dist/{finder-D0Wgl-6-.js → finder-CLTG1caj.js} +357 -274
- package/dist/finder-CLTG1caj.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/react.js +45 -41
- package/dist/react.js.map +1 -1
- package/dist/scanner-sdk.js +317 -319
- package/dist/scanner-sdk.js.map +1 -1
- package/dist/sdk/camera.d.ts +69 -31
- package/dist/sdk/finder.d.ts +14 -11
- package/dist/sdk/lighting.d.ts +6 -0
- package/dist/sdk/ocr.d.ts +7 -3
- package/dist/sdk/ort-runtime.d.ts +1 -1
- package/dist/sdk/scan.d.ts +1 -0
- package/dist/sdk/types.d.ts +2 -0
- package/package.json +1 -1
- package/dist/finder-D0Wgl-6-.js.map +0 -1
- package/dist/sdk/preview-frame.d.ts +0 -27
package/dist/scanner-sdk.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as e, C as t, D as n, E as r, F as i, I as a,
|
|
1
|
+
import { A as e, C as t, D as n, E as r, F as i, I as a, L as o, M as s, N as c, O as l, P as u, R as d, S as f, T as p, _ as m, a as h, b as g, c as _, d as v, f as ee, g as te, h as ne, i as re, j as ie, k as y, l as ae, m as oe, n as se, o as ce, p as le, r as ue, s as de, t as fe, u as pe, v as me, w as he, x as ge, y as _e } from "./finder-CLTG1caj.js";
|
|
2
2
|
//#region src/sdk/frame-geometry.ts
|
|
3
3
|
function b(e, t, n) {
|
|
4
4
|
let r = Math.max(1, t.width), i = Math.max(1, t.height), a = Math.max(1, n.width), o = Math.max(1, n.height), s = Math.max(r / a, i / o), c = (r - a * s) / 2, l = (i - o * s) / 2;
|
|
@@ -7,7 +7,7 @@ function b(e, t, n) {
|
|
|
7
7
|
y: (e.y - l) / s
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
-
function
|
|
10
|
+
function ve(e, t, n) {
|
|
11
11
|
return {
|
|
12
12
|
topLeft: b(e.topLeft, t, n),
|
|
13
13
|
topRight: b(e.topRight, t, n),
|
|
@@ -57,7 +57,7 @@ function S(e, t, n) {
|
|
|
57
57
|
y: n.centerY + e * i + t * r
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
|
-
function
|
|
60
|
+
function ye(e) {
|
|
61
61
|
let t = S(-e.width / 2, -e.height / 2, e), n = S(e.width / 2, -e.height / 2, e), r = S(e.width / 2, e.height / 2, e), i = S(-e.width / 2, e.height / 2, e);
|
|
62
62
|
return {
|
|
63
63
|
topLeft: t,
|
|
@@ -86,13 +86,13 @@ function _e(e) {
|
|
|
86
86
|
}
|
|
87
87
|
function C(e, t) {
|
|
88
88
|
let n = e.getContext("2d");
|
|
89
|
-
if (!n) throw
|
|
89
|
+
if (!n) throw d();
|
|
90
90
|
let r = n.createLinearGradient(0, 0, e.width, e.height);
|
|
91
91
|
r.addColorStop(0, "#16231c"), r.addColorStop(1, "#32443a"), n.fillStyle = r, n.fillRect(0, 0, e.width, e.height), n.save(), n.translate(t.centerX, t.centerY), n.rotate(t.rotation), n.shadowColor = "rgba(0,0,0,.45)", n.shadowBlur = 24, n.fillStyle = "#fffdf4", n.strokeStyle = "#111914", n.lineWidth = 7, n.fillRect(-t.width / 2, -t.height / 2, t.width, t.height), n.strokeRect(-t.width / 2, -t.height / 2, t.width, t.height), n.shadowColor = "transparent";
|
|
92
|
-
let i = -t.width / 2,
|
|
93
|
-
n.fillStyle = "#d7ded8", n.fillRect(i + 38,
|
|
92
|
+
let i = -t.width / 2, a = -t.height / 2;
|
|
93
|
+
n.fillStyle = "#d7ded8", n.fillRect(i + 38, a + 84, 145, 190), n.fillStyle = "#617269", n.beginPath(), n.arc(i + 110, a + 140, 38, 0, Math.PI * 2), n.fill(), n.fillRect(i + 70, a + 185, 80, 65), n.fillStyle = "#17231c", n.font = "700 25px Arial", n.fillText("IDENTITY CARD", i + 38, a + 48), n.font = "700 31px Arial", n.fillText("TAYLOR MORGAN", i + 220, a + 118), n.font = "23px Arial", n.fillText("01/02/1990", i + 220, a + 166), n.fillText("A1234567", i + 220, a + 208), n.fillText("123 MAIN ST", i + 220, a + 270), n.fillText("AUSTIN TX 78701", i + 220, a + 307), n.restore();
|
|
94
94
|
}
|
|
95
|
-
function
|
|
95
|
+
function be(e) {
|
|
96
96
|
e.width ||= 960, e.height ||= 600;
|
|
97
97
|
let t = !1, n = 0, r = 0, i = (a) => {
|
|
98
98
|
t && (n ||= a, C(e, x(a - n, e.width, e.height)), r = requestAnimationFrame(i));
|
|
@@ -111,7 +111,7 @@ function ve(e) {
|
|
|
111
111
|
}
|
|
112
112
|
//#endregion
|
|
113
113
|
//#region src/sdk/ocr-runtime.ts
|
|
114
|
-
var
|
|
114
|
+
var xe = new URL("./assets/tesseract/worker.min.js", import.meta.url), Se = new URL("./assets/tesseract/tesseract-core-lstm.wasm.js", import.meta.url), Ce = new URL("./assets/tesseract/tesseract-core-simd-lstm.wasm.js", import.meta.url), we = new URL("./assets/tesseract/tesseract-core-relaxedsimd-lstm.wasm.js", import.meta.url), Te = new URL("./assets/tesseract/eng.traineddata.gz", import.meta.url), Ee = Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 15, 1, 13, 0, 65, 1, 253, 15, 65, 2, 253, 15, 253, 128, 2, 11), De = Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0, 253, 15, 253, 98, 11);
|
|
115
115
|
function w(e) {
|
|
116
116
|
try {
|
|
117
117
|
return typeof WebAssembly < "u" && WebAssembly.validate(e);
|
|
@@ -119,16 +119,16 @@ function w(e) {
|
|
|
119
119
|
return !1;
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
function
|
|
122
|
+
function Oe() {
|
|
123
123
|
return {
|
|
124
|
-
relaxedSimd: w(
|
|
125
|
-
simd: w(
|
|
124
|
+
relaxedSimd: w(Ee),
|
|
125
|
+
simd: w(De)
|
|
126
126
|
};
|
|
127
127
|
}
|
|
128
|
-
function
|
|
129
|
-
return e.relaxedSimd ?
|
|
128
|
+
function ke(e) {
|
|
129
|
+
return e.relaxedSimd ? we : e.simd ? Ce : Se;
|
|
130
130
|
}
|
|
131
|
-
function
|
|
131
|
+
function Ae(e, t) {
|
|
132
132
|
let n = e?.trim() || "eng", r = t?.trim() || "";
|
|
133
133
|
if (r) return {
|
|
134
134
|
kind: "lang-path",
|
|
@@ -139,22 +139,22 @@ function Oe(e, t) {
|
|
|
139
139
|
kind: "bundled-eng",
|
|
140
140
|
language: "eng"
|
|
141
141
|
};
|
|
142
|
-
throw new
|
|
142
|
+
throw new o("language", `OCR language "${n}" is not packaged. Pass langPath, a directory URL that contains ${n}.traineddata.gz.`);
|
|
143
143
|
}
|
|
144
|
-
function
|
|
144
|
+
function je(e = Te) {
|
|
145
145
|
let t = e.href, n = t.lastIndexOf("/");
|
|
146
146
|
return n === -1 ? t : t.slice(0, n);
|
|
147
147
|
}
|
|
148
|
-
function
|
|
149
|
-
return e.kind === "bundled-eng" ?
|
|
148
|
+
function Me(e) {
|
|
149
|
+
return e.kind === "bundled-eng" ? je() : e.langPath;
|
|
150
150
|
}
|
|
151
|
-
async function
|
|
152
|
-
return new Promise((r,
|
|
153
|
-
let
|
|
154
|
-
if (
|
|
155
|
-
|
|
151
|
+
async function Ne(e, t, n) {
|
|
152
|
+
return new Promise((r, i) => {
|
|
153
|
+
let a = !1, s = (e) => {
|
|
154
|
+
if (a) return;
|
|
155
|
+
a = !0;
|
|
156
156
|
let t = e instanceof Error ? e.message : String(e);
|
|
157
|
-
|
|
157
|
+
i(new o("worker", `OCR worker failed to start (${t}).`));
|
|
158
158
|
};
|
|
159
159
|
e(t, void 0, {
|
|
160
160
|
workerPath: n.workerPath,
|
|
@@ -164,11 +164,11 @@ async function je(e, t, n) {
|
|
|
164
164
|
logger: n.logger,
|
|
165
165
|
errorHandler: s
|
|
166
166
|
}).then((e) => {
|
|
167
|
-
if (
|
|
167
|
+
if (a) {
|
|
168
168
|
e.terminate?.();
|
|
169
169
|
return;
|
|
170
170
|
}
|
|
171
|
-
|
|
171
|
+
a = !0, r(e);
|
|
172
172
|
}, s);
|
|
173
173
|
});
|
|
174
174
|
}
|
|
@@ -190,13 +190,13 @@ function E(e = .5) {
|
|
|
190
190
|
if (!Number.isFinite(e) || e < 0 || e > 1) throw RangeError("OCR minConfidence must be between 0 and 1.");
|
|
191
191
|
return e;
|
|
192
192
|
}
|
|
193
|
-
function
|
|
193
|
+
function Pe(e, t) {
|
|
194
194
|
let n = Math.max(e.bbox.x, t.bbox.x), r = Math.max(e.bbox.y, t.bbox.y), i = Math.min(e.bbox.x + e.bbox.width, t.bbox.x + t.bbox.width), a = Math.min(e.bbox.y + e.bbox.height, t.bbox.y + t.bbox.height), o = Math.max(0, i - n) * Math.max(0, a - r), s = Math.min(e.bbox.width * e.bbox.height, t.bbox.width * t.bbox.height);
|
|
195
195
|
return s > 0 ? o / s : 0;
|
|
196
196
|
}
|
|
197
197
|
function D(e) {
|
|
198
198
|
let t = [...e].sort((e, t) => t.confidence - e.confidence || e.bbox.y - t.bbox.y || e.bbox.x - t.bbox.x || e.text.localeCompare(t.text)), n = [];
|
|
199
|
-
for (let e of t) n.some((t) =>
|
|
199
|
+
for (let e of t) n.some((t) => Pe(t, e) >= .65) || n.push(e);
|
|
200
200
|
return n;
|
|
201
201
|
}
|
|
202
202
|
function O(e, t, n, r, i) {
|
|
@@ -210,7 +210,7 @@ function O(e, t, n, r, i) {
|
|
|
210
210
|
}
|
|
211
211
|
return !0;
|
|
212
212
|
}
|
|
213
|
-
function
|
|
213
|
+
function Fe(e) {
|
|
214
214
|
let t = [...e].sort((e, t) => e.bbox.y + e.bbox.height / 2 - (t.bbox.y + t.bbox.height / 2) || e.bbox.x - t.bbox.x), n = [];
|
|
215
215
|
for (let e of t) {
|
|
216
216
|
let t = e.bbox.y + e.bbox.height / 2, r = n.find((n) => Math.abs(n.centerY - t) <= Math.max(n.height, e.bbox.height) * .6);
|
|
@@ -241,31 +241,15 @@ function k(e, t, n, r) {
|
|
|
241
241
|
}
|
|
242
242
|
let l = n?.width ?? s, u = n?.height ?? c, d = D(a).filter((e) => O(e, "word", l, u, r)), f = D(o).filter((e) => O(e, "character", l, u, r));
|
|
243
243
|
return {
|
|
244
|
-
text:
|
|
244
|
+
text: Fe(d),
|
|
245
245
|
confidence: d.length ? d.reduce((e, t) => e + t.confidence, 0) / d.length : 0,
|
|
246
246
|
words: t.includeWords ? d : [],
|
|
247
247
|
characters: t.includeCharacters ? f : []
|
|
248
248
|
};
|
|
249
249
|
}
|
|
250
|
-
function Pe(e) {
|
|
251
|
-
let t = /* @__PURE__ */ new Uint32Array(256);
|
|
252
|
-
for (let n of e) t[n] += 1;
|
|
253
|
-
let n = 0;
|
|
254
|
-
for (let e = 0; e < 256; e += 1) n += e * t[e];
|
|
255
|
-
let r = 0, i = 0, a = -1, o = 127;
|
|
256
|
-
for (let s = 0; s < 256; s += 1) {
|
|
257
|
-
if (r += t[s], !r) continue;
|
|
258
|
-
let c = e.length - r;
|
|
259
|
-
if (!c) break;
|
|
260
|
-
i += s * t[s];
|
|
261
|
-
let l = i / r, u = (n - i) / c, d = r * c * (l - u) ** 2;
|
|
262
|
-
d > a && (a = d, o = s);
|
|
263
|
-
}
|
|
264
|
-
return o;
|
|
265
|
-
}
|
|
266
250
|
function A(e) {
|
|
267
251
|
let t = e.getContext("2d", { willReadFrequently: !0 });
|
|
268
|
-
if (!t) throw
|
|
252
|
+
if (!t) throw d();
|
|
269
253
|
let n = t.getImageData(0, 0, e.width, e.height), r = new Uint8Array(e.width * e.height);
|
|
270
254
|
for (let e = 0, t = 0; e < r.length; e += 1, t += 4) r[e] = Math.round(n.data[t] * .299 + n.data[t + 1] * .587 + n.data[t + 2] * .114);
|
|
271
255
|
return {
|
|
@@ -274,16 +258,6 @@ function A(e) {
|
|
|
274
258
|
luminance: r
|
|
275
259
|
};
|
|
276
260
|
}
|
|
277
|
-
function Fe(e) {
|
|
278
|
-
let t = 1800, n = Math.max(e.width, e.height), r = n > t ? t / n : Math.min(2, t / Math.max(1, n)), i = document.createElement("canvas");
|
|
279
|
-
i.width = Math.max(1, Math.round(e.width * r)), i.height = Math.max(1, Math.round(e.height * r));
|
|
280
|
-
let o = i.getContext("2d", { willReadFrequently: !0 });
|
|
281
|
-
if (!o) throw a();
|
|
282
|
-
o.drawImage(e, 0, 0, i.width, i.height);
|
|
283
|
-
let { image: s, luminance: c } = A(i), l = re(c, i.width, i.height);
|
|
284
|
-
for (let e = 0, t = 0; e < l.length; e += 1, t += 4) s.data[t] = l[e], s.data[t + 1] = l[e], s.data[t + 2] = l[e], s.data[t + 3] = 255;
|
|
285
|
-
return o.putImageData(s, 0, 0), i;
|
|
286
|
-
}
|
|
287
261
|
function Ie(e) {
|
|
288
262
|
let t = document.createElement("canvas");
|
|
289
263
|
t.width = 72, t.height = 72;
|
|
@@ -296,18 +270,38 @@ function Ie(e) {
|
|
|
296
270
|
};
|
|
297
271
|
n.drawImage(e, 0, 0, t.width, t.height);
|
|
298
272
|
let { luminance: r } = A(t);
|
|
299
|
-
return
|
|
273
|
+
return ae(r, t.width, t.height);
|
|
300
274
|
}
|
|
301
275
|
function Le(e, t, n, r) {
|
|
302
276
|
let i = (e.bbox.x + e.bbox.width / 2) / n, a = (e.bbox.y + e.bbox.height / 2) / r;
|
|
303
277
|
return i >= t.x && i <= t.x + t.width && a >= t.y && a <= t.y + t.height;
|
|
304
278
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
let
|
|
308
|
-
|
|
279
|
+
var Re = .58;
|
|
280
|
+
function ze(e) {
|
|
281
|
+
let t = e.trim();
|
|
282
|
+
if (!t || !/[\p{L}\p{N}]/u.test(t)) return 0;
|
|
283
|
+
if (/^\$?\d{1,5}([.,]\d{2})?$/.test(t)) return 1;
|
|
284
|
+
let n = t.replace(/[^\p{L}]/gu, "");
|
|
285
|
+
return n.length >= 3 && /[aeiouy]/i.test(n) ? .9 : n.length >= 4 ? .7 : t.length <= 2 ? .35 : /[^\p{L}\p{N}.,'’$/-]/u.test(t) ? .25 : .5;
|
|
286
|
+
}
|
|
287
|
+
function Be(e, t, n) {
|
|
288
|
+
let r = Math.max(0, Math.min(1, (1 - e.darkness / 80) * (1 - e.strength / 24)));
|
|
289
|
+
if (!t.length) return r * .35;
|
|
290
|
+
let i = 0;
|
|
291
|
+
for (let e of t) {
|
|
292
|
+
let t = ze(e.text);
|
|
293
|
+
if (n) {
|
|
294
|
+
let r = n(e.bbox);
|
|
295
|
+
(r < .04 || r > .62) && (t *= .4);
|
|
296
|
+
}
|
|
297
|
+
i += t;
|
|
298
|
+
}
|
|
299
|
+
return r * .7 + i / t.length * .3;
|
|
309
300
|
}
|
|
310
|
-
function
|
|
301
|
+
function Ve(e, t, n, r) {
|
|
302
|
+
return !e.strong || !e.region ? !1 : Be(e, t.filter((t) => Le(t, e.region, n.width, n.height)), r) < Re;
|
|
303
|
+
}
|
|
304
|
+
function He(e, t, n, r) {
|
|
311
305
|
let i = Math.max(0, Math.floor(t.x * e.width)), a = Math.max(0, Math.floor(t.y * e.height)), o = Math.min(e.width, Math.ceil((t.x + t.width) * e.width)), s = Math.min(e.height, Math.ceil((t.y + t.height) * e.height)), c = document.createElement("canvas");
|
|
312
306
|
c.width = Math.max(1, o - i), c.height = Math.max(1, s - a), c.getContext("2d")?.drawImage(e, i, a, c.width, c.height, 0, 0, c.width, c.height);
|
|
313
307
|
let l = n / e.width, u = r / e.height;
|
|
@@ -321,22 +315,26 @@ function ze(e, t, n, r) {
|
|
|
321
315
|
}
|
|
322
316
|
};
|
|
323
317
|
}
|
|
324
|
-
function
|
|
318
|
+
function j(e, t) {
|
|
319
|
+
for (let n = 0, r = 0; n < t.length; n += 1, r += 4) e.data[r] = t[n], e.data[r + 1] = t[n], e.data[r + 2] = t[n], e.data[r + 3] = 255;
|
|
320
|
+
}
|
|
321
|
+
function Ue(e) {
|
|
325
322
|
let t = 2200, n = Math.max(e.width, e.height), r = n > t ? t / n : Math.min(1.5, t / Math.max(1, n)), i = document.createElement("canvas");
|
|
326
323
|
i.width = Math.max(1, Math.round(e.width * r)), i.height = Math.max(1, Math.round(e.height * r));
|
|
327
|
-
let
|
|
328
|
-
if (!
|
|
329
|
-
|
|
330
|
-
let s =
|
|
331
|
-
|
|
332
|
-
let
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
324
|
+
let a = i.getContext("2d", { willReadFrequently: !0 });
|
|
325
|
+
if (!a) throw d();
|
|
326
|
+
a.drawImage(e, 0, 0, i.width, i.height);
|
|
327
|
+
let { image: o, luminance: s } = A(i), c = pe(s, i.width, i.height), l = _(c, i.width, i.height), u = document.createElement("canvas");
|
|
328
|
+
u.width = i.width, u.height = i.height;
|
|
329
|
+
let f = u.getContext("2d", { willReadFrequently: !0 });
|
|
330
|
+
if (!f) throw d();
|
|
331
|
+
let p = f.createImageData(u.width, u.height);
|
|
332
|
+
return j(p, c), f.putImageData(p, 0, 0), j(o, l), a.putImageData(o, 0, 0), {
|
|
333
|
+
sparseCanvas: i,
|
|
334
|
+
flattenedCanvas: u
|
|
335
|
+
};
|
|
338
336
|
}
|
|
339
|
-
function
|
|
337
|
+
function We(e) {
|
|
340
338
|
let t = document.createElement("canvas");
|
|
341
339
|
t.width = 24, t.height = 24;
|
|
342
340
|
let n = t.getContext("2d", { willReadFrequently: !0 });
|
|
@@ -354,88 +352,88 @@ function Ve(e) {
|
|
|
354
352
|
return l / s.length;
|
|
355
353
|
};
|
|
356
354
|
}
|
|
357
|
-
async function
|
|
358
|
-
await
|
|
359
|
-
let
|
|
355
|
+
async function M(e, t = {}) {
|
|
356
|
+
await u("ocr");
|
|
357
|
+
let n = Ae(t.language, t.langPath), r = n.language;
|
|
360
358
|
E(t.minConfidence);
|
|
361
|
-
let
|
|
359
|
+
let i = await y(e, { cloneCanvas: !1 }), { sparseCanvas: a, flattenedCanvas: o } = Ue(i), s = t.shadowPass === !1 ? {
|
|
362
360
|
strong: !1,
|
|
363
361
|
strength: 0,
|
|
364
362
|
darkness: 0,
|
|
365
363
|
areaRatio: 0
|
|
366
|
-
} : Ie(
|
|
367
|
-
workerPath:
|
|
368
|
-
corePath:
|
|
369
|
-
langPath:
|
|
364
|
+
} : Ie(i), { createWorker: c, PSM: l } = await import("tesseract.js"), d = 0, f = s.strong ? 3 : 2, p = await Ne(c, r, {
|
|
365
|
+
workerPath: xe.href,
|
|
366
|
+
corePath: ke(Oe()).href,
|
|
367
|
+
langPath: Me(n),
|
|
370
368
|
logger(e) {
|
|
371
369
|
typeof e.progress == "number" && t.onProgress?.(Math.min(1, (d + e.progress) / f), e.status ?? "recognizing text");
|
|
372
370
|
}
|
|
373
371
|
});
|
|
374
372
|
try {
|
|
375
|
-
await p.setParameters({ tessedit_pageseg_mode:
|
|
376
|
-
let e = await p.recognize(
|
|
373
|
+
await p.setParameters({ tessedit_pageseg_mode: l.AUTO });
|
|
374
|
+
let e = await p.recognize(i, {}, {
|
|
377
375
|
text: !0,
|
|
378
376
|
blocks: !0
|
|
379
377
|
});
|
|
380
378
|
d = 1, await p.setParameters({
|
|
381
|
-
tessedit_pageseg_mode:
|
|
379
|
+
tessedit_pageseg_mode: l.SPARSE_TEXT,
|
|
382
380
|
preserve_interword_spaces: "1"
|
|
383
381
|
});
|
|
384
|
-
let n = await p.recognize(
|
|
382
|
+
let n = await p.recognize(a, {}, {
|
|
385
383
|
text: !0,
|
|
386
384
|
blocks: !0
|
|
387
|
-
}),
|
|
385
|
+
}), c = [{ blocks: e.data.blocks }, {
|
|
388
386
|
blocks: n.data.blocks,
|
|
389
|
-
scaleX:
|
|
390
|
-
scaleY:
|
|
391
|
-
}],
|
|
387
|
+
scaleX: i.width / a.width,
|
|
388
|
+
scaleY: i.height / a.height
|
|
389
|
+
}], u = {
|
|
392
390
|
...t,
|
|
393
391
|
includeWords: !0
|
|
394
|
-
},
|
|
395
|
-
width:
|
|
396
|
-
height:
|
|
397
|
-
},
|
|
398
|
-
width:
|
|
399
|
-
height:
|
|
400
|
-
});
|
|
401
|
-
if (
|
|
392
|
+
}, f = We(i), m = k(c, u, {
|
|
393
|
+
width: i.width,
|
|
394
|
+
height: i.height
|
|
395
|
+
}, f), h = t.shadowPass !== !1 && Ve(s, m.words, {
|
|
396
|
+
width: i.width,
|
|
397
|
+
height: i.height
|
|
398
|
+
}, f);
|
|
399
|
+
if (h && s.region) {
|
|
402
400
|
d = 2;
|
|
403
|
-
let { tile: e, passMapping: t } =
|
|
401
|
+
let { tile: e, passMapping: t } = He(o, s.region, i.width, i.height);
|
|
404
402
|
await p.setParameters({
|
|
405
|
-
tessedit_pageseg_mode:
|
|
403
|
+
tessedit_pageseg_mode: l.SPARSE_TEXT,
|
|
406
404
|
preserve_interword_spaces: "1"
|
|
407
405
|
});
|
|
408
406
|
let n = await p.recognize(e, {}, {
|
|
409
407
|
text: !0,
|
|
410
408
|
blocks: !0
|
|
411
409
|
});
|
|
412
|
-
|
|
410
|
+
c.push({
|
|
413
411
|
blocks: n.data.blocks,
|
|
414
412
|
...t
|
|
415
413
|
});
|
|
416
414
|
}
|
|
417
|
-
let
|
|
418
|
-
width:
|
|
419
|
-
height:
|
|
420
|
-
},
|
|
415
|
+
let g = c.length === 2 ? m : k(c, u, {
|
|
416
|
+
width: i.width,
|
|
417
|
+
height: i.height
|
|
418
|
+
}, f);
|
|
421
419
|
return t.onProgress?.(1, "recognizing text"), {
|
|
422
|
-
text:
|
|
423
|
-
confidence:
|
|
424
|
-
language:
|
|
425
|
-
imageWidth:
|
|
426
|
-
imageHeight:
|
|
420
|
+
text: g.text,
|
|
421
|
+
confidence: g.confidence,
|
|
422
|
+
language: r,
|
|
423
|
+
imageWidth: i.width,
|
|
424
|
+
imageHeight: i.height,
|
|
427
425
|
diagnostics: {
|
|
428
|
-
shadowPassUsed:
|
|
429
|
-
castShadowStrength:
|
|
430
|
-
shadowRegion:
|
|
431
|
-
x:
|
|
432
|
-
y:
|
|
433
|
-
width:
|
|
434
|
-
height:
|
|
426
|
+
shadowPassUsed: h,
|
|
427
|
+
castShadowStrength: s.strength,
|
|
428
|
+
shadowRegion: s.region ? {
|
|
429
|
+
x: s.region.x * i.width,
|
|
430
|
+
y: s.region.y * i.height,
|
|
431
|
+
width: s.region.width * i.width,
|
|
432
|
+
height: s.region.height * i.height
|
|
435
433
|
} : void 0
|
|
436
434
|
},
|
|
437
|
-
...t.includeWords ? { words:
|
|
438
|
-
...t.includeCharacters ? { characters:
|
|
435
|
+
...t.includeWords ? { words: g.words } : {},
|
|
436
|
+
...t.includeCharacters ? { characters: g.characters } : {}
|
|
439
437
|
};
|
|
440
438
|
} finally {
|
|
441
439
|
await p.terminate();
|
|
@@ -443,38 +441,38 @@ async function j(e, t = {}) {
|
|
|
443
441
|
}
|
|
444
442
|
//#endregion
|
|
445
443
|
//#region src/sdk/redact.ts
|
|
446
|
-
async function
|
|
447
|
-
await
|
|
448
|
-
let
|
|
449
|
-
if (!
|
|
444
|
+
async function N(e, t, r = {}) {
|
|
445
|
+
await u("redaction");
|
|
446
|
+
let i = await y(e), a = i.getContext("2d");
|
|
447
|
+
if (!a) throw d();
|
|
450
448
|
for (let e of t) {
|
|
451
|
-
let t =
|
|
449
|
+
let t = l(e, i.width, i.height);
|
|
452
450
|
if (t.width < 1 || t.height < 1) continue;
|
|
453
|
-
if ((e.mode ??
|
|
454
|
-
|
|
451
|
+
if ((e.mode ?? r.mode ?? "blackout") === "blackout") {
|
|
452
|
+
a.fillStyle = "#050706", a.fillRect(t.x, t.y, t.width, t.height);
|
|
455
453
|
continue;
|
|
456
454
|
}
|
|
457
455
|
let n = document.createElement("canvas");
|
|
458
456
|
n.width = Math.ceil(t.width), n.height = Math.ceil(t.height);
|
|
459
|
-
let
|
|
460
|
-
if (!
|
|
461
|
-
|
|
462
|
-
let
|
|
463
|
-
|
|
457
|
+
let o = n.getContext("2d");
|
|
458
|
+
if (!o) continue;
|
|
459
|
+
o.filter = `blur(${r.blurRadius ?? 18}px)`;
|
|
460
|
+
let s = Math.max(12, r.blurRadius ?? 18);
|
|
461
|
+
o.drawImage(i, t.x - s, t.y - s, t.width + s * 2, t.height + s * 2, -s, -s, t.width + s * 2, t.height + s * 2), a.drawImage(n, t.x, t.y, t.width, t.height);
|
|
464
462
|
}
|
|
465
463
|
return {
|
|
466
|
-
canvas:
|
|
467
|
-
blob: await
|
|
464
|
+
canvas: i,
|
|
465
|
+
blob: await n(i, r.mimeType ?? "image/png", r.quality)
|
|
468
466
|
};
|
|
469
467
|
}
|
|
470
468
|
//#endregion
|
|
471
469
|
//#region src/sdk/face-model.ts
|
|
472
|
-
var
|
|
473
|
-
function
|
|
470
|
+
var P = /* @__PURE__ */ new Map(), Ge = 8400;
|
|
471
|
+
function Ke(e, t) {
|
|
474
472
|
let n = Math.max(0, Math.min(e.x + e.width, t.x + t.width) - Math.max(e.x, t.x)) * Math.max(0, Math.min(e.y + e.height, t.y + t.height) - Math.max(e.y, t.y));
|
|
475
473
|
return n / Math.max(1, e.width * e.height + t.width * t.height - n);
|
|
476
474
|
}
|
|
477
|
-
function
|
|
475
|
+
function qe(e, t, n, r, i, a, o) {
|
|
478
476
|
return {
|
|
479
477
|
x: (e - n / 2) / i * a,
|
|
480
478
|
y: (t - r / 2) / i * o,
|
|
@@ -482,48 +480,48 @@ function We(e, t, n, r, i, a, o) {
|
|
|
482
480
|
height: r / i * o
|
|
483
481
|
};
|
|
484
482
|
}
|
|
485
|
-
function
|
|
483
|
+
function Je(e, t, n) {
|
|
486
484
|
let r = t.length === 3 ? t[1] : t[0], i = t.length === 3 ? t[2] : t[1];
|
|
487
|
-
if (i !== 6 || r < 1) throw Error(`Face model expects [1, anchors, 6]. A 640 YOLOX-Nano face export is [1, ${
|
|
485
|
+
if (i !== 6 || r < 1) throw Error(`Face model expects [1, anchors, 6]. A 640 YOLOX-Nano face export is [1, ${Ge}, 6].`);
|
|
488
486
|
let a = n.confidence ?? .35, o = n.inputSize ?? 640, s = [];
|
|
489
487
|
for (let t = 0; t < r; t += 1) {
|
|
490
488
|
let r = t * i, c = e[r + 4] * e[r + 5];
|
|
491
489
|
c < a || s.push({
|
|
492
490
|
confidence: c,
|
|
493
|
-
bbox:
|
|
491
|
+
bbox: qe(e[r], e[r + 1], e[r + 2], e[r + 3], o, n.imageWidth, n.imageHeight)
|
|
494
492
|
});
|
|
495
493
|
}
|
|
496
|
-
return s.sort((e, t) => t.confidence - e.confidence).filter((e, t, n) => n.slice(0, t).every((t) =>
|
|
497
|
-
}
|
|
498
|
-
async function
|
|
499
|
-
let
|
|
500
|
-
|
|
501
|
-
let
|
|
502
|
-
if (!
|
|
503
|
-
|
|
504
|
-
let
|
|
505
|
-
for (let e = 0; e <
|
|
506
|
-
let
|
|
507
|
-
|
|
494
|
+
return s.sort((e, t) => t.confidence - e.confidence).filter((e, t, n) => n.slice(0, t).every((t) => Ke(e.bbox, t.bbox) < .45)).slice(0, 8);
|
|
495
|
+
}
|
|
496
|
+
async function Ye(e, t, n = .35) {
|
|
497
|
+
let r = await y(e, { cloneCanvas: !1 }), i = await import("onnxruntime-web"), a = document.createElement("canvas");
|
|
498
|
+
a.width = 640, a.height = 640;
|
|
499
|
+
let o = a.getContext("2d", { willReadFrequently: !0 });
|
|
500
|
+
if (!o) throw d();
|
|
501
|
+
o.drawImage(r, 0, 0, 640, 640);
|
|
502
|
+
let s = o.getImageData(0, 0, 640, 640).data, c = 409600, l = new Float32Array(c * 3);
|
|
503
|
+
for (let e = 0; e < c; e += 1) l[e] = s[e * 4] / 255, l[c + e] = s[e * 4 + 1] / 255, l[c * 2 + e] = s[e * 4 + 2] / 255;
|
|
504
|
+
let u = P.get(t);
|
|
505
|
+
u || (u = i.InferenceSession.create(t, {
|
|
508
506
|
executionProviders: ["wasm"],
|
|
509
507
|
graphOptimizationLevel: "all"
|
|
510
|
-
}),
|
|
511
|
-
let
|
|
508
|
+
}), P.set(t, u));
|
|
509
|
+
let f = await u, p = (await f.run({ [f.inputNames[0]]: new i.Tensor("float32", l, [
|
|
512
510
|
1,
|
|
513
511
|
3,
|
|
514
512
|
640,
|
|
515
513
|
640
|
|
516
|
-
]) }))[
|
|
517
|
-
return
|
|
518
|
-
confidence:
|
|
519
|
-
imageWidth:
|
|
520
|
-
imageHeight:
|
|
514
|
+
]) }))[f.outputNames[0]];
|
|
515
|
+
return Je(p.data, p.dims.map(Number), {
|
|
516
|
+
confidence: n,
|
|
517
|
+
imageWidth: r.width,
|
|
518
|
+
imageHeight: r.height,
|
|
521
519
|
inputSize: 640
|
|
522
520
|
});
|
|
523
521
|
}
|
|
524
522
|
//#endregion
|
|
525
523
|
//#region src/sdk/field-model.ts
|
|
526
|
-
var
|
|
524
|
+
var F = /* @__PURE__ */ new Map(), I = [
|
|
527
525
|
"person_name",
|
|
528
526
|
"date_of_birth",
|
|
529
527
|
"address",
|
|
@@ -531,19 +529,19 @@ var P = /* @__PURE__ */ new Map(), F = [
|
|
|
531
529
|
"expiration_date",
|
|
532
530
|
"postal_code",
|
|
533
531
|
"state"
|
|
534
|
-
],
|
|
535
|
-
function
|
|
532
|
+
], Xe = 8400;
|
|
533
|
+
function L(e) {
|
|
536
534
|
return 5 + e;
|
|
537
535
|
}
|
|
538
|
-
var
|
|
539
|
-
function
|
|
536
|
+
var R = 640;
|
|
537
|
+
function Ze(e, t) {
|
|
540
538
|
let n = Math.max(0, Math.min(e.bbox.x + e.bbox.width, t.bbox.x + t.bbox.width) - Math.max(e.bbox.x, t.bbox.x)) * Math.max(0, Math.min(e.bbox.y + e.bbox.height, t.bbox.y + t.bbox.height) - Math.max(e.bbox.y, t.bbox.y));
|
|
541
539
|
return n / Math.max(1, e.bbox.width * e.bbox.height + t.bbox.width * t.bbox.height - n);
|
|
542
540
|
}
|
|
543
|
-
function
|
|
544
|
-
return e.sort((e, t) => t.confidence - e.confidence).filter((e, t, n) => n.slice(0, t).every((t) => t.field !== e.field ||
|
|
541
|
+
function z(e) {
|
|
542
|
+
return e.sort((e, t) => t.confidence - e.confidence).filter((e, t, n) => n.slice(0, t).every((t) => t.field !== e.field || Ze(e, t) < .45)).slice(0, 30);
|
|
545
543
|
}
|
|
546
|
-
function
|
|
544
|
+
function B(e, t, n, r, i, a, o) {
|
|
547
545
|
return {
|
|
548
546
|
x: (e - n / 2) / i * a,
|
|
549
547
|
y: (t - r / 2) / i * o,
|
|
@@ -551,8 +549,8 @@ function z(e, t, n, r, i, a, o) {
|
|
|
551
549
|
height: r / i * o
|
|
552
550
|
};
|
|
553
551
|
}
|
|
554
|
-
function
|
|
555
|
-
let n =
|
|
552
|
+
function V(e, t) {
|
|
553
|
+
let n = L(t);
|
|
556
554
|
return e.length === 3 && e[0] === 1 && e[2] === n && e[1] > e[2] ? {
|
|
557
555
|
anchors: e[1],
|
|
558
556
|
features: n
|
|
@@ -561,15 +559,15 @@ function B(e, t) {
|
|
|
561
559
|
features: n
|
|
562
560
|
} : null;
|
|
563
561
|
}
|
|
564
|
-
function
|
|
562
|
+
function Qe(e) {
|
|
565
563
|
return e.length === 3 && e[1] >= 5 && e[2] >= 1;
|
|
566
564
|
}
|
|
567
|
-
function
|
|
568
|
-
let t =
|
|
569
|
-
return /* @__PURE__ */ Error(`YOLOX field layout expects [1, anchors, ${t}] (cx, cy, w, h, objectness, ${e} class scores). A 640 YOLOX-Nano export with decode baked in is [1, ${
|
|
565
|
+
function $e(e) {
|
|
566
|
+
let t = L(e);
|
|
567
|
+
return /* @__PURE__ */ Error(`YOLOX field layout expects [1, anchors, ${t}] (cx, cy, w, h, objectness, ${e} class scores). A 640 YOLOX-Nano export with decode baked in is [1, ${Xe}, ${t}].`);
|
|
570
568
|
}
|
|
571
|
-
function
|
|
572
|
-
if (!
|
|
569
|
+
function et(e, t, n, r, i, a, o) {
|
|
570
|
+
if (!Qe(t)) throw Error("Unsupported field-model YOLO output shape.");
|
|
573
571
|
let s = t[1], c = t[2], l = [];
|
|
574
572
|
for (let t = 0; t < c; t += 1) {
|
|
575
573
|
let u = 0, d = 0;
|
|
@@ -581,14 +579,14 @@ function Ze(e, t, n, r, i, a, o) {
|
|
|
581
579
|
!f || u < r || l.push({
|
|
582
580
|
field: f,
|
|
583
581
|
confidence: u,
|
|
584
|
-
bbox:
|
|
582
|
+
bbox: B(e[t], e[c + t], e[c * 2 + t], e[c * 3 + t], i, a, o)
|
|
585
583
|
});
|
|
586
584
|
}
|
|
587
|
-
return
|
|
585
|
+
return z(l);
|
|
588
586
|
}
|
|
589
|
-
function
|
|
590
|
-
let s =
|
|
591
|
-
if (!s) throw
|
|
587
|
+
function tt(e, t, n, r, i, a, o) {
|
|
588
|
+
let s = V(t, n.length);
|
|
589
|
+
if (!s) throw $e(n.length);
|
|
592
590
|
let c = [];
|
|
593
591
|
for (let t = 0; t < s.anchors; t += 1) {
|
|
594
592
|
let l = t * s.features, u = e[l + 4], d = 0, f = 0;
|
|
@@ -600,54 +598,54 @@ function Qe(e, t, n, r, i, a, o) {
|
|
|
600
598
|
!p || m < r || c.push({
|
|
601
599
|
field: p,
|
|
602
600
|
confidence: m,
|
|
603
|
-
bbox:
|
|
601
|
+
bbox: B(e[l], e[l + 1], e[l + 2], e[l + 3], i, a, o)
|
|
604
602
|
});
|
|
605
603
|
}
|
|
606
|
-
return
|
|
604
|
+
return z(c);
|
|
607
605
|
}
|
|
608
|
-
function
|
|
609
|
-
let r = n.classes ??
|
|
606
|
+
function nt(e, t, n) {
|
|
607
|
+
let r = n.classes ?? I, i = n.confidence ?? .35, a = n.inputSize ?? R, o = V(t, r.length);
|
|
610
608
|
if (n.layout === "yolov8" && o) throw Error("This tensor is YOLOX [1, anchors, 5+classes]. Pass fieldLayout: 'yolox', or omit fieldLayout to detect it from the shape.");
|
|
611
|
-
return (n.layout ?? (o ? "yolox" : "yolov8")) === "yolox" ?
|
|
612
|
-
}
|
|
613
|
-
async function
|
|
614
|
-
let
|
|
615
|
-
|
|
616
|
-
let
|
|
617
|
-
if (!
|
|
618
|
-
|
|
619
|
-
let
|
|
620
|
-
for (let e = 0; e <
|
|
621
|
-
let
|
|
622
|
-
|
|
609
|
+
return (n.layout ?? (o ? "yolox" : "yolov8")) === "yolox" ? tt(e, t, r, i, a, n.imageWidth, n.imageHeight) : et(e, t, r, i, a, n.imageWidth, n.imageHeight);
|
|
610
|
+
}
|
|
611
|
+
async function rt(t, n, r = I, i = .35, a) {
|
|
612
|
+
let o = await y(t, { cloneCanvas: !1 }), s = import("onnxruntime-web"), c = R, l = document.createElement("canvas");
|
|
613
|
+
l.width = c, l.height = c;
|
|
614
|
+
let u = l.getContext("2d", { willReadFrequently: !0 });
|
|
615
|
+
if (!u) throw d();
|
|
616
|
+
u.drawImage(o, 0, 0, c, c);
|
|
617
|
+
let f = u.getImageData(0, 0, c, c).data, p = c * c, m = new Float32Array(p * 3);
|
|
618
|
+
for (let e = 0; e < p; e += 1) m[e] = f[e * 4] / 255, m[p + e] = f[e * 4 + 1] / 255, m[p * 2 + e] = f[e * 4 + 2] / 255;
|
|
619
|
+
let h = F.get(n);
|
|
620
|
+
h || (h = (async () => {
|
|
623
621
|
try {
|
|
624
|
-
let [
|
|
625
|
-
return await
|
|
622
|
+
let [t, r] = await Promise.all([s, e(n)]);
|
|
623
|
+
return await t.InferenceSession.create(r, {
|
|
626
624
|
executionProviders: ["wasm"],
|
|
627
625
|
graphOptimizationLevel: "all"
|
|
628
626
|
});
|
|
629
627
|
} catch (e) {
|
|
630
|
-
throw
|
|
628
|
+
throw F.delete(n), e;
|
|
631
629
|
}
|
|
632
|
-
})(),
|
|
633
|
-
let [
|
|
630
|
+
})(), F.set(n, h));
|
|
631
|
+
let [g, _] = await Promise.all([s, h]), v = (await _.run({ [_.inputNames[0]]: new g.Tensor("float32", m, [
|
|
634
632
|
1,
|
|
635
633
|
3,
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
]) }))[
|
|
639
|
-
return
|
|
634
|
+
c,
|
|
635
|
+
c
|
|
636
|
+
]) }))[_.outputNames[0]];
|
|
637
|
+
return nt(v.data, v.dims.map(Number), {
|
|
640
638
|
classes: r,
|
|
641
639
|
confidence: i,
|
|
642
|
-
layout:
|
|
643
|
-
imageWidth:
|
|
644
|
-
imageHeight:
|
|
645
|
-
inputSize:
|
|
640
|
+
layout: a,
|
|
641
|
+
imageWidth: o.width,
|
|
642
|
+
imageHeight: o.height,
|
|
643
|
+
inputSize: c
|
|
646
644
|
});
|
|
647
645
|
}
|
|
648
646
|
//#endregion
|
|
649
647
|
//#region src/sdk/programmatic-redaction.ts
|
|
650
|
-
function
|
|
648
|
+
function H(e) {
|
|
651
649
|
let t = Math.min(...e.map((e) => e.bbox.x)), n = Math.min(...e.map((e) => e.bbox.y)), r = Math.max(...e.map((e) => e.bbox.x + e.bbox.width)), i = Math.max(...e.map((e) => e.bbox.y + e.bbox.height));
|
|
652
650
|
return {
|
|
653
651
|
x: t,
|
|
@@ -656,11 +654,11 @@ function V(e) {
|
|
|
656
654
|
height: i - n
|
|
657
655
|
};
|
|
658
656
|
}
|
|
659
|
-
function
|
|
657
|
+
function U(e) {
|
|
660
658
|
let t = [], n = [...e].sort((e, t) => e.bbox.y + e.bbox.height / 2 - (t.bbox.y + t.bbox.height / 2) || e.bbox.x - t.bbox.x);
|
|
661
659
|
for (let e of n) {
|
|
662
660
|
let n = e.bbox.y + e.bbox.height / 2, r = t.find((t) => {
|
|
663
|
-
let r =
|
|
661
|
+
let r = H(t), i = r.y + r.height / 2;
|
|
664
662
|
return Math.abs(n - i) <= Math.max(e.bbox.height, r.height) * .65;
|
|
665
663
|
});
|
|
666
664
|
r ? r.push(e) : t.push([e]);
|
|
@@ -668,17 +666,17 @@ function H(e) {
|
|
|
668
666
|
return t.map((e) => (e.sort((e, t) => e.bbox.x - t.bbox.x), {
|
|
669
667
|
words: e,
|
|
670
668
|
text: e.map((e) => e.text).join(" "),
|
|
671
|
-
bbox:
|
|
669
|
+
bbox: H(e)
|
|
672
670
|
}));
|
|
673
671
|
}
|
|
674
|
-
function
|
|
672
|
+
function W(e, t, n = !1) {
|
|
675
673
|
let r = e.trim().replace(/\s+/g, " "), i = n ? r.replace(/[::]\s*$/, "") : r;
|
|
676
674
|
return t ? i.toLocaleLowerCase() : i;
|
|
677
675
|
}
|
|
678
|
-
function
|
|
676
|
+
function G(e) {
|
|
679
677
|
return e.reduce((e, t) => e + t.confidence, 0) / Math.max(1, e.length);
|
|
680
678
|
}
|
|
681
|
-
function
|
|
679
|
+
function it(e, t) {
|
|
682
680
|
let n = e.mode, r = n === "case-insensitive", i = e.scope ?? "line", a;
|
|
683
681
|
if (n === "regex") {
|
|
684
682
|
let t = e.pattern ?? e.values?.[0];
|
|
@@ -689,14 +687,14 @@ function tt(e, t) {
|
|
|
689
687
|
ruleId: e.id,
|
|
690
688
|
strategy: "string",
|
|
691
689
|
source: "string",
|
|
692
|
-
bbox:
|
|
690
|
+
bbox: H(t),
|
|
693
691
|
text: t.map((e) => e.text).join(" "),
|
|
694
|
-
confidence:
|
|
692
|
+
confidence: G(t)
|
|
695
693
|
});
|
|
696
694
|
if (i === "word") {
|
|
697
695
|
for (let e of t) for (let t of e.words) {
|
|
698
|
-
let e =
|
|
699
|
-
(a ? a.test(t.text) : o.some((t) => e ===
|
|
696
|
+
let e = W(t.text, r);
|
|
697
|
+
(a ? a.test(t.text) : o.some((t) => e === W(t, r))) && c([t]);
|
|
700
698
|
}
|
|
701
699
|
return s;
|
|
702
700
|
}
|
|
@@ -706,40 +704,40 @@ function tt(e, t) {
|
|
|
706
704
|
continue;
|
|
707
705
|
}
|
|
708
706
|
for (let t of o) {
|
|
709
|
-
let n =
|
|
707
|
+
let n = W(t, r).split(" ");
|
|
710
708
|
for (let t = 0; t <= e.words.length - n.length; t += 1) {
|
|
711
709
|
let i = e.words.slice(t, t + n.length);
|
|
712
|
-
i.every((e, t) =>
|
|
710
|
+
i.every((e, t) => W(e.text, r) === n[t]) && c(i);
|
|
713
711
|
}
|
|
714
712
|
}
|
|
715
713
|
}
|
|
716
714
|
return s;
|
|
717
715
|
}
|
|
718
|
-
function
|
|
719
|
-
let n = e.labelMode !== "exact", r =
|
|
716
|
+
function at(e, t) {
|
|
717
|
+
let n = e.labelMode !== "exact", r = W(e.label, n, !0).split(" ");
|
|
720
718
|
for (let e of t) for (let t = 0; t <= e.words.length - r.length; t += 1) {
|
|
721
719
|
let i = e.words.slice(t, t + r.length);
|
|
722
|
-
if (i.every((e, t) =>
|
|
720
|
+
if (i.every((e, t) => W(e.text, n, !0) === r[t])) return i;
|
|
723
721
|
}
|
|
724
722
|
}
|
|
725
|
-
function
|
|
723
|
+
function ot(e, t, n, r, i, a) {
|
|
726
724
|
let o = a.window?.distance ?? .28, s = a.window?.crossAxis ?? .12, c = t.x + t.width / 2, l = t.y + t.height / 2;
|
|
727
725
|
return e.filter((e) => {
|
|
728
726
|
let a = e.bbox.x + e.bbox.width / 2, u = e.bbox.y + e.bbox.height / 2;
|
|
729
727
|
return n === "right" ? e.bbox.x >= t.x + t.width && e.bbox.x - t.x - t.width <= r * o && Math.abs(u - l) <= i * s : n === "left" ? e.bbox.x + e.bbox.width <= t.x && t.x - e.bbox.x - e.bbox.width <= r * o && Math.abs(u - l) <= i * s : n === "below" ? e.bbox.y >= t.y + t.height && e.bbox.y - t.y - t.height <= i * o && Math.abs(a - c) <= r * s : e.bbox.y + e.bbox.height <= t.y && t.y - e.bbox.y - e.bbox.height <= i * o && Math.abs(a - c) <= r * s;
|
|
730
728
|
});
|
|
731
729
|
}
|
|
732
|
-
function
|
|
733
|
-
let o =
|
|
730
|
+
function K(e, t, n, r, i, a = "label") {
|
|
731
|
+
let o = at(e, n);
|
|
734
732
|
if (!o) return [];
|
|
735
|
-
let s =
|
|
733
|
+
let s = H(o), c = new Set(o), l = t.filter((e) => !c.has(e)), u = e.direction === "any" ? [
|
|
736
734
|
"right",
|
|
737
735
|
"below",
|
|
738
736
|
"left",
|
|
739
737
|
"above"
|
|
740
738
|
] : [e.direction], d;
|
|
741
739
|
for (let t of u) {
|
|
742
|
-
let n =
|
|
740
|
+
let n = U(ot(l, s, t, r, i, e));
|
|
743
741
|
for (let t of n) {
|
|
744
742
|
if (e.valuePattern && !new RegExp(e.valuePattern, e.patternFlags).test(t.text)) continue;
|
|
745
743
|
let n = t.bbox.x + t.bbox.width / 2, a = t.bbox.y + t.bbox.height / 2, o = s.x + s.width / 2, c = s.y + s.height / 2, l = Math.hypot((n - o) / r, (a - c) / i);
|
|
@@ -755,12 +753,12 @@ function G(e, t, n, r, i, a = "label") {
|
|
|
755
753
|
ruleId: e.id,
|
|
756
754
|
strategy: a,
|
|
757
755
|
source: a === "template-anchor" ? "template" : "label",
|
|
758
|
-
bbox:
|
|
756
|
+
bbox: H(f),
|
|
759
757
|
text: d.words.map((e) => e.text).join(" "),
|
|
760
|
-
confidence: Math.max(0,
|
|
758
|
+
confidence: Math.max(0, G(d.words) * (1 - Math.min(.5, d.distance)))
|
|
761
759
|
}];
|
|
762
760
|
}
|
|
763
|
-
var
|
|
761
|
+
var q = {
|
|
764
762
|
person_name: [
|
|
765
763
|
"LN",
|
|
766
764
|
"FN",
|
|
@@ -797,32 +795,32 @@ var K = {
|
|
|
797
795
|
"POSTAL"
|
|
798
796
|
],
|
|
799
797
|
state: ["STATE", "DAJ"]
|
|
800
|
-
},
|
|
801
|
-
function
|
|
798
|
+
}, st = new Set(Object.values(q).flat()), J = /^(?:\d{2}[/-]\d{2}[/-]\d{2,4}|\d{4}[/-]\d{2}[/-]\d{2}|\d{6}|\d{8})$/, Y = /\b(?:ST|STREET|AVE|AVENUE|RD|ROAD|BLVD|BOULEVARD|LN|LANE|DR|DRIVE|WAY|CT|COURT)\b/i, X = /\b(?:DRIVER|LICENSE|IDENTIFICATION|CARD|CLASS|SEX|EYES|HEIGHT|RESTRICTIONS)\b/i;
|
|
799
|
+
function Z(e, t) {
|
|
802
800
|
let n = t.trim();
|
|
803
|
-
return n ? e === "date_of_birth" || e === "expiration_date" ?
|
|
801
|
+
return n ? e === "date_of_birth" || e === "expiration_date" ? J.test(n.replace(/\s/g, "")) : e === "postal_code" ? /^(?:\d{5}(?:-\d{4})?|[A-Z]\d[A-Z]\s?\d[A-Z]\d)$/i.test(n) : e === "state" ? /^[A-Z]{2}$/i.test(n) : e === "document_number" ? /^(?=.*\d)[A-Z0-9-]{5,20}$/i.test(n) : e === "person_name" ? !/\d/.test(n) && /^[A-ZÀ-ÖØ-öø-ÿ' -]{3,}$/i.test(n) && !X.test(n) : /\d/.test(n) && /[A-Z]/i.test(n) : !1;
|
|
804
802
|
}
|
|
805
|
-
function
|
|
803
|
+
function Q(e, t, n, r, i = 1) {
|
|
806
804
|
return {
|
|
807
805
|
ruleId: e,
|
|
808
806
|
strategy: "semantic",
|
|
809
807
|
source: t,
|
|
810
|
-
bbox:
|
|
808
|
+
bbox: H(n),
|
|
811
809
|
text: r ?? n.map((e) => e.text).join(" "),
|
|
812
|
-
confidence: Math.max(0, Math.min(1,
|
|
810
|
+
confidence: Math.max(0, Math.min(1, G(n) * i))
|
|
813
811
|
};
|
|
814
812
|
}
|
|
815
|
-
function
|
|
816
|
-
let r =
|
|
813
|
+
function ct(e, t, n) {
|
|
814
|
+
let r = q[e], i = [];
|
|
817
815
|
for (let a of n) for (let n = 0; n < a.words.length; n += 1) {
|
|
818
816
|
let o = a.words[n].text.toUpperCase().replace(/[^A-Z0-9]/g, ""), s = r.find((e) => o === e || o.startsWith(e));
|
|
819
817
|
if (!s) continue;
|
|
820
818
|
let c, l;
|
|
821
|
-
o === s ? (c = a.words.slice(n + 1).filter((e) => !
|
|
819
|
+
o === s ? (c = a.words.slice(n + 1).filter((e) => !st.has(e.text.toUpperCase().replace(/[^A-Z0-9]/g, ""))), l = c.map((e) => e.text).join(" ")) : (c = [a.words[n]], l = a.words[n].text.replace(RegExp(`^${s}[:\\s-]*`, "i"), "")), c.length && Z(e, l) && i.push(Q(t, "alias", c, l, .98));
|
|
822
820
|
}
|
|
823
821
|
return i;
|
|
824
822
|
}
|
|
825
|
-
function
|
|
823
|
+
function lt(e, t, n) {
|
|
826
824
|
let r = n.filter((e) => {
|
|
827
825
|
let t = e.text.replace(/\s/g, "");
|
|
828
826
|
return t.includes("<<") || /^[A-Z0-9<]{25,}$/.test(t);
|
|
@@ -832,19 +830,19 @@ function Q(e, t, n) {
|
|
|
832
830
|
let e = r.find((e) => e.text.includes("<<"));
|
|
833
831
|
if (!e) return [];
|
|
834
832
|
let n = e.text.replace(/\s/g, ""), i = /^[PIV]<[A-Z]{3}/.test(n) ? 5 : n.indexOf("<") + 1, a = n.slice(i).replace(/<+/g, " ").trim();
|
|
835
|
-
return a ? [
|
|
833
|
+
return a ? [Q(t, "mrz", e.words, a, .96)] : [];
|
|
836
834
|
}
|
|
837
835
|
if (e === "date_of_birth") for (let e of r) {
|
|
838
836
|
let n = e.text.replace(/\s/g, ""), r = /^\d{6}/.exec(n)?.[0] ?? /^.{13}(\d{6})/.exec(n)?.[1];
|
|
839
|
-
if (r) return [
|
|
837
|
+
if (r) return [Q(t, "mrz", e.words, r, .96)];
|
|
840
838
|
}
|
|
841
839
|
if (e === "document_number") {
|
|
842
840
|
let e = r[0];
|
|
843
|
-
return [
|
|
841
|
+
return [Q(t, "mrz", e.words, e.text, .9)];
|
|
844
842
|
}
|
|
845
843
|
return [];
|
|
846
844
|
}
|
|
847
|
-
function
|
|
845
|
+
function ut(e) {
|
|
848
846
|
let t = e.replace(/\s/g, ""), n = t.match(/\d{4}/)?.[0];
|
|
849
847
|
if (n) return Number(n);
|
|
850
848
|
let r = t.match(/^\d{2}/)?.[0];
|
|
@@ -852,67 +850,67 @@ function ot(e) {
|
|
|
852
850
|
let i = Number(r);
|
|
853
851
|
return i > (/* @__PURE__ */ new Date()).getFullYear() % 100 ? 1900 + i : 2e3 + i;
|
|
854
852
|
}
|
|
855
|
-
function
|
|
853
|
+
function dt(e, t, n, r, i) {
|
|
856
854
|
if (e === "address") {
|
|
857
|
-
let e = r.findIndex((e) => /\d/.test(e.text) &&
|
|
855
|
+
let e = r.findIndex((e) => /\d/.test(e.text) && Y.test(e.text));
|
|
858
856
|
if (e < 0) return [];
|
|
859
857
|
let n = [...r[e].words], i = r[e + 1];
|
|
860
|
-
return i && /(?:\b[A-Z]{2}\b.*\d{5}|\d{5})/i.test(i.text) && n.push(...i.words), [
|
|
858
|
+
return i && /(?:\b[A-Z]{2}\b.*\d{5}|\d{5})/i.test(i.text) && n.push(...i.words), [Q(t, "layout", n, void 0, .76)];
|
|
861
859
|
}
|
|
862
860
|
if (e === "person_name") {
|
|
863
861
|
let e = r.map((e) => {
|
|
864
|
-
let t = e.words.filter((e) => /^[A-ZÀ-ÖØ-öø-ÿ'-]+$/i.test(e.text)), n = e.bbox.y / Math.max(1, i), r = t.length >= 2 && t.length <= 5 && t.length === e.words.length && !
|
|
862
|
+
let t = e.words.filter((e) => /^[A-ZÀ-ÖØ-öø-ÿ'-]+$/i.test(e.text)), n = e.bbox.y / Math.max(1, i), r = t.length >= 2 && t.length <= 5 && t.length === e.words.length && !X.test(e.text) && !Y.test(e.text) && !e.text.includes("<"), a = e.text.replace(/[^A-Z]/g, "").length / Math.max(1, e.text.replace(/[^A-Za-z]/g, "").length);
|
|
865
863
|
return {
|
|
866
864
|
line: e,
|
|
867
|
-
score: r ? .45 +
|
|
865
|
+
score: r ? .45 + G(e.words) * .3 + a * .15 + (n < .65 ? .1 : 0) : 0
|
|
868
866
|
};
|
|
869
867
|
}).filter((e) => e.score >= .68).sort((e, t) => t.score - e.score);
|
|
870
|
-
return !e.length || e[1] && e[0].score - e[1].score < .04 ? [] : [
|
|
868
|
+
return !e.length || e[1] && e[0].score - e[1].score < .04 ? [] : [Q(t, "layout", e[0].line.words, void 0, e[0].score)];
|
|
871
869
|
}
|
|
872
870
|
if (e === "date_of_birth" || e === "expiration_date") {
|
|
873
|
-
let r = (/* @__PURE__ */ new Date()).getFullYear(), i = n.filter((e) =>
|
|
871
|
+
let r = (/* @__PURE__ */ new Date()).getFullYear(), i = n.filter((e) => J.test(e.text.replace(/\s/g, ""))).map((e) => ({
|
|
874
872
|
word: e,
|
|
875
|
-
year:
|
|
873
|
+
year: ut(e.text)
|
|
876
874
|
})).filter((e) => e.year !== void 0), a = e === "date_of_birth" ? i.filter((e) => e.year <= r - 14 && e.year >= r - 120) : i.filter((e) => e.year >= r - 1);
|
|
877
|
-
return a.sort((t, n) => e === "date_of_birth" ? t.year - n.year : n.year - t.year), a.length ? [
|
|
875
|
+
return a.sort((t, n) => e === "date_of_birth" ? t.year - n.year : n.year - t.year), a.length ? [Q(t, "layout", [a[0].word], void 0, .72)] : [];
|
|
878
876
|
}
|
|
879
877
|
if (e === "postal_code") {
|
|
880
|
-
let r = n.find((t) =>
|
|
881
|
-
return r ? [
|
|
878
|
+
let r = n.find((t) => Z(e, t.text));
|
|
879
|
+
return r ? [Q(t, "layout", [r], void 0, .78)] : [];
|
|
882
880
|
}
|
|
883
881
|
if (e === "state") {
|
|
884
|
-
let r = n.filter((t) =>
|
|
885
|
-
return r.length === 1 ? [
|
|
882
|
+
let r = n.filter((t) => Z(e, t.text));
|
|
883
|
+
return r.length === 1 ? [Q(t, "layout", r, void 0, .7)] : [];
|
|
886
884
|
}
|
|
887
885
|
if (e === "document_number") {
|
|
888
|
-
let r = n.filter((t) =>
|
|
889
|
-
return r.sort((e, t) => t.text.length - e.text.length), r.length ? [
|
|
886
|
+
let r = n.filter((t) => Z(e, t.text));
|
|
887
|
+
return r.sort((e, t) => t.text.length - e.text.length), r.length ? [Q(t, "layout", [r[0]], void 0, .7)] : [];
|
|
890
888
|
}
|
|
891
889
|
return [];
|
|
892
890
|
}
|
|
893
|
-
function
|
|
891
|
+
function ft(e, t) {
|
|
894
892
|
return Math.max(0, Math.min(e.x + e.width, t.x + t.width) - Math.max(e.x, t.x)) * Math.max(0, Math.min(e.y + e.height, t.y + t.height) - Math.max(e.y, t.y)) / Math.max(1, t.width * t.height);
|
|
895
893
|
}
|
|
896
|
-
function
|
|
897
|
-
let i =
|
|
898
|
-
return i.length || a.length ? [...i, ...a] :
|
|
894
|
+
function pt(e, t, n, r) {
|
|
895
|
+
let i = ct(e.field, e.id, n), a = lt(e.field, e.id, n);
|
|
896
|
+
return i.length || a.length ? [...i, ...a] : dt(e.field, e.id, t, n, r);
|
|
899
897
|
}
|
|
900
|
-
function
|
|
898
|
+
function mt(e, t, n) {
|
|
901
899
|
let r = (n ?? []).filter((t) => t.field === e.field).sort((e, t) => t.confidence - e.confidence);
|
|
902
900
|
for (let n of r) {
|
|
903
|
-
let r = t.filter((e) =>
|
|
901
|
+
let r = t.filter((e) => ft(n.bbox, e.bbox) >= .3);
|
|
904
902
|
if (!r.length) continue;
|
|
905
903
|
let i = r.map((e) => e.text).join(" ");
|
|
906
|
-
if (
|
|
904
|
+
if (Z(e.field, i)) return Q(e.id, "model", r, i, n.confidence);
|
|
907
905
|
}
|
|
908
906
|
}
|
|
909
|
-
function
|
|
910
|
-
let o =
|
|
907
|
+
function ht(e, t, n, r, i, a) {
|
|
908
|
+
let o = pt(e, t, n, r);
|
|
911
909
|
if (e.detector !== "model" || !a) return o;
|
|
912
|
-
let s =
|
|
910
|
+
let s = mt(e, t, i);
|
|
913
911
|
return s ? [s] : [];
|
|
914
912
|
}
|
|
915
|
-
function
|
|
913
|
+
function gt(e, t, n, r) {
|
|
916
914
|
let i = (e) => Math.max(0, Math.min(1, e)), a = i(e.rect.x), o = i(e.rect.y);
|
|
917
915
|
return {
|
|
918
916
|
ruleId: `${t}.${e.id}`,
|
|
@@ -928,18 +926,18 @@ function ft(e, t, n, r) {
|
|
|
928
926
|
};
|
|
929
927
|
}
|
|
930
928
|
function $(e, t) {
|
|
931
|
-
let n = t.imageWidth ?? e?.imageWidth ?? 0, r = t.imageHeight ?? e?.imageHeight ?? 0, i = e?.words ?? [], a =
|
|
929
|
+
let n = t.imageWidth ?? e?.imageWidth ?? 0, r = t.imageHeight ?? e?.imageHeight ?? 0, i = e?.words ?? [], a = U(i), o = [], s = [];
|
|
932
930
|
for (let e of t.rules) {
|
|
933
|
-
let c = e.strategy === "string" ?
|
|
931
|
+
let c = e.strategy === "string" ? it(e, a) : e.strategy === "label" ? K(e, i, a, n, r) : ht(e, i, a, r, t.semanticDetections, t.modelRequested === !0);
|
|
934
932
|
c.length ? o.push(...c) : s.push(e.id);
|
|
935
933
|
}
|
|
936
934
|
for (let e of t.template?.fields ?? []) {
|
|
937
935
|
let c = `${t.template.id}.${e.id}`;
|
|
938
936
|
if (e.strategy === "rect") {
|
|
939
|
-
n && r ? o.push(
|
|
937
|
+
n && r ? o.push(gt(e, t.template.id, n, r)) : s.push(c);
|
|
940
938
|
continue;
|
|
941
939
|
}
|
|
942
|
-
let l =
|
|
940
|
+
let l = K({
|
|
943
941
|
...e,
|
|
944
942
|
id: c,
|
|
945
943
|
strategy: "label"
|
|
@@ -957,68 +955,68 @@ function $(e, t) {
|
|
|
957
955
|
confidence: u
|
|
958
956
|
};
|
|
959
957
|
}
|
|
960
|
-
function
|
|
958
|
+
function _t(e) {
|
|
961
959
|
return e.rules.length > 0 || e.template?.fields.some((e) => e.strategy === "anchor") === !0;
|
|
962
960
|
}
|
|
963
|
-
function
|
|
961
|
+
function vt(e, t = !1) {
|
|
964
962
|
return t && !e.complete ? "unmatched" : e.complete ? "success" : "partial";
|
|
965
963
|
}
|
|
966
|
-
async function
|
|
967
|
-
await
|
|
968
|
-
let
|
|
969
|
-
|
|
970
|
-
let
|
|
964
|
+
async function yt(e, t) {
|
|
965
|
+
await u("redaction");
|
|
966
|
+
let n = await y(e, { cloneCanvas: !1 }), r = t.ocr;
|
|
967
|
+
_t(t) && !r && (r = await M(n, { includeWords: !0 }));
|
|
968
|
+
let i = t.semanticDetections, a = t.modelRequested === !0, o = "not-requested", s;
|
|
971
969
|
if (t.models?.fields) try {
|
|
972
|
-
|
|
970
|
+
i = await rt(n, t.models.fields, t.models.fieldClasses, t.models.confidence, t.models.fieldLayout), a = !0, o = "loaded";
|
|
973
971
|
} catch (e) {
|
|
974
|
-
|
|
972
|
+
s = e instanceof Error ? e.message : "The field model could not load.";
|
|
975
973
|
let n = t.models.allowHeuristicFallback !== !1;
|
|
976
|
-
|
|
974
|
+
a = !n, o = n ? "fallback" : "failed";
|
|
977
975
|
}
|
|
978
|
-
let
|
|
976
|
+
let c = $(r, {
|
|
979
977
|
rules: t.rules,
|
|
980
978
|
template: t.template,
|
|
981
|
-
imageWidth:
|
|
982
|
-
imageHeight:
|
|
983
|
-
semanticDetections:
|
|
984
|
-
modelRequested:
|
|
985
|
-
}),
|
|
979
|
+
imageWidth: n.width,
|
|
980
|
+
imageHeight: n.height,
|
|
981
|
+
semanticDetections: i,
|
|
982
|
+
modelRequested: a
|
|
983
|
+
}), l = vt(c, t.requireAll), d = [], f = "not-requested", p;
|
|
986
984
|
if (t.models?.face) try {
|
|
987
|
-
|
|
985
|
+
d = await Ye(n, t.models.face, t.models.faceConfidence), f = "loaded";
|
|
988
986
|
} catch (e) {
|
|
989
|
-
|
|
990
|
-
}
|
|
991
|
-
if (
|
|
992
|
-
...
|
|
993
|
-
status:
|
|
994
|
-
ocr:
|
|
995
|
-
modelStatus:
|
|
996
|
-
modelError:
|
|
997
|
-
faces:
|
|
998
|
-
faceStatus:
|
|
999
|
-
faceError:
|
|
987
|
+
p = e instanceof Error ? e.message : "The face model could not load.", f = "failed";
|
|
988
|
+
}
|
|
989
|
+
if (l === "unmatched") return {
|
|
990
|
+
...c,
|
|
991
|
+
status: l,
|
|
992
|
+
ocr: r,
|
|
993
|
+
modelStatus: o,
|
|
994
|
+
modelError: s,
|
|
995
|
+
faces: d,
|
|
996
|
+
faceStatus: f,
|
|
997
|
+
faceError: p
|
|
1000
998
|
};
|
|
1001
|
-
let
|
|
999
|
+
let m = await N(n, [...c.matches.map((e) => ({
|
|
1002
1000
|
...e.bbox,
|
|
1003
1001
|
mode: t.style?.mode
|
|
1004
|
-
})), ...
|
|
1002
|
+
})), ...d.map((e) => ({
|
|
1005
1003
|
...e.bbox,
|
|
1006
1004
|
mode: t.style?.mode
|
|
1007
1005
|
}))], t.style);
|
|
1008
1006
|
return {
|
|
1009
|
-
...
|
|
1010
|
-
status:
|
|
1011
|
-
ocr:
|
|
1012
|
-
canvas:
|
|
1013
|
-
blob:
|
|
1014
|
-
modelStatus:
|
|
1015
|
-
modelError:
|
|
1016
|
-
faces:
|
|
1017
|
-
faceStatus:
|
|
1018
|
-
faceError:
|
|
1007
|
+
...c,
|
|
1008
|
+
status: l,
|
|
1009
|
+
ocr: r,
|
|
1010
|
+
canvas: m.canvas,
|
|
1011
|
+
blob: m.blob,
|
|
1012
|
+
modelStatus: o,
|
|
1013
|
+
modelError: s,
|
|
1014
|
+
faces: d,
|
|
1015
|
+
faceStatus: f,
|
|
1016
|
+
faceError: p
|
|
1019
1017
|
};
|
|
1020
1018
|
}
|
|
1021
1019
|
//#endregion
|
|
1022
|
-
export {
|
|
1020
|
+
export { v as AutoTorchController, f as DEFAULT_DOCUMENT_MODEL_URL, s as DEV_LICENSE_HOSTS, c as LicenseError, ee as NEAR_BLACK_LUMINANCE, fe as QuadStabilityTracker, le as STILL_MAX_EDGE, o as ScannerError, N as applyRedactions, u as assertLicense, se as autoCaptureControl, oe as captureVideoFrame, be as createFinderSimulator, t as cropDocument, ne as detectBrowserEngine, he as detectDocument, te as formatCaptureDiagnosticsReport, i as getLicense, m as isNearBlackFrame, ue as limitingFillTarget, ve as mapDocumentCornersToFrame, $ as matchRedactionRules, me as meanFrameLuminance, re as measureQuadFill, _e as mobileCameraConstraints, M as recognizeText, yt as redactProgrammatically, p as resolveDocumentModelUrl, r as scanDocument, h as selectFinderGuidance, ce as selectFinderHint, a as setLicenseKey, ye as simulatorQuad, g as startCamera, de as startFinder, ie as subscribeModelDownloads, ge as torchControl };
|
|
1023
1021
|
|
|
1024
1022
|
//# sourceMappingURL=scanner-sdk.js.map
|