@exige/web-scanner-sdk 0.2.9
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 +330 -0
- package/dist/assets/document-worker.js +2 -0
- package/dist/assets/document-worker.js.map +1 -0
- package/dist/assets/ort-wasm-simd-threaded.jsep.mjs +108 -0
- package/dist/assets/ort-wasm-simd-threaded.jsep.wasm +0 -0
- package/dist/assets/ort.min.js +2937 -0
- package/dist/assets/ort.min.js.map +1 -0
- package/dist/assets/tesseract/eng.traineddata.LICENSE +201 -0
- package/dist/assets/tesseract/eng.traineddata.NOTICE +9 -0
- package/dist/assets/tesseract/eng.traineddata.gz +0 -0
- package/dist/assets/tesseract/tesseract-core-lstm.wasm.js +191 -0
- package/dist/assets/tesseract/tesseract-core-relaxedsimd-lstm.wasm.js +191 -0
- package/dist/assets/tesseract/tesseract-core-simd-lstm.wasm.js +191 -0
- package/dist/assets/tesseract/tesseract.js-core.LICENSE +202 -0
- package/dist/assets/tesseract/tesseract.js.LICENSE +201 -0
- package/dist/assets/tesseract/worker.min.js +3 -0
- package/dist/assets/tesseract/worker.min.js.LICENSE.txt +12 -0
- package/dist/assets-manifest.json +11 -0
- package/dist/components/ModelDownloadRing.d.ts +4 -0
- package/dist/components/scanner-camera-overlay.css +2 -0
- package/dist/finder-DFueuWuS.js +1225 -0
- package/dist/finder-DFueuWuS.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/live-overlay-geometry.d.ts +11 -0
- package/dist/react/context.d.ts +2 -0
- package/dist/react/hooks.d.ts +3 -0
- package/dist/react/parts.d.ts +16 -0
- package/dist/react/scanner-context.d.ts +5 -0
- package/dist/react/types.d.ts +113 -0
- package/dist/react.d.ts +4 -0
- package/dist/react.js +594 -0
- package/dist/react.js.map +1 -0
- package/dist/scanner-sdk.js +1001 -0
- package/dist/scanner-sdk.js.map +1 -0
- package/dist/sdk/camera.d.ts +150 -0
- package/dist/sdk/display-quad.d.ts +23 -0
- package/dist/sdk/errors.d.ts +14 -0
- package/dist/sdk/face-model.d.ts +22 -0
- package/dist/sdk/field-model.d.ts +29 -0
- package/dist/sdk/finder-simulator.d.ts +17 -0
- package/dist/sdk/finder.d.ts +174 -0
- package/dist/sdk/frame-geometry.d.ts +6 -0
- package/dist/sdk/image.d.ts +18 -0
- package/dist/sdk/license-codec.d.ts +2 -0
- package/dist/sdk/license-public-key.d.ts +9 -0
- package/dist/sdk/license-runtime.d.ts +2 -0
- package/dist/sdk/license.d.ts +84 -0
- package/dist/sdk/lighting.d.ts +22 -0
- package/dist/sdk/model-download.d.ts +25 -0
- package/dist/sdk/ocr-runtime.d.ts +27 -0
- package/dist/sdk/ocr.d.ts +104 -0
- package/dist/sdk/ort-runtime.d.ts +23 -0
- package/dist/sdk/programmatic-redaction.d.ts +151 -0
- package/dist/sdk/redact.d.ts +17 -0
- package/dist/sdk/scan.d.ts +61 -0
- package/dist/sdk/types.d.ts +80 -0
- package/package.json +93 -0
|
@@ -0,0 +1,1001 @@
|
|
|
1
|
+
import { A as e, C as t, D as n, E as r, F as i, M as a, N as o, O as s, P as c, S as l, T as u, _ as d, a as f, b as p, c as m, d as h, f as g, g as _, h as v, i as y, j as b, k as ee, l as te, m as ne, n as re, o as ie, p as ae, r as oe, s as se, t as ce, u as le, v as ue, w as de, x as fe, y as pe } from "./finder-DFueuWuS.js";
|
|
2
|
+
//#region src/sdk/frame-geometry.ts
|
|
3
|
+
function x(e, t, n) {
|
|
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;
|
|
5
|
+
return {
|
|
6
|
+
x: (e.x - c) / s,
|
|
7
|
+
y: (e.y - l) / s
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function me(e, t, n) {
|
|
11
|
+
return {
|
|
12
|
+
topLeft: x(e.topLeft, t, n),
|
|
13
|
+
topRight: x(e.topRight, t, n),
|
|
14
|
+
bottomRight: x(e.bottomRight, t, n),
|
|
15
|
+
bottomLeft: x(e.bottomLeft, t, n)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/sdk/finder-simulator.ts
|
|
20
|
+
function S(e, t = 960, n = 600) {
|
|
21
|
+
let r = t * .86, i = n * .86;
|
|
22
|
+
if (e < 1200) {
|
|
23
|
+
let a = e / 1200;
|
|
24
|
+
return {
|
|
25
|
+
centerX: -r / 2 + (t / 2 + r / 2) * (1 - (1 - a) ** 3),
|
|
26
|
+
centerY: n / 2,
|
|
27
|
+
rotation: -.14 + a * .08,
|
|
28
|
+
width: r,
|
|
29
|
+
height: i,
|
|
30
|
+
phase: "entering"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (e < 3600) {
|
|
34
|
+
let a = (e - 1200) / 2400, o = (1 - a) * 18;
|
|
35
|
+
return {
|
|
36
|
+
centerX: t / 2 + Math.sin(e / 85) * o,
|
|
37
|
+
centerY: n / 2 + Math.cos(e / 110) * o * .55,
|
|
38
|
+
rotation: -.055 + Math.sin(e / 125) * (1 - a) * .045,
|
|
39
|
+
width: r,
|
|
40
|
+
height: i,
|
|
41
|
+
phase: "jittering"
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
centerX: t / 2,
|
|
46
|
+
centerY: n / 2,
|
|
47
|
+
rotation: 0,
|
|
48
|
+
width: r,
|
|
49
|
+
height: i,
|
|
50
|
+
phase: "settled"
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function C(e, t, n) {
|
|
54
|
+
let r = Math.cos(n.rotation), i = Math.sin(n.rotation);
|
|
55
|
+
return {
|
|
56
|
+
x: n.centerX + e * r - t * i,
|
|
57
|
+
y: n.centerY + e * i + t * r
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function he(e) {
|
|
61
|
+
let t = C(-e.width / 2, -e.height / 2, e), n = C(e.width / 2, -e.height / 2, e), r = C(e.width / 2, e.height / 2, e), i = C(-e.width / 2, e.height / 2, e);
|
|
62
|
+
return {
|
|
63
|
+
topLeft: t,
|
|
64
|
+
topRight: n,
|
|
65
|
+
bottomRight: r,
|
|
66
|
+
bottomLeft: i,
|
|
67
|
+
edges: {
|
|
68
|
+
top: {
|
|
69
|
+
start: t,
|
|
70
|
+
end: n
|
|
71
|
+
},
|
|
72
|
+
right: {
|
|
73
|
+
start: n,
|
|
74
|
+
end: r
|
|
75
|
+
},
|
|
76
|
+
bottom: {
|
|
77
|
+
start: r,
|
|
78
|
+
end: i
|
|
79
|
+
},
|
|
80
|
+
left: {
|
|
81
|
+
start: i,
|
|
82
|
+
end: t
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function w(e, t) {
|
|
88
|
+
let n = e.getContext("2d");
|
|
89
|
+
if (!n) throw i();
|
|
90
|
+
let r = n.createLinearGradient(0, 0, e.width, e.height);
|
|
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 a = -t.width / 2, o = -t.height / 2;
|
|
93
|
+
n.fillStyle = "#d7ded8", n.fillRect(a + 38, o + 84, 145, 190), n.fillStyle = "#617269", n.beginPath(), n.arc(a + 110, o + 140, 38, 0, Math.PI * 2), n.fill(), n.fillRect(a + 70, o + 185, 80, 65), n.fillStyle = "#17231c", n.font = "700 25px Arial", n.fillText("IDENTITY CARD", a + 38, o + 48), n.font = "700 31px Arial", n.fillText("TAYLOR MORGAN", a + 220, o + 118), n.font = "23px Arial", n.fillText("01/02/1990", a + 220, o + 166), n.fillText("A1234567", a + 220, o + 208), n.fillText("123 MAIN ST", a + 220, o + 270), n.fillText("AUSTIN TX 78701", a + 220, o + 307), n.restore();
|
|
94
|
+
}
|
|
95
|
+
function ge(e) {
|
|
96
|
+
e.width ||= 960, e.height ||= 600;
|
|
97
|
+
let t = !1, n = 0, r = 0, i = (a) => {
|
|
98
|
+
t && (n ||= a, w(e, S(a - n, e.width, e.height)), r = requestAnimationFrame(i));
|
|
99
|
+
}, a = () => {
|
|
100
|
+
n = 0, w(e, S(0, e.width, e.height));
|
|
101
|
+
};
|
|
102
|
+
return a(), {
|
|
103
|
+
start() {
|
|
104
|
+
t || (t = !0, r = requestAnimationFrame(i));
|
|
105
|
+
},
|
|
106
|
+
stop() {
|
|
107
|
+
t = !1, cancelAnimationFrame(r);
|
|
108
|
+
},
|
|
109
|
+
reset: a
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/sdk/ocr-runtime.ts
|
|
114
|
+
var _e = new URL("./assets/tesseract/worker.min.js", import.meta.url), ve = new URL("./assets/tesseract/tesseract-core-lstm.wasm.js", import.meta.url), ye = new URL("./assets/tesseract/tesseract-core-simd-lstm.wasm.js", import.meta.url), be = new URL("./assets/tesseract/tesseract-core-relaxedsimd-lstm.wasm.js", import.meta.url), xe = new URL("./assets/tesseract/eng.traineddata.gz", import.meta.url), Se = 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), Ce = 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
|
+
function T(e) {
|
|
116
|
+
try {
|
|
117
|
+
return typeof WebAssembly < "u" && WebAssembly.validate(e);
|
|
118
|
+
} catch {
|
|
119
|
+
return !1;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function we() {
|
|
123
|
+
return {
|
|
124
|
+
relaxedSimd: T(Se),
|
|
125
|
+
simd: T(Ce)
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function Te(e) {
|
|
129
|
+
return e.relaxedSimd ? be : e.simd ? ye : ve;
|
|
130
|
+
}
|
|
131
|
+
function Ee(e, t) {
|
|
132
|
+
let n = e?.trim() || "eng", r = t?.trim() || "";
|
|
133
|
+
if (r) return {
|
|
134
|
+
kind: "lang-path",
|
|
135
|
+
language: n,
|
|
136
|
+
langPath: r
|
|
137
|
+
};
|
|
138
|
+
if (n === "eng") return {
|
|
139
|
+
kind: "bundled-eng",
|
|
140
|
+
language: "eng"
|
|
141
|
+
};
|
|
142
|
+
throw new c("language", `OCR language "${n}" is not packaged. Pass langPath, a directory URL that contains ${n}.traineddata.gz.`);
|
|
143
|
+
}
|
|
144
|
+
async function De() {
|
|
145
|
+
let e = await fetch(xe);
|
|
146
|
+
if (!e.ok) throw new c("model-http", `Packaged English OCR data failed to load (HTTP ${e.status}).`);
|
|
147
|
+
return new Uint8Array(await e.arrayBuffer());
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/sdk/ocr.ts
|
|
151
|
+
function E(e, t = 1, n = 1, r = 0, i = 0) {
|
|
152
|
+
return {
|
|
153
|
+
text: e.text,
|
|
154
|
+
confidence: Math.max(0, Math.min(1, e.confidence / 100)),
|
|
155
|
+
bbox: {
|
|
156
|
+
x: e.bbox.x0 * t + r,
|
|
157
|
+
y: e.bbox.y0 * n + i,
|
|
158
|
+
width: Math.max(0, e.bbox.x1 - e.bbox.x0) * t,
|
|
159
|
+
height: Math.max(0, e.bbox.y1 - e.bbox.y0) * n
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function D(e = .5) {
|
|
164
|
+
if (!Number.isFinite(e) || e < 0 || e > 1) throw RangeError("OCR minConfidence must be between 0 and 1.");
|
|
165
|
+
return e;
|
|
166
|
+
}
|
|
167
|
+
function Oe(e, t) {
|
|
168
|
+
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);
|
|
169
|
+
return s > 0 ? o / s : 0;
|
|
170
|
+
}
|
|
171
|
+
function O(e) {
|
|
172
|
+
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 = [];
|
|
173
|
+
for (let e of t) n.some((t) => Oe(t, e) >= .65) || n.push(e);
|
|
174
|
+
return n;
|
|
175
|
+
}
|
|
176
|
+
function k(e, t, n, r, i) {
|
|
177
|
+
let a = e.text.trim();
|
|
178
|
+
if (!a || !/[\p{L}\p{N}]/u.test(a)) return !1;
|
|
179
|
+
let o = e.bbox.width * e.bbox.height / Math.max(1, n * r), s = e.bbox.width / Math.max(1, e.bbox.height);
|
|
180
|
+
if (o < (t === "word" ? 15e-6 : 15e-7) || s < .05 || s > (t === "word" ? 50 : 8)) return !1;
|
|
181
|
+
if (i) {
|
|
182
|
+
let t = i(e.bbox);
|
|
183
|
+
if (t < .01 || t > .9) return !1;
|
|
184
|
+
}
|
|
185
|
+
return !0;
|
|
186
|
+
}
|
|
187
|
+
function ke(e) {
|
|
188
|
+
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 = [];
|
|
189
|
+
for (let e of t) {
|
|
190
|
+
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);
|
|
191
|
+
if (r) {
|
|
192
|
+
r.words.push(e);
|
|
193
|
+
let n = r.words.length;
|
|
194
|
+
r.centerY = (r.centerY * (n - 1) + t) / n, r.height = Math.max(r.height, e.bbox.height);
|
|
195
|
+
} else n.push({
|
|
196
|
+
centerY: t,
|
|
197
|
+
height: e.bbox.height,
|
|
198
|
+
words: [e]
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return n.sort((e, t) => e.centerY - t.centerY).map((e) => e.words.sort((e, t) => e.bbox.x - t.bbox.x).map((e) => e.text.trim()).join(" ")).join("\n");
|
|
202
|
+
}
|
|
203
|
+
function A(e, t, n, r) {
|
|
204
|
+
let i = D(t.minConfidence), a = [], o = [], s = 1, c = 1;
|
|
205
|
+
for (let t of e) {
|
|
206
|
+
let e = t.scaleX ?? 1, n = t.scaleY ?? 1, r = t.offsetX ?? 0, l = t.offsetY ?? 0;
|
|
207
|
+
for (let u of t.blocks ?? []) for (let t of u.paragraphs) for (let u of t.lines) for (let t of u.words) {
|
|
208
|
+
let u = E(t, e, n, r, l);
|
|
209
|
+
s = Math.max(s, u.bbox.x + u.bbox.width), c = Math.max(c, u.bbox.y + u.bbox.height), u.confidence >= i && a.push(u);
|
|
210
|
+
for (let a of t.symbols) {
|
|
211
|
+
let t = E(a, e, n, r, l);
|
|
212
|
+
t.confidence >= i && o.push(t);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
let l = n?.width ?? s, u = n?.height ?? c, d = O(a).filter((e) => k(e, "word", l, u, r)), f = O(o).filter((e) => k(e, "character", l, u, r));
|
|
217
|
+
return {
|
|
218
|
+
text: ke(d),
|
|
219
|
+
confidence: d.length ? d.reduce((e, t) => e + t.confidence, 0) / d.length : 0,
|
|
220
|
+
words: t.includeWords ? d : [],
|
|
221
|
+
characters: t.includeCharacters ? f : []
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function Ae(e) {
|
|
225
|
+
let t = /* @__PURE__ */ new Uint32Array(256);
|
|
226
|
+
for (let n of e) t[n] += 1;
|
|
227
|
+
let n = 0;
|
|
228
|
+
for (let e = 0; e < 256; e += 1) n += e * t[e];
|
|
229
|
+
let r = 0, i = 0, a = -1, o = 127;
|
|
230
|
+
for (let s = 0; s < 256; s += 1) {
|
|
231
|
+
if (r += t[s], !r) continue;
|
|
232
|
+
let c = e.length - r;
|
|
233
|
+
if (!c) break;
|
|
234
|
+
i += s * t[s];
|
|
235
|
+
let l = i / r, u = (n - i) / c, d = r * c * (l - u) ** 2;
|
|
236
|
+
d > a && (a = d, o = s);
|
|
237
|
+
}
|
|
238
|
+
return o;
|
|
239
|
+
}
|
|
240
|
+
function j(e) {
|
|
241
|
+
let t = e.getContext("2d", { willReadFrequently: !0 });
|
|
242
|
+
if (!t) throw i();
|
|
243
|
+
let n = t.getImageData(0, 0, e.width, e.height), r = new Uint8Array(e.width * e.height);
|
|
244
|
+
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);
|
|
245
|
+
return {
|
|
246
|
+
context: t,
|
|
247
|
+
image: n,
|
|
248
|
+
luminance: r
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function je(e) {
|
|
252
|
+
let t = 1800, n = Math.max(e.width, e.height), r = n > t ? t / n : Math.min(2, t / Math.max(1, n)), a = document.createElement("canvas");
|
|
253
|
+
a.width = Math.max(1, Math.round(e.width * r)), a.height = Math.max(1, Math.round(e.height * r));
|
|
254
|
+
let o = a.getContext("2d", { willReadFrequently: !0 });
|
|
255
|
+
if (!o) throw i();
|
|
256
|
+
o.drawImage(e, 0, 0, a.width, a.height);
|
|
257
|
+
let { image: s, luminance: c } = j(a), l = te(c, a.width, a.height);
|
|
258
|
+
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;
|
|
259
|
+
return o.putImageData(s, 0, 0), a;
|
|
260
|
+
}
|
|
261
|
+
function Me(e) {
|
|
262
|
+
let t = document.createElement("canvas");
|
|
263
|
+
t.width = 72, t.height = 72;
|
|
264
|
+
let n = t.getContext("2d", { willReadFrequently: !0 });
|
|
265
|
+
if (!n) return {
|
|
266
|
+
strong: !1,
|
|
267
|
+
strength: 0,
|
|
268
|
+
darkness: 0,
|
|
269
|
+
areaRatio: 0
|
|
270
|
+
};
|
|
271
|
+
n.drawImage(e, 0, 0, t.width, t.height);
|
|
272
|
+
let { luminance: r } = j(t);
|
|
273
|
+
return m(r, t.width, t.height);
|
|
274
|
+
}
|
|
275
|
+
function Ne(e, t, n, r) {
|
|
276
|
+
let i = (e.bbox.x + e.bbox.width / 2) / n, a = (e.bbox.y + e.bbox.height / 2) / r;
|
|
277
|
+
return i >= t.x && i <= t.x + t.width && a >= t.y && a <= t.y + t.height;
|
|
278
|
+
}
|
|
279
|
+
function Pe(e, t, n) {
|
|
280
|
+
if (!e.strong || !e.region) return !1;
|
|
281
|
+
let r = t.filter((t) => Ne(t, e.region, n.width, n.height)).length, i = t.length - r, a = e.region.width * e.region.height, o = i > 0 && a < .95 ? i / (1 - a) * a : 3;
|
|
282
|
+
return r < Math.max(3, o * .85);
|
|
283
|
+
}
|
|
284
|
+
function Fe(e, t, n, r) {
|
|
285
|
+
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");
|
|
286
|
+
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);
|
|
287
|
+
let l = n / e.width, u = r / e.height;
|
|
288
|
+
return {
|
|
289
|
+
tile: c,
|
|
290
|
+
passMapping: {
|
|
291
|
+
scaleX: l,
|
|
292
|
+
scaleY: u,
|
|
293
|
+
offsetX: i * l,
|
|
294
|
+
offsetY: a * u
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function Ie(e) {
|
|
299
|
+
let t = 2200, n = Math.max(e.width, e.height), r = n > t ? t / n : Math.min(1.5, t / Math.max(1, n)), a = document.createElement("canvas");
|
|
300
|
+
a.width = Math.max(1, Math.round(e.width * r)), a.height = Math.max(1, Math.round(e.height * r));
|
|
301
|
+
let o = a.getContext("2d", { willReadFrequently: !0 });
|
|
302
|
+
if (!o) throw i();
|
|
303
|
+
o.drawImage(e, 0, 0, a.width, a.height);
|
|
304
|
+
let s = o.getImageData(0, 0, a.width, a.height), c = new Uint8Array(a.width * a.height);
|
|
305
|
+
for (let e = 0, t = 0; e < c.length; e += 1, t += 4) c[e] = Math.round(s.data[t] * .299 + s.data[t + 1] * .587 + s.data[t + 2] * .114);
|
|
306
|
+
let l = Ae(c);
|
|
307
|
+
for (let e = 0, t = 0; e < c.length; e += 1, t += 4) {
|
|
308
|
+
let n = c[e] <= l ? 0 : 255;
|
|
309
|
+
s.data[t] = n, s.data[t + 1] = n, s.data[t + 2] = n, s.data[t + 3] = 255;
|
|
310
|
+
}
|
|
311
|
+
return o.putImageData(s, 0, 0), a;
|
|
312
|
+
}
|
|
313
|
+
function Le(e) {
|
|
314
|
+
let t = document.createElement("canvas");
|
|
315
|
+
t.width = 24, t.height = 24;
|
|
316
|
+
let n = t.getContext("2d", { willReadFrequently: !0 });
|
|
317
|
+
return (r) => {
|
|
318
|
+
if (!n || r.width <= 0 || r.height <= 0) return 0;
|
|
319
|
+
n.clearRect(0, 0, t.width, t.height), n.drawImage(e, r.x, r.y, r.width, r.height, 0, 0, t.width, t.height);
|
|
320
|
+
let i = n.getImageData(0, 0, t.width, t.height).data, a = 255, o = 0, s = new Uint8Array(t.width * t.height);
|
|
321
|
+
for (let e = 0, t = 0; e < s.length; e += 1, t += 4) {
|
|
322
|
+
let n = Math.round(i[t] * .299 + i[t + 1] * .587 + i[t + 2] * .114);
|
|
323
|
+
s[e] = n, a = Math.min(a, n), o = Math.max(o, n);
|
|
324
|
+
}
|
|
325
|
+
if (o - a < 12) return 0;
|
|
326
|
+
let c = a + (o - a) * .55, l = 0;
|
|
327
|
+
for (let e of s) e < c && (l += 1);
|
|
328
|
+
return l / s.length;
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
async function M(e, t = {}) {
|
|
332
|
+
await b("ocr");
|
|
333
|
+
let r = Ee(t.language, t.langPath), i = r.language;
|
|
334
|
+
D(t.minConfidence);
|
|
335
|
+
let a = await n(e, { cloneCanvas: !1 }), o = Ie(a), s = t.shadowPass === !1 ? {
|
|
336
|
+
strong: !1,
|
|
337
|
+
strength: 0,
|
|
338
|
+
darkness: 0,
|
|
339
|
+
areaRatio: 0
|
|
340
|
+
} : Me(a), { createWorker: c, PSM: l } = await import("tesseract.js"), u = 0, d = s.strong ? 3 : 2, f = await c(r.kind === "bundled-eng" ? [{
|
|
341
|
+
code: "eng",
|
|
342
|
+
data: await De()
|
|
343
|
+
}] : r.language, void 0, {
|
|
344
|
+
workerPath: _e.href,
|
|
345
|
+
corePath: Te(we()).href,
|
|
346
|
+
...r.kind === "lang-path" ? { langPath: r.langPath } : {},
|
|
347
|
+
logger(e) {
|
|
348
|
+
typeof e.progress == "number" && t.onProgress?.(Math.min(1, (u + e.progress) / d), e.status);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
try {
|
|
352
|
+
await f.setParameters({ tessedit_pageseg_mode: l.AUTO });
|
|
353
|
+
let e = await f.recognize(a, {}, {
|
|
354
|
+
text: !0,
|
|
355
|
+
blocks: !0
|
|
356
|
+
});
|
|
357
|
+
u = 1, await f.setParameters({
|
|
358
|
+
tessedit_pageseg_mode: l.SPARSE_TEXT,
|
|
359
|
+
preserve_interword_spaces: "1"
|
|
360
|
+
});
|
|
361
|
+
let n = await f.recognize(o, {}, {
|
|
362
|
+
text: !0,
|
|
363
|
+
blocks: !0
|
|
364
|
+
}), r = [{ blocks: e.data.blocks }, {
|
|
365
|
+
blocks: n.data.blocks,
|
|
366
|
+
scaleX: a.width / o.width,
|
|
367
|
+
scaleY: a.height / o.height
|
|
368
|
+
}], c = {
|
|
369
|
+
...t,
|
|
370
|
+
includeWords: !0
|
|
371
|
+
}, d = Le(a), p = A(r, c, {
|
|
372
|
+
width: a.width,
|
|
373
|
+
height: a.height
|
|
374
|
+
}, d), m = t.shadowPass !== !1 && Pe(s, p.words, {
|
|
375
|
+
width: a.width,
|
|
376
|
+
height: a.height
|
|
377
|
+
});
|
|
378
|
+
if (m && s.region) {
|
|
379
|
+
u = 2;
|
|
380
|
+
let { tile: e, passMapping: t } = Fe(je(a), s.region, a.width, a.height);
|
|
381
|
+
await f.setParameters({
|
|
382
|
+
tessedit_pageseg_mode: l.SPARSE_TEXT,
|
|
383
|
+
preserve_interword_spaces: "1"
|
|
384
|
+
});
|
|
385
|
+
let n = await f.recognize(e, {}, {
|
|
386
|
+
text: !0,
|
|
387
|
+
blocks: !0
|
|
388
|
+
});
|
|
389
|
+
r.push({
|
|
390
|
+
blocks: n.data.blocks,
|
|
391
|
+
...t
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
let h = r.length === 2 ? p : A(r, c, {
|
|
395
|
+
width: a.width,
|
|
396
|
+
height: a.height
|
|
397
|
+
}, d);
|
|
398
|
+
return t.onProgress?.(1, "recognizing text"), {
|
|
399
|
+
text: h.text,
|
|
400
|
+
confidence: h.confidence,
|
|
401
|
+
language: i,
|
|
402
|
+
imageWidth: a.width,
|
|
403
|
+
imageHeight: a.height,
|
|
404
|
+
diagnostics: {
|
|
405
|
+
shadowPassUsed: m,
|
|
406
|
+
castShadowStrength: s.strength,
|
|
407
|
+
shadowRegion: s.region ? {
|
|
408
|
+
x: s.region.x * a.width,
|
|
409
|
+
y: s.region.y * a.height,
|
|
410
|
+
width: s.region.width * a.width,
|
|
411
|
+
height: s.region.height * a.height
|
|
412
|
+
} : void 0
|
|
413
|
+
},
|
|
414
|
+
...t.includeWords ? { words: h.words } : {},
|
|
415
|
+
...t.includeCharacters ? { characters: h.characters } : {}
|
|
416
|
+
};
|
|
417
|
+
} finally {
|
|
418
|
+
await f.terminate();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/sdk/redact.ts
|
|
423
|
+
async function N(e, t, a = {}) {
|
|
424
|
+
await b("redaction");
|
|
425
|
+
let o = await n(e), s = o.getContext("2d");
|
|
426
|
+
if (!s) throw i();
|
|
427
|
+
for (let e of t) {
|
|
428
|
+
let t = r(e, o.width, o.height);
|
|
429
|
+
if (t.width < 1 || t.height < 1) continue;
|
|
430
|
+
if ((e.mode ?? a.mode ?? "blackout") === "blackout") {
|
|
431
|
+
s.fillStyle = "#050706", s.fillRect(t.x, t.y, t.width, t.height);
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
let n = document.createElement("canvas");
|
|
435
|
+
n.width = Math.ceil(t.width), n.height = Math.ceil(t.height);
|
|
436
|
+
let i = n.getContext("2d");
|
|
437
|
+
if (!i) continue;
|
|
438
|
+
i.filter = `blur(${a.blurRadius ?? 18}px)`;
|
|
439
|
+
let c = Math.max(12, a.blurRadius ?? 18);
|
|
440
|
+
i.drawImage(o, t.x - c, t.y - c, t.width + c * 2, t.height + c * 2, -c, -c, t.width + c * 2, t.height + c * 2), s.drawImage(n, t.x, t.y, t.width, t.height);
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
canvas: o,
|
|
444
|
+
blob: await u(o, a.mimeType ?? "image/png", a.quality)
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
//#endregion
|
|
448
|
+
//#region src/sdk/face-model.ts
|
|
449
|
+
var P = /* @__PURE__ */ new Map(), Re = 8400;
|
|
450
|
+
function ze(e, t) {
|
|
451
|
+
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));
|
|
452
|
+
return n / Math.max(1, e.width * e.height + t.width * t.height - n);
|
|
453
|
+
}
|
|
454
|
+
function Be(e, t, n, r, i, a, o) {
|
|
455
|
+
return {
|
|
456
|
+
x: (e - n / 2) / i * a,
|
|
457
|
+
y: (t - r / 2) / i * o,
|
|
458
|
+
width: n / i * a,
|
|
459
|
+
height: r / i * o
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function Ve(e, t, n) {
|
|
463
|
+
let r = t.length === 3 ? t[1] : t[0], i = t.length === 3 ? t[2] : t[1];
|
|
464
|
+
if (i !== 6 || r < 1) throw Error(`Face model expects [1, anchors, 6]. A 640 YOLOX-Nano face export is [1, ${Re}, 6].`);
|
|
465
|
+
let a = n.confidence ?? .35, o = n.inputSize ?? 640, s = [];
|
|
466
|
+
for (let t = 0; t < r; t += 1) {
|
|
467
|
+
let r = t * i, c = e[r + 4] * e[r + 5];
|
|
468
|
+
c < a || s.push({
|
|
469
|
+
confidence: c,
|
|
470
|
+
bbox: Be(e[r], e[r + 1], e[r + 2], e[r + 3], o, n.imageWidth, n.imageHeight)
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
return s.sort((e, t) => t.confidence - e.confidence).filter((e, t, n) => n.slice(0, t).every((t) => ze(e.bbox, t.bbox) < .45)).slice(0, 8);
|
|
474
|
+
}
|
|
475
|
+
async function He(e, t, r = .35) {
|
|
476
|
+
let a = await n(e, { cloneCanvas: !1 }), o = await import("onnxruntime-web"), s = document.createElement("canvas");
|
|
477
|
+
s.width = 640, s.height = 640;
|
|
478
|
+
let c = s.getContext("2d", { willReadFrequently: !0 });
|
|
479
|
+
if (!c) throw i();
|
|
480
|
+
c.drawImage(a, 0, 0, 640, 640);
|
|
481
|
+
let l = c.getImageData(0, 0, 640, 640).data, u = 409600, d = new Float32Array(u * 3);
|
|
482
|
+
for (let e = 0; e < u; e += 1) d[e] = l[e * 4] / 255, d[u + e] = l[e * 4 + 1] / 255, d[u * 2 + e] = l[e * 4 + 2] / 255;
|
|
483
|
+
let f = P.get(t);
|
|
484
|
+
f || (f = o.InferenceSession.create(t, {
|
|
485
|
+
executionProviders: ["wasm"],
|
|
486
|
+
graphOptimizationLevel: "all"
|
|
487
|
+
}), P.set(t, f));
|
|
488
|
+
let p = await f, m = (await p.run({ [p.inputNames[0]]: new o.Tensor("float32", d, [
|
|
489
|
+
1,
|
|
490
|
+
3,
|
|
491
|
+
640,
|
|
492
|
+
640
|
|
493
|
+
]) }))[p.outputNames[0]];
|
|
494
|
+
return Ve(m.data, m.dims.map(Number), {
|
|
495
|
+
confidence: r,
|
|
496
|
+
imageWidth: a.width,
|
|
497
|
+
imageHeight: a.height,
|
|
498
|
+
inputSize: 640
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
//#endregion
|
|
502
|
+
//#region src/sdk/field-model.ts
|
|
503
|
+
var F = /* @__PURE__ */ new Map(), I = [
|
|
504
|
+
"person_name",
|
|
505
|
+
"date_of_birth",
|
|
506
|
+
"address",
|
|
507
|
+
"document_number",
|
|
508
|
+
"expiration_date",
|
|
509
|
+
"postal_code",
|
|
510
|
+
"state"
|
|
511
|
+
], Ue = 8400;
|
|
512
|
+
function L(e) {
|
|
513
|
+
return 5 + e;
|
|
514
|
+
}
|
|
515
|
+
var R = 640;
|
|
516
|
+
function We(e, t) {
|
|
517
|
+
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));
|
|
518
|
+
return n / Math.max(1, e.bbox.width * e.bbox.height + t.bbox.width * t.bbox.height - n);
|
|
519
|
+
}
|
|
520
|
+
function z(e) {
|
|
521
|
+
return e.sort((e, t) => t.confidence - e.confidence).filter((e, t, n) => n.slice(0, t).every((t) => t.field !== e.field || We(e, t) < .45)).slice(0, 30);
|
|
522
|
+
}
|
|
523
|
+
function B(e, t, n, r, i, a, o) {
|
|
524
|
+
return {
|
|
525
|
+
x: (e - n / 2) / i * a,
|
|
526
|
+
y: (t - r / 2) / i * o,
|
|
527
|
+
width: n / i * a,
|
|
528
|
+
height: r / i * o
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
function V(e, t) {
|
|
532
|
+
let n = L(t);
|
|
533
|
+
return e.length === 3 && e[0] === 1 && e[2] === n && e[1] > e[2] ? {
|
|
534
|
+
anchors: e[1],
|
|
535
|
+
features: n
|
|
536
|
+
} : e.length === 2 && e[1] === n && e[0] > e[1] ? {
|
|
537
|
+
anchors: e[0],
|
|
538
|
+
features: n
|
|
539
|
+
} : null;
|
|
540
|
+
}
|
|
541
|
+
function Ge(e) {
|
|
542
|
+
return e.length === 3 && e[1] >= 5 && e[2] >= 1;
|
|
543
|
+
}
|
|
544
|
+
function Ke(e) {
|
|
545
|
+
let t = L(e);
|
|
546
|
+
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, ${Ue}, ${t}].`);
|
|
547
|
+
}
|
|
548
|
+
function qe(e, t, n, r, i, a, o) {
|
|
549
|
+
if (!Ge(t)) throw Error("Unsupported field-model YOLO output shape.");
|
|
550
|
+
let s = t[1], c = t[2], l = [];
|
|
551
|
+
for (let t = 0; t < c; t += 1) {
|
|
552
|
+
let u = 0, d = 0;
|
|
553
|
+
for (let r = 4; r < Math.min(s, n.length + 4); r += 1) {
|
|
554
|
+
let n = e[r * c + t];
|
|
555
|
+
n > u && (u = n, d = r - 4);
|
|
556
|
+
}
|
|
557
|
+
let f = n[d];
|
|
558
|
+
!f || u < r || l.push({
|
|
559
|
+
field: f,
|
|
560
|
+
confidence: u,
|
|
561
|
+
bbox: B(e[t], e[c + t], e[c * 2 + t], e[c * 3 + t], i, a, o)
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
return z(l);
|
|
565
|
+
}
|
|
566
|
+
function Je(e, t, n, r, i, a, o) {
|
|
567
|
+
let s = V(t, n.length);
|
|
568
|
+
if (!s) throw Ke(n.length);
|
|
569
|
+
let c = [];
|
|
570
|
+
for (let t = 0; t < s.anchors; t += 1) {
|
|
571
|
+
let l = t * s.features, u = e[l + 4], d = 0, f = 0;
|
|
572
|
+
for (let t = 0; t < n.length; t += 1) {
|
|
573
|
+
let n = e[l + 5 + t];
|
|
574
|
+
n > d && (d = n, f = t);
|
|
575
|
+
}
|
|
576
|
+
let p = n[f], m = u * d;
|
|
577
|
+
!p || m < r || c.push({
|
|
578
|
+
field: p,
|
|
579
|
+
confidence: m,
|
|
580
|
+
bbox: B(e[l], e[l + 1], e[l + 2], e[l + 3], i, a, o)
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
return z(c);
|
|
584
|
+
}
|
|
585
|
+
function Ye(e, t, n) {
|
|
586
|
+
let r = n.classes ?? I, i = n.confidence ?? .35, a = n.inputSize ?? R, o = V(t, r.length);
|
|
587
|
+
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.");
|
|
588
|
+
return (n.layout ?? (o ? "yolox" : "yolov8")) === "yolox" ? Je(e, t, r, i, a, n.imageWidth, n.imageHeight) : qe(e, t, r, i, a, n.imageWidth, n.imageHeight);
|
|
589
|
+
}
|
|
590
|
+
async function Xe(e, t, r = I, a = .35, o) {
|
|
591
|
+
let c = await n(e, { cloneCanvas: !1 }), l = import("onnxruntime-web"), u = R, d = document.createElement("canvas");
|
|
592
|
+
d.width = u, d.height = u;
|
|
593
|
+
let f = d.getContext("2d", { willReadFrequently: !0 });
|
|
594
|
+
if (!f) throw i();
|
|
595
|
+
f.drawImage(c, 0, 0, u, u);
|
|
596
|
+
let p = f.getImageData(0, 0, u, u).data, m = u * u, h = new Float32Array(m * 3);
|
|
597
|
+
for (let e = 0; e < m; e += 1) h[e] = p[e * 4] / 255, h[m + e] = p[e * 4 + 1] / 255, h[m * 2 + e] = p[e * 4 + 2] / 255;
|
|
598
|
+
let g = F.get(t);
|
|
599
|
+
g || (g = (async () => {
|
|
600
|
+
try {
|
|
601
|
+
let [e, n] = await Promise.all([l, s(t)]);
|
|
602
|
+
return await e.InferenceSession.create(n, {
|
|
603
|
+
executionProviders: ["wasm"],
|
|
604
|
+
graphOptimizationLevel: "all"
|
|
605
|
+
});
|
|
606
|
+
} catch (e) {
|
|
607
|
+
throw F.delete(t), e;
|
|
608
|
+
}
|
|
609
|
+
})(), F.set(t, g));
|
|
610
|
+
let [_, v] = await Promise.all([l, g]), y = (await v.run({ [v.inputNames[0]]: new _.Tensor("float32", h, [
|
|
611
|
+
1,
|
|
612
|
+
3,
|
|
613
|
+
u,
|
|
614
|
+
u
|
|
615
|
+
]) }))[v.outputNames[0]];
|
|
616
|
+
return Ye(y.data, y.dims.map(Number), {
|
|
617
|
+
classes: r,
|
|
618
|
+
confidence: a,
|
|
619
|
+
layout: o,
|
|
620
|
+
imageWidth: c.width,
|
|
621
|
+
imageHeight: c.height,
|
|
622
|
+
inputSize: u
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
//#endregion
|
|
626
|
+
//#region src/sdk/programmatic-redaction.ts
|
|
627
|
+
function H(e) {
|
|
628
|
+
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));
|
|
629
|
+
return {
|
|
630
|
+
x: t,
|
|
631
|
+
y: n,
|
|
632
|
+
width: r - t,
|
|
633
|
+
height: i - n
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
function U(e) {
|
|
637
|
+
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);
|
|
638
|
+
for (let e of n) {
|
|
639
|
+
let n = e.bbox.y + e.bbox.height / 2, r = t.find((t) => {
|
|
640
|
+
let r = H(t), i = r.y + r.height / 2;
|
|
641
|
+
return Math.abs(n - i) <= Math.max(e.bbox.height, r.height) * .65;
|
|
642
|
+
});
|
|
643
|
+
r ? r.push(e) : t.push([e]);
|
|
644
|
+
}
|
|
645
|
+
return t.map((e) => (e.sort((e, t) => e.bbox.x - t.bbox.x), {
|
|
646
|
+
words: e,
|
|
647
|
+
text: e.map((e) => e.text).join(" "),
|
|
648
|
+
bbox: H(e)
|
|
649
|
+
}));
|
|
650
|
+
}
|
|
651
|
+
function W(e, t, n = !1) {
|
|
652
|
+
let r = e.trim().replace(/\s+/g, " "), i = n ? r.replace(/[::]\s*$/, "") : r;
|
|
653
|
+
return t ? i.toLocaleLowerCase() : i;
|
|
654
|
+
}
|
|
655
|
+
function G(e) {
|
|
656
|
+
return e.reduce((e, t) => e + t.confidence, 0) / Math.max(1, e.length);
|
|
657
|
+
}
|
|
658
|
+
function Ze(e, t) {
|
|
659
|
+
let n = e.mode, r = n === "case-insensitive", i = e.scope ?? "line", a;
|
|
660
|
+
if (n === "regex") {
|
|
661
|
+
let t = e.pattern ?? e.values?.[0];
|
|
662
|
+
if (!t) return [];
|
|
663
|
+
a = new RegExp(t, e.flags?.replace("g", "") ?? "");
|
|
664
|
+
}
|
|
665
|
+
let o = (e.values ?? []).filter(Boolean), s = [], c = (t) => s.push({
|
|
666
|
+
ruleId: e.id,
|
|
667
|
+
strategy: "string",
|
|
668
|
+
source: "string",
|
|
669
|
+
bbox: H(t),
|
|
670
|
+
text: t.map((e) => e.text).join(" "),
|
|
671
|
+
confidence: G(t)
|
|
672
|
+
});
|
|
673
|
+
if (i === "word") {
|
|
674
|
+
for (let e of t) for (let t of e.words) {
|
|
675
|
+
let e = W(t.text, r);
|
|
676
|
+
(a ? a.test(t.text) : o.some((t) => e === W(t, r))) && c([t]);
|
|
677
|
+
}
|
|
678
|
+
return s;
|
|
679
|
+
}
|
|
680
|
+
for (let e of t) {
|
|
681
|
+
if (a) {
|
|
682
|
+
a.test(e.text) && c(e.words);
|
|
683
|
+
continue;
|
|
684
|
+
}
|
|
685
|
+
for (let t of o) {
|
|
686
|
+
let n = W(t, r).split(" ");
|
|
687
|
+
for (let t = 0; t <= e.words.length - n.length; t += 1) {
|
|
688
|
+
let i = e.words.slice(t, t + n.length);
|
|
689
|
+
i.every((e, t) => W(e.text, r) === n[t]) && c(i);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return s;
|
|
694
|
+
}
|
|
695
|
+
function Qe(e, t) {
|
|
696
|
+
let n = e.labelMode !== "exact", r = W(e.label, n, !0).split(" ");
|
|
697
|
+
for (let e of t) for (let t = 0; t <= e.words.length - r.length; t += 1) {
|
|
698
|
+
let i = e.words.slice(t, t + r.length);
|
|
699
|
+
if (i.every((e, t) => W(e.text, n, !0) === r[t])) return i;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
function $e(e, t, n, r, i, a) {
|
|
703
|
+
let o = a.window?.distance ?? .28, s = a.window?.crossAxis ?? .12, c = t.x + t.width / 2, l = t.y + t.height / 2;
|
|
704
|
+
return e.filter((e) => {
|
|
705
|
+
let a = e.bbox.x + e.bbox.width / 2, u = e.bbox.y + e.bbox.height / 2;
|
|
706
|
+
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;
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
function K(e, t, n, r, i, a = "label") {
|
|
710
|
+
let o = Qe(e, n);
|
|
711
|
+
if (!o) return [];
|
|
712
|
+
let s = H(o), c = new Set(o), l = t.filter((e) => !c.has(e)), u = e.direction === "any" ? [
|
|
713
|
+
"right",
|
|
714
|
+
"below",
|
|
715
|
+
"left",
|
|
716
|
+
"above"
|
|
717
|
+
] : [e.direction], d;
|
|
718
|
+
for (let t of u) {
|
|
719
|
+
let n = U($e(l, s, t, r, i, e));
|
|
720
|
+
for (let t of n) {
|
|
721
|
+
if (e.valuePattern && !new RegExp(e.valuePattern, e.patternFlags).test(t.text)) continue;
|
|
722
|
+
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);
|
|
723
|
+
(!d || l < d.distance) && (d = {
|
|
724
|
+
words: t.words,
|
|
725
|
+
distance: l
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
if (!d) return [];
|
|
730
|
+
let f = e.redactLabel ? [...o, ...d.words] : d.words;
|
|
731
|
+
return [{
|
|
732
|
+
ruleId: e.id,
|
|
733
|
+
strategy: a,
|
|
734
|
+
source: a === "template-anchor" ? "template" : "label",
|
|
735
|
+
bbox: H(f),
|
|
736
|
+
text: d.words.map((e) => e.text).join(" "),
|
|
737
|
+
confidence: Math.max(0, G(d.words) * (1 - Math.min(.5, d.distance)))
|
|
738
|
+
}];
|
|
739
|
+
}
|
|
740
|
+
var q = {
|
|
741
|
+
person_name: [
|
|
742
|
+
"LN",
|
|
743
|
+
"FN",
|
|
744
|
+
"DCS",
|
|
745
|
+
"DAC",
|
|
746
|
+
"DAD",
|
|
747
|
+
"NAME"
|
|
748
|
+
],
|
|
749
|
+
date_of_birth: [
|
|
750
|
+
"DOB",
|
|
751
|
+
"DBB",
|
|
752
|
+
"BIRTH"
|
|
753
|
+
],
|
|
754
|
+
address: [
|
|
755
|
+
"ADDRESS",
|
|
756
|
+
"ADDR",
|
|
757
|
+
"DAG",
|
|
758
|
+
"DAI"
|
|
759
|
+
],
|
|
760
|
+
document_number: [
|
|
761
|
+
"DLN",
|
|
762
|
+
"DAQ",
|
|
763
|
+
"IDNO",
|
|
764
|
+
"LICNO"
|
|
765
|
+
],
|
|
766
|
+
expiration_date: [
|
|
767
|
+
"EXP",
|
|
768
|
+
"DBA",
|
|
769
|
+
"EXPIRES"
|
|
770
|
+
],
|
|
771
|
+
postal_code: [
|
|
772
|
+
"ZIP",
|
|
773
|
+
"DAK",
|
|
774
|
+
"POSTAL"
|
|
775
|
+
],
|
|
776
|
+
state: ["STATE", "DAJ"]
|
|
777
|
+
}, et = 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;
|
|
778
|
+
function Z(e, t) {
|
|
779
|
+
let n = t.trim();
|
|
780
|
+
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;
|
|
781
|
+
}
|
|
782
|
+
function Q(e, t, n, r, i = 1) {
|
|
783
|
+
return {
|
|
784
|
+
ruleId: e,
|
|
785
|
+
strategy: "semantic",
|
|
786
|
+
source: t,
|
|
787
|
+
bbox: H(n),
|
|
788
|
+
text: r ?? n.map((e) => e.text).join(" "),
|
|
789
|
+
confidence: Math.max(0, Math.min(1, G(n) * i))
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
function tt(e, t, n) {
|
|
793
|
+
let r = q[e], i = [];
|
|
794
|
+
for (let a of n) for (let n = 0; n < a.words.length; n += 1) {
|
|
795
|
+
let o = a.words[n].text.toUpperCase().replace(/[^A-Z0-9]/g, ""), s = r.find((e) => o === e || o.startsWith(e));
|
|
796
|
+
if (!s) continue;
|
|
797
|
+
let c, l;
|
|
798
|
+
o === s ? (c = a.words.slice(n + 1).filter((e) => !et.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));
|
|
799
|
+
}
|
|
800
|
+
return i;
|
|
801
|
+
}
|
|
802
|
+
function nt(e, t, n) {
|
|
803
|
+
let r = n.filter((e) => {
|
|
804
|
+
let t = e.text.replace(/\s/g, "");
|
|
805
|
+
return t.includes("<<") || /^[A-Z0-9<]{25,}$/.test(t);
|
|
806
|
+
});
|
|
807
|
+
if (!r.length) return [];
|
|
808
|
+
if (e === "person_name") {
|
|
809
|
+
let e = r.find((e) => e.text.includes("<<"));
|
|
810
|
+
if (!e) return [];
|
|
811
|
+
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();
|
|
812
|
+
return a ? [Q(t, "mrz", e.words, a, .96)] : [];
|
|
813
|
+
}
|
|
814
|
+
if (e === "date_of_birth") for (let e of r) {
|
|
815
|
+
let n = e.text.replace(/\s/g, ""), r = /^\d{6}/.exec(n)?.[0] ?? /^.{13}(\d{6})/.exec(n)?.[1];
|
|
816
|
+
if (r) return [Q(t, "mrz", e.words, r, .96)];
|
|
817
|
+
}
|
|
818
|
+
if (e === "document_number") {
|
|
819
|
+
let e = r[0];
|
|
820
|
+
return [Q(t, "mrz", e.words, e.text, .9)];
|
|
821
|
+
}
|
|
822
|
+
return [];
|
|
823
|
+
}
|
|
824
|
+
function rt(e) {
|
|
825
|
+
let t = e.replace(/\s/g, ""), n = t.match(/\d{4}/)?.[0];
|
|
826
|
+
if (n) return Number(n);
|
|
827
|
+
let r = t.match(/^\d{2}/)?.[0];
|
|
828
|
+
if (!r) return;
|
|
829
|
+
let i = Number(r);
|
|
830
|
+
return i > (/* @__PURE__ */ new Date()).getFullYear() % 100 ? 1900 + i : 2e3 + i;
|
|
831
|
+
}
|
|
832
|
+
function it(e, t, n, r, i) {
|
|
833
|
+
if (e === "address") {
|
|
834
|
+
let e = r.findIndex((e) => /\d/.test(e.text) && Y.test(e.text));
|
|
835
|
+
if (e < 0) return [];
|
|
836
|
+
let n = [...r[e].words], i = r[e + 1];
|
|
837
|
+
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)];
|
|
838
|
+
}
|
|
839
|
+
if (e === "person_name") {
|
|
840
|
+
let e = r.map((e) => {
|
|
841
|
+
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);
|
|
842
|
+
return {
|
|
843
|
+
line: e,
|
|
844
|
+
score: r ? .45 + G(e.words) * .3 + a * .15 + (n < .65 ? .1 : 0) : 0
|
|
845
|
+
};
|
|
846
|
+
}).filter((e) => e.score >= .68).sort((e, t) => t.score - e.score);
|
|
847
|
+
return !e.length || e[1] && e[0].score - e[1].score < .04 ? [] : [Q(t, "layout", e[0].line.words, void 0, e[0].score)];
|
|
848
|
+
}
|
|
849
|
+
if (e === "date_of_birth" || e === "expiration_date") {
|
|
850
|
+
let r = (/* @__PURE__ */ new Date()).getFullYear(), i = n.filter((e) => J.test(e.text.replace(/\s/g, ""))).map((e) => ({
|
|
851
|
+
word: e,
|
|
852
|
+
year: rt(e.text)
|
|
853
|
+
})).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);
|
|
854
|
+
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)] : [];
|
|
855
|
+
}
|
|
856
|
+
if (e === "postal_code") {
|
|
857
|
+
let r = n.find((t) => Z(e, t.text));
|
|
858
|
+
return r ? [Q(t, "layout", [r], void 0, .78)] : [];
|
|
859
|
+
}
|
|
860
|
+
if (e === "state") {
|
|
861
|
+
let r = n.filter((t) => Z(e, t.text));
|
|
862
|
+
return r.length === 1 ? [Q(t, "layout", r, void 0, .7)] : [];
|
|
863
|
+
}
|
|
864
|
+
if (e === "document_number") {
|
|
865
|
+
let r = n.filter((t) => Z(e, t.text));
|
|
866
|
+
return r.sort((e, t) => t.text.length - e.text.length), r.length ? [Q(t, "layout", [r[0]], void 0, .7)] : [];
|
|
867
|
+
}
|
|
868
|
+
return [];
|
|
869
|
+
}
|
|
870
|
+
function at(e, t) {
|
|
871
|
+
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);
|
|
872
|
+
}
|
|
873
|
+
function ot(e, t, n, r) {
|
|
874
|
+
let i = tt(e.field, e.id, n), a = nt(e.field, e.id, n);
|
|
875
|
+
return i.length || a.length ? [...i, ...a] : it(e.field, e.id, t, n, r);
|
|
876
|
+
}
|
|
877
|
+
function st(e, t, n) {
|
|
878
|
+
let r = (n ?? []).filter((t) => t.field === e.field).sort((e, t) => t.confidence - e.confidence);
|
|
879
|
+
for (let n of r) {
|
|
880
|
+
let r = t.filter((e) => at(n.bbox, e.bbox) >= .3);
|
|
881
|
+
if (!r.length) continue;
|
|
882
|
+
let i = r.map((e) => e.text).join(" ");
|
|
883
|
+
if (Z(e.field, i)) return Q(e.id, "model", r, i, n.confidence);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
function ct(e, t, n, r, i, a) {
|
|
887
|
+
let o = ot(e, t, n, r);
|
|
888
|
+
if (e.detector !== "model" || !a) return o;
|
|
889
|
+
let s = st(e, t, i);
|
|
890
|
+
return s ? [s] : [];
|
|
891
|
+
}
|
|
892
|
+
function lt(e, t, n, r) {
|
|
893
|
+
let i = (e) => Math.max(0, Math.min(1, e)), a = i(e.rect.x), o = i(e.rect.y);
|
|
894
|
+
return {
|
|
895
|
+
ruleId: `${t}.${e.id}`,
|
|
896
|
+
strategy: "template-rect",
|
|
897
|
+
source: "template",
|
|
898
|
+
bbox: {
|
|
899
|
+
x: a * n,
|
|
900
|
+
y: o * r,
|
|
901
|
+
width: Math.max(0, Math.min(1 - a, e.rect.width)) * n,
|
|
902
|
+
height: Math.max(0, Math.min(1 - o, e.rect.height)) * r
|
|
903
|
+
},
|
|
904
|
+
confidence: 1
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
function $(e, t) {
|
|
908
|
+
let n = t.imageWidth ?? e?.imageWidth ?? 0, r = t.imageHeight ?? e?.imageHeight ?? 0, i = e?.words ?? [], a = U(i), o = [], s = [];
|
|
909
|
+
for (let e of t.rules) {
|
|
910
|
+
let c = e.strategy === "string" ? Ze(e, a) : e.strategy === "label" ? K(e, i, a, n, r) : ct(e, i, a, r, t.semanticDetections, t.modelRequested === !0);
|
|
911
|
+
c.length ? o.push(...c) : s.push(e.id);
|
|
912
|
+
}
|
|
913
|
+
for (let e of t.template?.fields ?? []) {
|
|
914
|
+
let c = `${t.template.id}.${e.id}`;
|
|
915
|
+
if (e.strategy === "rect") {
|
|
916
|
+
n && r ? o.push(lt(e, t.template.id, n, r)) : s.push(c);
|
|
917
|
+
continue;
|
|
918
|
+
}
|
|
919
|
+
let l = K({
|
|
920
|
+
...e,
|
|
921
|
+
id: c,
|
|
922
|
+
strategy: "label"
|
|
923
|
+
}, i, a, n, r, "template-anchor");
|
|
924
|
+
l.length ? o.push(...l) : s.push(c);
|
|
925
|
+
}
|
|
926
|
+
let c = t.rules.length + (t.template?.fields.length ?? 0), l = new Set(o.map((e) => e.ruleId)), u = c ? [...l].reduce((e, t) => {
|
|
927
|
+
let n = o.filter((e) => e.ruleId === t);
|
|
928
|
+
return e + Math.max(...n.map((e) => e.confidence));
|
|
929
|
+
}, 0) / c : 1;
|
|
930
|
+
return {
|
|
931
|
+
matches: o,
|
|
932
|
+
unmatchedRuleIds: s,
|
|
933
|
+
complete: s.length === 0,
|
|
934
|
+
confidence: u
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
function ut(e) {
|
|
938
|
+
return e.rules.length > 0 || e.template?.fields.some((e) => e.strategy === "anchor") === !0;
|
|
939
|
+
}
|
|
940
|
+
function dt(e, t = !1) {
|
|
941
|
+
return t && !e.complete ? "unmatched" : e.complete ? "success" : "partial";
|
|
942
|
+
}
|
|
943
|
+
async function ft(e, t) {
|
|
944
|
+
await b("redaction");
|
|
945
|
+
let r = await n(e, { cloneCanvas: !1 }), i = t.ocr;
|
|
946
|
+
ut(t) && !i && (i = await M(r, { includeWords: !0 }));
|
|
947
|
+
let a = t.semanticDetections, o = t.modelRequested === !0, s = "not-requested", c;
|
|
948
|
+
if (t.models?.fields) try {
|
|
949
|
+
a = await Xe(r, t.models.fields, t.models.fieldClasses, t.models.confidence, t.models.fieldLayout), o = !0, s = "loaded";
|
|
950
|
+
} catch (e) {
|
|
951
|
+
c = e instanceof Error ? e.message : "The field model could not load.";
|
|
952
|
+
let n = t.models.allowHeuristicFallback !== !1;
|
|
953
|
+
o = !n, s = n ? "fallback" : "failed";
|
|
954
|
+
}
|
|
955
|
+
let l = $(i, {
|
|
956
|
+
rules: t.rules,
|
|
957
|
+
template: t.template,
|
|
958
|
+
imageWidth: r.width,
|
|
959
|
+
imageHeight: r.height,
|
|
960
|
+
semanticDetections: a,
|
|
961
|
+
modelRequested: o
|
|
962
|
+
}), u = dt(l, t.requireAll), d = [], f = "not-requested", p;
|
|
963
|
+
if (t.models?.face) try {
|
|
964
|
+
d = await He(r, t.models.face, t.models.faceConfidence), f = "loaded";
|
|
965
|
+
} catch (e) {
|
|
966
|
+
p = e instanceof Error ? e.message : "The face model could not load.", f = "failed";
|
|
967
|
+
}
|
|
968
|
+
if (u === "unmatched") return {
|
|
969
|
+
...l,
|
|
970
|
+
status: u,
|
|
971
|
+
ocr: i,
|
|
972
|
+
modelStatus: s,
|
|
973
|
+
modelError: c,
|
|
974
|
+
faces: d,
|
|
975
|
+
faceStatus: f,
|
|
976
|
+
faceError: p
|
|
977
|
+
};
|
|
978
|
+
let m = await N(r, [...l.matches.map((e) => ({
|
|
979
|
+
...e.bbox,
|
|
980
|
+
mode: t.style?.mode
|
|
981
|
+
})), ...d.map((e) => ({
|
|
982
|
+
...e.bbox,
|
|
983
|
+
mode: t.style?.mode
|
|
984
|
+
}))], t.style);
|
|
985
|
+
return {
|
|
986
|
+
...l,
|
|
987
|
+
status: u,
|
|
988
|
+
ocr: i,
|
|
989
|
+
canvas: m.canvas,
|
|
990
|
+
blob: m.blob,
|
|
991
|
+
modelStatus: s,
|
|
992
|
+
modelError: c,
|
|
993
|
+
faces: d,
|
|
994
|
+
faceStatus: f,
|
|
995
|
+
faceError: p
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
//#endregion
|
|
999
|
+
export { le as AutoTorchController, p as DEFAULT_DOCUMENT_MODEL_URL, ee as DEV_LICENSE_HOSTS, e as LicenseError, h as NEAR_BLACK_LUMINANCE, ce as QuadStabilityTracker, c as ScannerError, N as applyRedactions, b as assertLicense, re as autoCaptureControl, g as captureVideoFrame, ge as createFinderSimulator, fe as cropDocument, ae as detectBrowserEngine, l as detectDocument, ne as formatCaptureDiagnosticsReport, a as getLicense, v as isNearBlackFrame, oe as limitingFillTarget, me as mapDocumentCornersToFrame, $ as matchRedactionRules, _ as meanFrameLuminance, y as measureQuadFill, d as mobileCameraConstraints, M as recognizeText, ft as redactProgrammatically, t as resolveDocumentModelUrl, de as scanDocument, f as selectFinderGuidance, ie as selectFinderHint, o as setLicenseKey, he as simulatorQuad, ue as startCamera, se as startFinder, pe as torchControl };
|
|
1000
|
+
|
|
1001
|
+
//# sourceMappingURL=scanner-sdk.js.map
|