@scanfit/browser 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/THIRD_PARTY_NOTICES.md +27 -0
- package/dist/assets/processor.worker-ILg2T47t.js +1456 -0
- package/dist/chunks/geometry-57LNKdIq.js +37 -0
- package/dist/chunks/types-Bf5l1UNl.js +35 -0
- package/dist/core.js +572 -0
- package/dist/detector.js +580 -0
- package/dist/pdf.js +493 -0
- package/dist/react.js +993 -0
- package/dist/styles.css +1 -0
- package/dist/trigger.js +59 -0
- package/dist/types/core/canvas-bridge.d.ts +12 -0
- package/dist/types/core/geometry.d.ts +8 -0
- package/dist/types/core/headers.d.ts +10 -0
- package/dist/types/core/index.d.ts +20 -0
- package/dist/types/core/jpeg.d.ts +2 -0
- package/dist/types/core/processor.worker.d.ts +1 -0
- package/dist/types/core/protocol.d.ts +34 -0
- package/dist/types/core/types.d.ts +132 -0
- package/dist/types/core/worker-client.d.ts +13 -0
- package/dist/types/detector/index.d.ts +11 -0
- package/dist/types/detector/scanfit-classical.d.ts +4 -0
- package/dist/types/pdf/index.d.ts +25 -0
- package/dist/types/react/Camera.d.ts +6 -0
- package/dist/types/react/CornerEditor.d.ts +8 -0
- package/dist/types/react/DocumentScanner.d.ts +28 -0
- package/dist/types/react/hooks.d.ts +10 -0
- package/dist/types/react/index.d.ts +5 -0
- package/dist/types/react/messages.d.ts +77 -0
- package/dist/types/trigger/ScannerTrigger.d.ts +7 -0
- package/dist/types/trigger/index.d.ts +1 -0
- package/package.json +72 -0
package/dist/detector.js
ADDED
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import { v as _ } from "./chunks/geometry-57LNKdIq.js";
|
|
2
|
+
const O = 0.5, P = (t, n = 0, o = 1) => Math.max(n, Math.min(o, t)), V = () => typeof performance > "u" ? Date.now() : performance.now();
|
|
3
|
+
function T(t) {
|
|
4
|
+
const n = new Uint8Array(t.width * t.height);
|
|
5
|
+
for (let o = 0, r = 0; r < n.length; r++, o += 4) {
|
|
6
|
+
const e = t.data[o + 3] / 255, c = 77 * t.data[o] + 150 * t.data[o + 1] + 29 * t.data[o + 2] >> 8;
|
|
7
|
+
n[r] = Math.round(c * e + 255 * (1 - e));
|
|
8
|
+
}
|
|
9
|
+
return n;
|
|
10
|
+
}
|
|
11
|
+
function G(t, n, o) {
|
|
12
|
+
const r = Math.max(n, o) >= 600 ? 2 : 1, e = r * 2 + 1, c = new Uint8Array(t.length), a = new Uint8Array(t.length);
|
|
13
|
+
for (let s = 0; s < o; s++) {
|
|
14
|
+
const h = s * n;
|
|
15
|
+
for (let u = 0; u < n; u++) {
|
|
16
|
+
let f = 0;
|
|
17
|
+
for (let m = -r; m <= r; m++)
|
|
18
|
+
f += t[h + Math.max(0, Math.min(n - 1, u + m))];
|
|
19
|
+
c[h + u] = Math.round(f / e);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
for (let s = 0; s < o; s++)
|
|
23
|
+
for (let h = 0; h < n; h++) {
|
|
24
|
+
let u = 0;
|
|
25
|
+
for (let f = -r; f <= r; f++)
|
|
26
|
+
u += c[Math.max(0, Math.min(o - 1, s + f)) * n + h];
|
|
27
|
+
a[s * n + h] = Math.round(u / e);
|
|
28
|
+
}
|
|
29
|
+
return a;
|
|
30
|
+
}
|
|
31
|
+
function J(t, n, o) {
|
|
32
|
+
const r = new Uint16Array(t.length), e = new Uint32Array(2041);
|
|
33
|
+
let c = 0;
|
|
34
|
+
for (let u = 1; u < o - 1; u++)
|
|
35
|
+
for (let f = 1; f < n - 1; f++) {
|
|
36
|
+
const m = u * n + f, M = -t[m - n - 1] + t[m - n + 1] - 2 * t[m - 1] + 2 * t[m + 1] - t[m + n - 1] + t[m + n + 1], I = -t[m - n - 1] - 2 * t[m - n] - t[m - n + 1] + t[m + n - 1] + 2 * t[m + n] + t[m + n + 1], d = Math.min(2040, Math.abs(M) + Math.abs(I));
|
|
37
|
+
r[m] = d, d && (e[d]++, c++);
|
|
38
|
+
}
|
|
39
|
+
let a = 0;
|
|
40
|
+
const s = c * 0.82;
|
|
41
|
+
let h = 0;
|
|
42
|
+
for (let u = 1; u < e.length; u++)
|
|
43
|
+
if (a += e[u], a >= s) {
|
|
44
|
+
h = u;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
magnitude: r,
|
|
49
|
+
threshold: Math.round(P(h || 24, 24, 600)),
|
|
50
|
+
nonzero: c
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function K(t, n, o, r) {
|
|
54
|
+
const e = new Uint8Array(t.length), c = new Int32Array(t.length), a = r * 0.42;
|
|
55
|
+
let s = 0, h = 0, u = 0;
|
|
56
|
+
for (let M = 0; M < t.length; M++)
|
|
57
|
+
t[M] >= r ? (e[M] = 2, c[h++] = M, u++) : t[M] >= a && (e[M] = 1);
|
|
58
|
+
for (; s < h; ) {
|
|
59
|
+
const M = c[s++], I = M % n, d = Math.floor(M / n);
|
|
60
|
+
for (let y = -1; y <= 1; y++)
|
|
61
|
+
for (let b = -1; b <= 1; b++) {
|
|
62
|
+
if (!b && !y || I + b < 0 || I + b >= n || d + y < 0 || d + y >= o)
|
|
63
|
+
continue;
|
|
64
|
+
const A = M + y * n + b;
|
|
65
|
+
e[A] === 1 && (e[A] = 2, c[h++] = A);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const f = new Uint8Array(e.length);
|
|
69
|
+
let m = 0;
|
|
70
|
+
for (let M = 0; M < e.length; M++)
|
|
71
|
+
e[M] === 2 && (f[M] = 1, m++);
|
|
72
|
+
return { binary: f, density: m / f.length, strong: u };
|
|
73
|
+
}
|
|
74
|
+
function R(t, n, o) {
|
|
75
|
+
const r = new Uint8Array(t.length);
|
|
76
|
+
for (let e = 1; e < o - 1; e++)
|
|
77
|
+
for (let c = 1; c < n - 1; c++) {
|
|
78
|
+
const a = e * n + c;
|
|
79
|
+
(t[a] || t[a - 1] || t[a + 1] || t[a - n] || t[a + n] || t[a - n - 1] || t[a - n + 1] || t[a + n - 1] || t[a + n + 1]) && (r[a] = 1);
|
|
80
|
+
}
|
|
81
|
+
return r;
|
|
82
|
+
}
|
|
83
|
+
function W(t) {
|
|
84
|
+
const n = new Uint32Array(256);
|
|
85
|
+
for (const s of t) n[s]++;
|
|
86
|
+
let o = 0;
|
|
87
|
+
for (let s = 0; s < 256; s++) o += s * n[s];
|
|
88
|
+
let r = 0, e = 0, c = -1, a = 127;
|
|
89
|
+
for (let s = 0; s < 255; s++) {
|
|
90
|
+
r += n[s], e += s * n[s];
|
|
91
|
+
const h = t.length - r;
|
|
92
|
+
if (!r || !h) continue;
|
|
93
|
+
const u = e / r - (o - e) / h, f = r * h * u * u;
|
|
94
|
+
f > c && (c = f, a = s);
|
|
95
|
+
}
|
|
96
|
+
return a;
|
|
97
|
+
}
|
|
98
|
+
function Z(t, n, o) {
|
|
99
|
+
const r = new Uint32Array(256);
|
|
100
|
+
let e = 0;
|
|
101
|
+
const c = (f) => {
|
|
102
|
+
r[f]++, e++;
|
|
103
|
+
};
|
|
104
|
+
for (let f = 0; f < n; f++)
|
|
105
|
+
c(t[f]), c(t[(o - 1) * n + f]);
|
|
106
|
+
for (let f = 1; f < o - 1; f++)
|
|
107
|
+
c(t[f * n]), c(t[f * n + n - 1]);
|
|
108
|
+
let a = 0, s = 127;
|
|
109
|
+
for (let f = 0; f < r.length; f++)
|
|
110
|
+
if (a += r[f], a >= e / 2) {
|
|
111
|
+
s = f;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
const h = new Uint32Array(256);
|
|
115
|
+
for (let f = 0; f < r.length; f++)
|
|
116
|
+
h[Math.abs(f - s)] += r[f];
|
|
117
|
+
a = 0;
|
|
118
|
+
let u = 0;
|
|
119
|
+
for (let f = 0; f < h.length; f++)
|
|
120
|
+
if (a += h[f], a >= e / 2) {
|
|
121
|
+
u = f;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
return { median: s, deviation: u };
|
|
125
|
+
}
|
|
126
|
+
function w(t, n, o) {
|
|
127
|
+
const r = W(t), e = Z(t, n, o), c = new Uint8Array(t.length), a = new Uint8Array(t.length), s = new Uint8Array(t.length), h = Math.max(16, e.deviation * 2.5);
|
|
128
|
+
for (let u = 0; u < t.length; u++)
|
|
129
|
+
c[u] = t[u] > r ? 1 : 0, a[u] = t[u] <= r ? 1 : 0, s[u] = Math.abs(t[u] - e.median) >= h ? 1 : 0;
|
|
130
|
+
return { bright: c, dark: a, contrast: s };
|
|
131
|
+
}
|
|
132
|
+
function tt(t, n, o, r, e) {
|
|
133
|
+
const c = new Uint8Array(t.length), a = new Int32Array(t.length), s = [], h = r ? Math.max(64, t.length * 0.025) : Math.max(40, (n + o) * 0.06);
|
|
134
|
+
for (let u = 0; u < t.length; u++) {
|
|
135
|
+
if (!t[u] || c[u]) continue;
|
|
136
|
+
let f = 0, m = 0, M = 0, I = 0, d = n, y = o, b = 0, A = 0, i = 0;
|
|
137
|
+
const l = [], C = new Array(8), U = [
|
|
138
|
+
1 / 0,
|
|
139
|
+
-1 / 0,
|
|
140
|
+
1 / 0,
|
|
141
|
+
-1 / 0,
|
|
142
|
+
1 / 0,
|
|
143
|
+
-1 / 0,
|
|
144
|
+
1 / 0,
|
|
145
|
+
-1 / 0
|
|
146
|
+
];
|
|
147
|
+
for (a[m++] = u, c[u] = 1; f < m; ) {
|
|
148
|
+
const g = a[f++], p = g % n, x = Math.floor(g / n);
|
|
149
|
+
M++, d = Math.min(d, p), y = Math.min(y, x), b = Math.max(b, p), A = Math.max(A, x), (p === 0 || p === n - 1 || x === 0 || x === o - 1) && i++;
|
|
150
|
+
const S = [p + x, p + x, p - x, p - x, p, p, x, x];
|
|
151
|
+
for (let v = 0; v < 8; v++)
|
|
152
|
+
(v % 2 ? S[v] > U[v] : S[v] < U[v]) && (U[v] = S[v], C[v] = { x: p, y: x });
|
|
153
|
+
let N = !1;
|
|
154
|
+
for (let v = -1; v <= 1; v++)
|
|
155
|
+
for (let k = -1; k <= 1; k++) {
|
|
156
|
+
if (!k && !v) continue;
|
|
157
|
+
const z = p + k, D = x + v;
|
|
158
|
+
if (z < 0 || z >= n || D < 0 || D >= o) {
|
|
159
|
+
N = !0;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const q = D * n + z;
|
|
163
|
+
t[q] ? c[q] || (c[q] = 1, a[m++] = q) : N = !0;
|
|
164
|
+
}
|
|
165
|
+
if (N)
|
|
166
|
+
if (I++, l.length < 2048) l.push({ x: p, y: x });
|
|
167
|
+
else {
|
|
168
|
+
const v = (Math.imul(I, 2654435761) >>> 0) % I | 0;
|
|
169
|
+
v < l.length && (l[v] = { x: p, y: x });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const F = (b - d + 1) * (A - y + 1);
|
|
173
|
+
if (M >= h && F >= t.length * 0.055 && M < t.length * 0.985) {
|
|
174
|
+
for (const g of C) g && l.push(g);
|
|
175
|
+
s.push({ count: M, boundsArea: F, touches: i, boundary: l });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return s.sort(
|
|
179
|
+
(u, f) => f.boundsArea * Math.min(1, f.count / h) - u.boundsArea * Math.min(1, u.count / h)
|
|
180
|
+
).slice(0, e);
|
|
181
|
+
}
|
|
182
|
+
const Q = (t, n, o) => (n.x - t.x) * (o.y - t.y) - (n.y - t.y) * (o.x - t.x);
|
|
183
|
+
function nt(t) {
|
|
184
|
+
const n = [...t].sort((e, c) => e.x - c.x || e.y - c.y).filter(
|
|
185
|
+
(e, c, a) => !c || e.x !== a[c - 1].x || e.y !== a[c - 1].y
|
|
186
|
+
);
|
|
187
|
+
if (n.length <= 4) return n;
|
|
188
|
+
const o = [], r = [];
|
|
189
|
+
for (const e of n) {
|
|
190
|
+
for (; o.length >= 2 && Q(o.at(-2), o.at(-1), e) <= 0; )
|
|
191
|
+
o.pop();
|
|
192
|
+
o.push(e);
|
|
193
|
+
}
|
|
194
|
+
for (let e = n.length - 1; e >= 0; e--) {
|
|
195
|
+
const c = n[e];
|
|
196
|
+
for (; r.length >= 2 && Q(r.at(-2), r.at(-1), c) <= 0; )
|
|
197
|
+
r.pop();
|
|
198
|
+
r.push(c);
|
|
199
|
+
}
|
|
200
|
+
return o.pop(), r.pop(), [...o, ...r];
|
|
201
|
+
}
|
|
202
|
+
function et(t) {
|
|
203
|
+
const n = [...t];
|
|
204
|
+
for (; n.length > 4; ) {
|
|
205
|
+
let o = 0, r = 1 / 0;
|
|
206
|
+
for (let e = 0; e < n.length; e++) {
|
|
207
|
+
const c = n[(e + n.length - 1) % n.length], a = n[e], s = n[(e + 1) % n.length], h = Math.abs(Q(c, a, s));
|
|
208
|
+
h < r && (r = h, o = e);
|
|
209
|
+
}
|
|
210
|
+
n.splice(o, 1);
|
|
211
|
+
}
|
|
212
|
+
return n;
|
|
213
|
+
}
|
|
214
|
+
function H(t, n) {
|
|
215
|
+
const o = Math.abs(t - n) % Math.PI;
|
|
216
|
+
return Math.min(o, Math.PI - o);
|
|
217
|
+
}
|
|
218
|
+
function j(t, n) {
|
|
219
|
+
const o = Math.cos(t.theta), r = Math.sin(t.theta), e = Math.cos(n.theta), c = Math.sin(n.theta), a = o * c - r * e;
|
|
220
|
+
return Math.abs(a) < 0.08 ? null : {
|
|
221
|
+
x: (t.rho * c - r * n.rho) / a,
|
|
222
|
+
y: (o * n.rho - t.rho * e) / a
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function ot(t, n, o, r) {
|
|
226
|
+
const e = [];
|
|
227
|
+
let c = 0;
|
|
228
|
+
for (let i = 1; i < r - 1; i++)
|
|
229
|
+
for (let l = 1; l < o - 1; l++)
|
|
230
|
+
if (!(t[i * o + l] < n))
|
|
231
|
+
if (c++, e.length < 5e3) e.push({ x: l, y: i });
|
|
232
|
+
else {
|
|
233
|
+
const C = (Math.imul(c, 2246822519) >>> 0) % c | 0;
|
|
234
|
+
C < e.length && (e[C] = { x: l, y: i });
|
|
235
|
+
}
|
|
236
|
+
if (e.length < 24) return [];
|
|
237
|
+
const a = 90, s = 2, h = Math.hypot(o, r), u = Math.ceil(h * 2 / s) + 1, f = new Uint16Array(a * u), m = new Float32Array(a), M = new Float32Array(a);
|
|
238
|
+
for (let i = 0; i < a; i++) {
|
|
239
|
+
const l = i * Math.PI / a;
|
|
240
|
+
m[i] = Math.cos(l), M[i] = Math.sin(l);
|
|
241
|
+
}
|
|
242
|
+
for (const i of e)
|
|
243
|
+
for (let l = 0; l < a; l++) {
|
|
244
|
+
const C = Math.round(
|
|
245
|
+
(i.x * m[l] + i.y * M[l] + h) / s
|
|
246
|
+
), U = l * u + C;
|
|
247
|
+
f[U] < 65535 && f[U]++;
|
|
248
|
+
}
|
|
249
|
+
const I = Math.max(10, Math.round(Math.min(o, r) * 0.055)), d = [];
|
|
250
|
+
for (let i = 0; i < a; i++)
|
|
251
|
+
for (let l = 1; l < u - 1; l++) {
|
|
252
|
+
const C = f[i * u + l];
|
|
253
|
+
C < I || d.push({
|
|
254
|
+
theta: i * Math.PI / a,
|
|
255
|
+
rho: l * s - h,
|
|
256
|
+
votes: C
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
d.sort((i, l) => l.votes - i.votes);
|
|
260
|
+
const y = [];
|
|
261
|
+
for (const i of d)
|
|
262
|
+
if (!y.some(
|
|
263
|
+
(l) => H(i.theta, l.theta) < 4 * Math.PI / a && Math.abs(i.rho - l.rho) < s * 5
|
|
264
|
+
) && (y.push(i), y.length >= 28))
|
|
265
|
+
break;
|
|
266
|
+
const b = [];
|
|
267
|
+
for (let i = 0; i < y.length; i++)
|
|
268
|
+
for (let l = i + 1; l < y.length; l++) {
|
|
269
|
+
const C = Math.abs(y[i].rho - y[l].rho), U = H(y[i].theta, y[l].theta);
|
|
270
|
+
U <= 16 * Math.PI / 180 && C >= Math.min(o, r) * 0.16 && b.push({
|
|
271
|
+
lines: [y[i], y[l]],
|
|
272
|
+
score: y[i].votes + y[l].votes - U * 180 / Math.PI
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
b.sort((i, l) => l.score - i.score);
|
|
276
|
+
const A = [];
|
|
277
|
+
for (let i = 0; i < Math.min(16, b.length); i++)
|
|
278
|
+
for (let l = i + 1; l < Math.min(16, b.length); l++) {
|
|
279
|
+
if (H(
|
|
280
|
+
b[i].lines[0].theta,
|
|
281
|
+
b[l].lines[0].theta
|
|
282
|
+
) < 38 * Math.PI / 180) continue;
|
|
283
|
+
const [U, F] = b[i].lines, [g, p] = b[l].lines, x = [
|
|
284
|
+
j(U, g),
|
|
285
|
+
j(F, g),
|
|
286
|
+
j(F, p),
|
|
287
|
+
j(U, p)
|
|
288
|
+
];
|
|
289
|
+
x.some(
|
|
290
|
+
(S) => !S || S.x < -o * 0.03 || S.x > o * 1.03 || S.y < -r * 0.03 || S.y > r * 1.03
|
|
291
|
+
) || A.push({
|
|
292
|
+
points: x,
|
|
293
|
+
source: "hough-lines",
|
|
294
|
+
score: b[i].score + b[l].score
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return A.sort((i, l) => l.score - i.score).slice(0, 20).map(({ points: i, source: l }) => ({ points: i, source: l }));
|
|
298
|
+
}
|
|
299
|
+
function rt(t) {
|
|
300
|
+
return t.length < 4 ? [] : [
|
|
301
|
+
(o) => o.x + o.y,
|
|
302
|
+
(o) => -(o.x - o.y),
|
|
303
|
+
(o) => -(o.x + o.y),
|
|
304
|
+
(o) => o.x - o.y
|
|
305
|
+
].map(
|
|
306
|
+
(o) => t.reduce(
|
|
307
|
+
(r, e) => o(e) < o(r) ? e : r
|
|
308
|
+
)
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
function at(t, n, o) {
|
|
312
|
+
if (t.length !== 4 || new Set(t.map((s) => `${s.x},${s.y}`)).size !== 4)
|
|
313
|
+
return null;
|
|
314
|
+
const r = t.reduce(
|
|
315
|
+
(s, h) => ({
|
|
316
|
+
x: s.x + h.x / 4,
|
|
317
|
+
y: s.y + h.y / 4
|
|
318
|
+
}),
|
|
319
|
+
{ x: 0, y: 0 }
|
|
320
|
+
);
|
|
321
|
+
let e = [...t].sort(
|
|
322
|
+
(s, h) => Math.atan2(s.y - r.y, s.x - r.x) - Math.atan2(h.y - r.y, h.x - r.x)
|
|
323
|
+
), c = 0;
|
|
324
|
+
for (let s = 1; s < 4; s++)
|
|
325
|
+
e[s].x + e[s].y < e[c].x + e[c].y && (c = s);
|
|
326
|
+
e = [...e.slice(c), ...e.slice(0, c)], Q(e[0], e[1], e[2]) < 0 && (e = [e[0], e[3], e[2], e[1]]);
|
|
327
|
+
const a = e.map((s) => ({
|
|
328
|
+
x: P(s.x / Math.max(1, n - 1)),
|
|
329
|
+
y: P(s.y / Math.max(1, o - 1))
|
|
330
|
+
}));
|
|
331
|
+
try {
|
|
332
|
+
return _(a), a;
|
|
333
|
+
} catch {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
function st(t) {
|
|
338
|
+
let n = 0;
|
|
339
|
+
for (let o = 0; o < 4; o++) {
|
|
340
|
+
const r = t[o], e = t[(o + 1) % 4];
|
|
341
|
+
n += r.x * e.y - e.x * r.y;
|
|
342
|
+
}
|
|
343
|
+
return Math.abs(n) / 2;
|
|
344
|
+
}
|
|
345
|
+
function L(t, n, o, r, e) {
|
|
346
|
+
const c = Math.max(0, Math.min(n - 1, Math.round(r))), a = Math.max(0, Math.min(o - 1, Math.round(e)));
|
|
347
|
+
return t[a * n + c];
|
|
348
|
+
}
|
|
349
|
+
function ct(t, n, o, r, e, c) {
|
|
350
|
+
const a = st(t);
|
|
351
|
+
if (a < 0.055 || a > 0.985) return null;
|
|
352
|
+
const s = t.map((g) => ({
|
|
353
|
+
x: g.x * (e - 1),
|
|
354
|
+
y: g.y * (c - 1)
|
|
355
|
+
})), h = s.map((g, p) => {
|
|
356
|
+
const x = s[(p + 1) % 4];
|
|
357
|
+
return {
|
|
358
|
+
dx: x.x - g.x,
|
|
359
|
+
dy: x.y - g.y,
|
|
360
|
+
length: Math.hypot(x.x - g.x, x.y - g.y)
|
|
361
|
+
};
|
|
362
|
+
}), u = Math.hypot(e, c), f = Math.min(...h.map((g) => g.length)) / u;
|
|
363
|
+
if (f < 0.055) return null;
|
|
364
|
+
let m = 0;
|
|
365
|
+
for (let g = 0; g < 4; g++) {
|
|
366
|
+
const p = s[(g + 3) % 4], x = s[g], S = s[(g + 1) % 4], N = p.x - x.x, v = p.y - x.y, k = S.x - x.x, z = S.y - x.y, D = Math.abs(
|
|
367
|
+
(N * k + v * z) / Math.max(1, Math.hypot(N, v) * Math.hypot(k, z))
|
|
368
|
+
);
|
|
369
|
+
m += 1 - P(D / 0.72);
|
|
370
|
+
}
|
|
371
|
+
m /= 4;
|
|
372
|
+
let M = 0, I = 0, d = 0, y = 0;
|
|
373
|
+
for (let g = 0; g < 4; g++) {
|
|
374
|
+
const p = s[g], x = h[g], S = Math.max(12, Math.min(180, Math.round(x.length / 2))), N = -x.dy / Math.max(1, x.length), v = x.dx / Math.max(1, x.length);
|
|
375
|
+
for (let k = 1; k < S; k++) {
|
|
376
|
+
const z = k / S, D = p.x + x.dx * z, q = p.y + x.dy * z;
|
|
377
|
+
let E = 0;
|
|
378
|
+
for (let $ = -2; $ <= 2; $++)
|
|
379
|
+
E = Math.max(
|
|
380
|
+
E,
|
|
381
|
+
L(
|
|
382
|
+
o,
|
|
383
|
+
e,
|
|
384
|
+
c,
|
|
385
|
+
D + N * $,
|
|
386
|
+
q + v * $
|
|
387
|
+
)
|
|
388
|
+
);
|
|
389
|
+
M += E >= r * 0.7 ? 1 : 0, I += P(E / Math.max(1, r * 1.8));
|
|
390
|
+
const X = L(
|
|
391
|
+
n,
|
|
392
|
+
e,
|
|
393
|
+
c,
|
|
394
|
+
D + N * 4,
|
|
395
|
+
q + v * 4
|
|
396
|
+
), Y = L(
|
|
397
|
+
n,
|
|
398
|
+
e,
|
|
399
|
+
c,
|
|
400
|
+
D - N * 4,
|
|
401
|
+
q - v * 4
|
|
402
|
+
);
|
|
403
|
+
d += P(Math.abs(X - Y) / 48), y++;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const b = y ? 0.67 * (M / y) + 0.33 * (I / y) : 0;
|
|
407
|
+
d = y ? d / y : 0;
|
|
408
|
+
const A = P((a - 0.055) / 0.3) * P((0.985 - a) / 0.08), i = P((f - 0.055) / 0.18), l = 0.72 * m + 0.28 * i, C = t.reduce(
|
|
409
|
+
(g, p) => g + P(
|
|
410
|
+
Math.min(p.x, p.y, 1 - p.x, 1 - p.y) / 0.025
|
|
411
|
+
),
|
|
412
|
+
0
|
|
413
|
+
) / 4, F = (0.43 * b + 0.2 * d + 0.15 * A + 0.17 * l + 0.05 * i) * (0.56 + 0.44 * C);
|
|
414
|
+
return {
|
|
415
|
+
score: F,
|
|
416
|
+
confidence: P((F - 0.3) / 0.55),
|
|
417
|
+
coverage: a,
|
|
418
|
+
edgeSupport: b,
|
|
419
|
+
contrast: d,
|
|
420
|
+
rectangularity: l
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function B(t, n, o, r, e, c, a) {
|
|
424
|
+
return {
|
|
425
|
+
engine: "scanfit-classical",
|
|
426
|
+
confidence: n,
|
|
427
|
+
candidateCount: o,
|
|
428
|
+
edgeThreshold: r,
|
|
429
|
+
edgeDensity: e,
|
|
430
|
+
durationMs: Math.max(0, V() - t),
|
|
431
|
+
coverage: c?.coverage ?? 0,
|
|
432
|
+
edgeSupport: c?.edgeSupport ?? 0,
|
|
433
|
+
contrast: c?.contrast ?? 0,
|
|
434
|
+
rectangularity: c?.rectangularity ?? 0,
|
|
435
|
+
fallbackReason: a
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function it(t, n = {}) {
|
|
439
|
+
const o = V(), r = t.width, e = t.height;
|
|
440
|
+
if (!Number.isSafeInteger(r) || !Number.isSafeInteger(e) || r < 32 || e < 32 || t.data.length !== r * e * 4)
|
|
441
|
+
return {
|
|
442
|
+
corners: null,
|
|
443
|
+
confidence: 0,
|
|
444
|
+
diagnostics: B(
|
|
445
|
+
o,
|
|
446
|
+
0,
|
|
447
|
+
0,
|
|
448
|
+
0,
|
|
449
|
+
0,
|
|
450
|
+
void 0,
|
|
451
|
+
"invalid-image"
|
|
452
|
+
)
|
|
453
|
+
};
|
|
454
|
+
const c = G(T(t), r, e), a = J(c, r, e), s = K(
|
|
455
|
+
a.magnitude,
|
|
456
|
+
r,
|
|
457
|
+
e,
|
|
458
|
+
a.threshold
|
|
459
|
+
);
|
|
460
|
+
if (a.nonzero < t.width + t.height || s.strong < 8)
|
|
461
|
+
return {
|
|
462
|
+
corners: null,
|
|
463
|
+
confidence: 0,
|
|
464
|
+
diagnostics: B(
|
|
465
|
+
o,
|
|
466
|
+
0,
|
|
467
|
+
0,
|
|
468
|
+
a.threshold,
|
|
469
|
+
s.density,
|
|
470
|
+
void 0,
|
|
471
|
+
"uniform-image"
|
|
472
|
+
)
|
|
473
|
+
};
|
|
474
|
+
const h = w(c, r, e), u = [
|
|
475
|
+
{
|
|
476
|
+
binary: R(s.binary, r, e),
|
|
477
|
+
source: "edges",
|
|
478
|
+
filled: !1
|
|
479
|
+
},
|
|
480
|
+
{ binary: h.bright, source: "bright-region", filled: !0 },
|
|
481
|
+
{ binary: h.dark, source: "dark-region", filled: !0 },
|
|
482
|
+
{ binary: h.contrast, source: "border-contrast", filled: !0 }
|
|
483
|
+
], f = Math.max(
|
|
484
|
+
1,
|
|
485
|
+
Math.min(
|
|
486
|
+
16,
|
|
487
|
+
Number.isSafeInteger(n.maxComponents) ? n.maxComponents : 8
|
|
488
|
+
)
|
|
489
|
+
), m = [];
|
|
490
|
+
m.push(
|
|
491
|
+
...ot(
|
|
492
|
+
a.magnitude,
|
|
493
|
+
a.threshold,
|
|
494
|
+
r,
|
|
495
|
+
e
|
|
496
|
+
)
|
|
497
|
+
);
|
|
498
|
+
for (const A of u)
|
|
499
|
+
for (const i of tt(
|
|
500
|
+
A.binary,
|
|
501
|
+
r,
|
|
502
|
+
e,
|
|
503
|
+
A.filled,
|
|
504
|
+
f
|
|
505
|
+
)) {
|
|
506
|
+
if (i.touches > i.count * 0.45 && i.boundsArea > r * e * 0.9)
|
|
507
|
+
continue;
|
|
508
|
+
const l = nt(i.boundary);
|
|
509
|
+
l.length >= 4 && (m.push({ points: et(l), source: A.source }), m.push({ points: rt(l), source: A.source }));
|
|
510
|
+
}
|
|
511
|
+
const M = Math.max(
|
|
512
|
+
1,
|
|
513
|
+
Math.min(
|
|
514
|
+
64,
|
|
515
|
+
Number.isSafeInteger(n.maxCandidates) ? n.maxCandidates : 24
|
|
516
|
+
)
|
|
517
|
+
), I = /* @__PURE__ */ new Map();
|
|
518
|
+
for (const A of m) {
|
|
519
|
+
const i = at(A.points, r, e);
|
|
520
|
+
if (!i) continue;
|
|
521
|
+
const l = i.map(
|
|
522
|
+
(C) => `${Math.round(C.x * 40)},${Math.round(C.y * 40)}`
|
|
523
|
+
).join(";");
|
|
524
|
+
if (I.has(l) || I.set(l, { quad: i, source: A.source }), I.size >= M) break;
|
|
525
|
+
}
|
|
526
|
+
let d;
|
|
527
|
+
for (const A of I.values()) {
|
|
528
|
+
const i = ct(
|
|
529
|
+
A.quad,
|
|
530
|
+
c,
|
|
531
|
+
a.magnitude,
|
|
532
|
+
a.threshold,
|
|
533
|
+
r,
|
|
534
|
+
e
|
|
535
|
+
);
|
|
536
|
+
i && (!d || i.score > d.metrics.score) && (d = { ...A, metrics: i });
|
|
537
|
+
}
|
|
538
|
+
const y = d?.metrics.confidence ?? 0, b = P(
|
|
539
|
+
Number.isFinite(n.minConfidence) ? n.minConfidence : O,
|
|
540
|
+
0.15,
|
|
541
|
+
0.95
|
|
542
|
+
);
|
|
543
|
+
return !d || y < b || d.metrics.edgeSupport < 0.28 ? {
|
|
544
|
+
corners: null,
|
|
545
|
+
confidence: y,
|
|
546
|
+
diagnostics: B(
|
|
547
|
+
o,
|
|
548
|
+
y,
|
|
549
|
+
I.size,
|
|
550
|
+
a.threshold,
|
|
551
|
+
s.density,
|
|
552
|
+
d?.metrics,
|
|
553
|
+
d ? "low-confidence" : "no-candidate"
|
|
554
|
+
)
|
|
555
|
+
} : {
|
|
556
|
+
corners: d.quad,
|
|
557
|
+
confidence: y,
|
|
558
|
+
diagnostics: {
|
|
559
|
+
...B(
|
|
560
|
+
o,
|
|
561
|
+
y,
|
|
562
|
+
I.size,
|
|
563
|
+
a.threshold,
|
|
564
|
+
s.density,
|
|
565
|
+
d.metrics
|
|
566
|
+
),
|
|
567
|
+
source: d.source
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
function ft(t = {}) {
|
|
572
|
+
return {
|
|
573
|
+
async detect(n) {
|
|
574
|
+
return it(n, t);
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
export {
|
|
579
|
+
ft as createDetector
|
|
580
|
+
};
|