scanic 0.1.4 → 0.1.5
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 +45 -87
- package/dist/scanic.js +177 -178
- package/dist/scanic.js.map +1 -1
- package/dist/scanic.umd.cjs +1 -1
- package/dist/scanic.umd.cjs.map +1 -1
- package/package.json +1 -1
package/dist/scanic.js
CHANGED
|
@@ -22,57 +22,57 @@ const K = {
|
|
|
22
22
|
];
|
|
23
23
|
function V(I, A = {}) {
|
|
24
24
|
const g = A.width || Math.sqrt(I.length), C = A.height || I.length / g, E = A.mode !== void 0 ? A.mode : z, B = A.method !== void 0 ? A.method : x, Q = A.minArea || K.MIN_CONTOUR_AREA, i = g + 2, s = C + 2, t = new Int32Array(i * s);
|
|
25
|
-
for (let
|
|
25
|
+
for (let a = 0; a < C; a++)
|
|
26
26
|
for (let D = 0; D < g; D++)
|
|
27
|
-
I[
|
|
28
|
-
const
|
|
29
|
-
let
|
|
30
|
-
for (let
|
|
27
|
+
I[a * g + D] > 0 && (t[(a + 1) * i + (D + 1)] = 1);
|
|
28
|
+
const o = [];
|
|
29
|
+
let n = 2;
|
|
30
|
+
for (let a = 1; a <= C; a++)
|
|
31
31
|
for (let D = 1; D <= g; D++) {
|
|
32
|
-
const h = t[
|
|
32
|
+
const h = t[a * i + D], y = t[a * i + (D - 1)];
|
|
33
33
|
let c = null, r = !1, w = -1;
|
|
34
|
-
if (h === 1 && y === 0 ? (r = !0, c = { x: D, y:
|
|
34
|
+
if (h === 1 && y === 0 ? (r = !0, c = { x: D, y: a }, w = 2) : h === 0 && y >= 1 && y !== -1 && y === 1 && (r = !1, c = { x: D - 1, y: a }, w = 6), c) {
|
|
35
35
|
if (E === O && !r) {
|
|
36
36
|
t[c.y * i + c.x] = -1;
|
|
37
37
|
continue;
|
|
38
38
|
}
|
|
39
|
-
const F =
|
|
39
|
+
const F = n++, R = _(t, i, s, c, w, F);
|
|
40
40
|
if (R && R.length > 0) {
|
|
41
|
-
let
|
|
42
|
-
B === x && (
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
41
|
+
let d = R;
|
|
42
|
+
B === x && (d = $(R));
|
|
43
|
+
const S = d.map((G) => ({ x: G.x - 1, y: G.y - 1 }));
|
|
44
|
+
if (S.length >= (B === x ? 4 : K.MIN_CONTOUR_POINTS)) {
|
|
45
45
|
const G = {
|
|
46
46
|
id: F,
|
|
47
|
-
points:
|
|
47
|
+
points: S,
|
|
48
48
|
isOuter: r
|
|
49
49
|
// Calculate area and bounding box later if needed for filtering/sorting
|
|
50
50
|
};
|
|
51
|
-
|
|
51
|
+
o.push(G);
|
|
52
52
|
}
|
|
53
53
|
} else
|
|
54
54
|
t[c.y * i + c.x] === 1 && (t[c.y * i + c.x] = F);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
o.forEach((a) => {
|
|
58
|
+
a.area = AA(a.points), a.boundingBox = IA(a.points);
|
|
59
59
|
});
|
|
60
|
-
const
|
|
61
|
-
return
|
|
60
|
+
const e = o.filter((a) => a.area >= Q);
|
|
61
|
+
return e.sort((a, D) => D.area - a.area), A.debug && (A.debug.labels = t, A.debug.rawContours = o, A.debug.finalContours = e), e;
|
|
62
62
|
}
|
|
63
63
|
function _(I, A, g, C, E, B) {
|
|
64
64
|
const Q = [], i = /* @__PURE__ */ new Set();
|
|
65
65
|
let s = { ...C }, t = -1;
|
|
66
66
|
I[C.y * A + C.x] = B;
|
|
67
|
-
let
|
|
68
|
-
const
|
|
69
|
-
for (;
|
|
70
|
-
let
|
|
67
|
+
let o = 0;
|
|
68
|
+
const n = A * g;
|
|
69
|
+
for (; o++ < n; ) {
|
|
70
|
+
let e;
|
|
71
71
|
if (t === -1) {
|
|
72
72
|
let h = !1;
|
|
73
73
|
for (let y = 0; y < 8; y++) {
|
|
74
|
-
|
|
75
|
-
const c = s.x + Y[
|
|
74
|
+
e = (E + y) % 8;
|
|
75
|
+
const c = s.x + Y[e].dx, r = s.y + Y[e].dy;
|
|
76
76
|
if (c >= 0 && c < A && r >= 0 && r < g && I[r * A + c] > 0) {
|
|
77
77
|
h = !0;
|
|
78
78
|
break;
|
|
@@ -80,34 +80,34 @@ function _(I, A, g, C, E, B) {
|
|
|
80
80
|
}
|
|
81
81
|
if (!h) return null;
|
|
82
82
|
} else
|
|
83
|
-
|
|
84
|
-
let
|
|
83
|
+
e = (t + 2) % 8;
|
|
84
|
+
let a = null;
|
|
85
85
|
for (let h = 0; h < 8; h++) {
|
|
86
|
-
const y = (
|
|
86
|
+
const y = (e + h) % 8, c = s.x + Y[y].dx, r = s.y + Y[y].dy;
|
|
87
87
|
if (c >= 0 && c < A && r >= 0 && r < g && I[r * A + c] > 0) {
|
|
88
|
-
|
|
88
|
+
a = { x: c, y: r }, t = (y + 4) % 8;
|
|
89
89
|
break;
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
if (!
|
|
92
|
+
if (!a) {
|
|
93
93
|
Q.length === 0 && Q.push({ ...s }), console.warn(`Contour tracing stopped unexpectedly at (${s.x - 1}, ${s.y - 1}) for contour ${B}`);
|
|
94
94
|
break;
|
|
95
95
|
}
|
|
96
96
|
const D = `${s.x},${s.y}`;
|
|
97
97
|
if (i.has(D))
|
|
98
98
|
return Q;
|
|
99
|
-
if (Q.push({ ...s }), i.add(D), I[
|
|
99
|
+
if (Q.push({ ...s }), i.add(D), I[a.y * A + a.x] === 1 && (I[a.y * A + a.x] = B), s = a, s.x === C.x && s.y === C.y)
|
|
100
100
|
break;
|
|
101
101
|
}
|
|
102
|
-
return
|
|
102
|
+
return o >= n ? (console.warn(`Contour tracing exceeded max steps for contour ${B}`), null) : Q;
|
|
103
103
|
}
|
|
104
104
|
function $(I) {
|
|
105
105
|
if (I.length <= 2)
|
|
106
106
|
return I;
|
|
107
107
|
const A = [], g = I.length;
|
|
108
108
|
for (let C = 0; C < g; C++) {
|
|
109
|
-
const E = I[(C + g - 1) % g], B = I[C], Q = I[(C + 1) % g], i = B.x - E.x, s = B.y - E.y, t = Q.x - B.x,
|
|
110
|
-
i *
|
|
109
|
+
const E = I[(C + g - 1) % g], B = I[C], Q = I[(C + 1) % g], i = B.x - E.x, s = B.y - E.y, t = Q.x - B.x, o = Q.y - B.y;
|
|
110
|
+
i * o !== s * t && A.push(B);
|
|
111
111
|
}
|
|
112
112
|
if (A.length === 0 && g > 0) {
|
|
113
113
|
if (g === 1) return [I[0]];
|
|
@@ -166,8 +166,8 @@ function gA(I, A, g) {
|
|
|
166
166
|
const Q = ((I.x - A.x) * C + (I.y - A.y) * E) / B;
|
|
167
167
|
let i, s;
|
|
168
168
|
Q < 0 ? (i = A.x, s = A.y) : Q > 1 ? (i = g.x, s = g.y) : (i = A.x + Q * C, s = A.y + Q * E);
|
|
169
|
-
const t = I.x - i,
|
|
170
|
-
return Math.sqrt(t * t +
|
|
169
|
+
const t = I.x - i, o = I.y - s;
|
|
170
|
+
return Math.sqrt(t * t + o * o);
|
|
171
171
|
}
|
|
172
172
|
function BA(I, A = 0.02) {
|
|
173
173
|
const g = QA(I), C = A * g;
|
|
@@ -203,8 +203,8 @@ function iA(I) {
|
|
|
203
203
|
if (!I || I.length === 0) return null;
|
|
204
204
|
let A = I[0], g = I[0], C = I[0], E = I[0], B = A.x + A.y, Q = g.x - g.y, i = C.x + C.y, s = E.x - E.y;
|
|
205
205
|
for (let t = 1; t < I.length; t++) {
|
|
206
|
-
const
|
|
207
|
-
|
|
206
|
+
const o = I[t], n = o.x + o.y, e = o.x - o.y;
|
|
207
|
+
n < B && (B = n, A = o), n > i && (i = n, C = o), e > Q && (Q = e, g = o), e < s && (s = e, E = o);
|
|
208
208
|
}
|
|
209
209
|
return {
|
|
210
210
|
topLeft: A,
|
|
@@ -259,12 +259,12 @@ let L = null;
|
|
|
259
259
|
function Z() {
|
|
260
260
|
return (L === null || L.byteLength === 0) && (L = new Float32Array(N.memory.buffer)), L;
|
|
261
261
|
}
|
|
262
|
-
function
|
|
262
|
+
function oA(I, A) {
|
|
263
263
|
const g = A(I.length * 4, 4) >>> 0;
|
|
264
264
|
return Z().set(I, g / 4), k = I.length, g;
|
|
265
265
|
}
|
|
266
|
-
function
|
|
267
|
-
const B =
|
|
266
|
+
function eA(I, A, g, C, E) {
|
|
267
|
+
const B = oA(I, N.__wbindgen_malloc), Q = k, i = N.hysteresis_thresholding(B, Q, A, g, C, E);
|
|
268
268
|
var s = b(i[0], i[1]).slice();
|
|
269
269
|
return N.__wbindgen_free(i[0], i[1] * 1, 1), s;
|
|
270
270
|
}
|
|
@@ -286,8 +286,8 @@ function DA(I, A) {
|
|
|
286
286
|
}
|
|
287
287
|
function cA(I, A, g, C, E) {
|
|
288
288
|
const B = p(I, N.__wbindgen_malloc), Q = k, i = p(A, N.__wbindgen_malloc), s = k, t = N.non_maximum_suppression(B, Q, i, s, g, C, E);
|
|
289
|
-
var
|
|
290
|
-
return N.__wbindgen_free(t[0], t[1] * 4, 4),
|
|
289
|
+
var o = DA(t[0], t[1]).slice();
|
|
290
|
+
return N.__wbindgen_free(t[0], t[1] * 4, 4), o;
|
|
291
291
|
}
|
|
292
292
|
async function hA(I, A) {
|
|
293
293
|
if (typeof Response == "function" && I instanceof Response) {
|
|
@@ -336,24 +336,24 @@ function NA(I, A, g, C = 5, E = 0) {
|
|
|
336
336
|
E === 0 && (E = 0.3 * ((C - 1) * 0.5 - 1) + 0.8);
|
|
337
337
|
const B = Math.floor(C / 2), Q = FA(C, E), i = new Uint8ClampedArray(A * g), s = new Uint8ClampedArray(A * g);
|
|
338
338
|
for (let t = 0; t < g; t++) {
|
|
339
|
-
const
|
|
340
|
-
for (let
|
|
341
|
-
let
|
|
342
|
-
for (let
|
|
343
|
-
const D = Math.min(A - 1, Math.max(0,
|
|
344
|
-
|
|
339
|
+
const o = t * A;
|
|
340
|
+
for (let n = 0; n < A; n++) {
|
|
341
|
+
let e = 0;
|
|
342
|
+
for (let a = -B; a <= B; a++) {
|
|
343
|
+
const D = Math.min(A - 1, Math.max(0, n + a));
|
|
344
|
+
e += I[o + D] * Q[B + a];
|
|
345
345
|
}
|
|
346
|
-
i[
|
|
346
|
+
i[o + n] = e;
|
|
347
347
|
}
|
|
348
348
|
}
|
|
349
349
|
for (let t = 0; t < A; t++)
|
|
350
|
-
for (let
|
|
351
|
-
let
|
|
352
|
-
for (let
|
|
353
|
-
const
|
|
354
|
-
|
|
350
|
+
for (let o = 0; o < g; o++) {
|
|
351
|
+
let n = 0;
|
|
352
|
+
for (let e = -B; e <= B; e++) {
|
|
353
|
+
const a = Math.min(g - 1, Math.max(0, o + e));
|
|
354
|
+
n += i[a * A + t] * Q[B + e];
|
|
355
355
|
}
|
|
356
|
-
s[
|
|
356
|
+
s[o * A + t] = Math.round(n);
|
|
357
357
|
}
|
|
358
358
|
return s;
|
|
359
359
|
}
|
|
@@ -373,8 +373,8 @@ function RA(I, A, g) {
|
|
|
373
373
|
for (let B = 1; B < g - 1; B++) {
|
|
374
374
|
const Q = B * A, i = (B - 1) * A, s = (B + 1) * A;
|
|
375
375
|
for (let t = 1; t < A - 1; t++) {
|
|
376
|
-
const
|
|
377
|
-
C[
|
|
376
|
+
const o = Q + t, n = I[i + t - 1], e = I[i + t], a = I[i + t + 1], D = I[Q + t - 1], h = I[Q + t + 1], y = I[s + t - 1], c = I[s + t], r = I[s + t + 1], w = a - n + 2 * (h - D) + (r - y), F = y + 2 * c + r - (n + 2 * e + a);
|
|
377
|
+
C[o] = w, E[o] = F;
|
|
378
378
|
}
|
|
379
379
|
}
|
|
380
380
|
return { dx: C, dy: E };
|
|
@@ -387,32 +387,32 @@ function lA(I, A, g, C, E) {
|
|
|
387
387
|
}
|
|
388
388
|
for (let i = 1; i < C - 1; i++)
|
|
389
389
|
for (let s = 1; s < g - 1; s++) {
|
|
390
|
-
const t = i * g + s,
|
|
391
|
-
if (
|
|
390
|
+
const t = i * g + s, o = B[t];
|
|
391
|
+
if (o === 0) {
|
|
392
392
|
Q[t] = 0;
|
|
393
393
|
continue;
|
|
394
394
|
}
|
|
395
|
-
const
|
|
396
|
-
let
|
|
397
|
-
const h = Math.abs(
|
|
395
|
+
const n = I[t], e = A[t];
|
|
396
|
+
let a = 0, D = 0;
|
|
397
|
+
const h = Math.abs(n), y = Math.abs(e);
|
|
398
398
|
if (y > h * 2.4142)
|
|
399
|
-
|
|
399
|
+
a = B[t - g], D = B[t + g];
|
|
400
400
|
else if (h > y * 2.4142)
|
|
401
|
-
|
|
401
|
+
a = B[t - 1], D = B[t + 1];
|
|
402
402
|
else {
|
|
403
|
-
const c = (
|
|
404
|
-
|
|
403
|
+
const c = (n ^ e) < 0 ? -1 : 1;
|
|
404
|
+
e > 0 ? (a = B[(i - 1) * g + (s - c)], D = B[(i + 1) * g + (s + c)]) : (a = B[(i + 1) * g + (s - c)], D = B[(i - 1) * g + (s + c)]), n > 0 && e > 0 || n < 0 && e < 0 ? (a = B[(i - 1) * g + (s + 1)], D = B[(i + 1) * g + (s - 1)]) : (a = B[(i - 1) * g + (s - 1)], D = B[(i + 1) * g + (s + 1)]);
|
|
405
405
|
}
|
|
406
|
-
|
|
406
|
+
o >= a && o >= D ? Q[t] = o : Q[t] = 0;
|
|
407
407
|
}
|
|
408
408
|
return Q;
|
|
409
409
|
}
|
|
410
410
|
function v(I, A, g, C, E) {
|
|
411
411
|
const B = new Uint8Array(A * g), Q = [];
|
|
412
412
|
for (let t = 1; t < g - 1; t++)
|
|
413
|
-
for (let
|
|
414
|
-
const
|
|
415
|
-
|
|
413
|
+
for (let o = 1; o < A - 1; o++) {
|
|
414
|
+
const n = t * A + o, e = I[n];
|
|
415
|
+
e >= E ? (B[n] = 2, Q.push({ x: o, y: t })) : e >= C ? B[n] = 0 : B[n] = 1;
|
|
416
416
|
}
|
|
417
417
|
for (let t = 0; t < A; t++)
|
|
418
418
|
B[t] = 1, B[(g - 1) * A + t] = 1;
|
|
@@ -420,10 +420,10 @@ function v(I, A, g, C, E) {
|
|
|
420
420
|
B[t * A] = 1, B[t * A + A - 1] = 1;
|
|
421
421
|
const i = [-1, 0, 1, -1, 1, -1, 0, 1], s = [-1, -1, -1, 0, 0, 1, 1, 1];
|
|
422
422
|
for (; Q.length > 0; ) {
|
|
423
|
-
const { x: t, y:
|
|
424
|
-
for (let
|
|
425
|
-
const
|
|
426
|
-
B[D] === 0 && (B[D] = 2, Q.push({ x:
|
|
423
|
+
const { x: t, y: o } = Q.pop();
|
|
424
|
+
for (let n = 0; n < 8; n++) {
|
|
425
|
+
const e = t + i[n], a = o + s[n], D = a * A + e;
|
|
426
|
+
B[D] === 0 && (B[D] = 2, Q.push({ x: e, y: a }));
|
|
427
427
|
}
|
|
428
428
|
}
|
|
429
429
|
return B;
|
|
@@ -433,35 +433,35 @@ function GA(I, A, g, C = 5) {
|
|
|
433
433
|
for (let i = 0; i < g; i++) {
|
|
434
434
|
const s = i * A;
|
|
435
435
|
for (let t = 0; t < A; t++) {
|
|
436
|
-
let
|
|
437
|
-
for (let
|
|
438
|
-
const
|
|
439
|
-
if (
|
|
440
|
-
const
|
|
441
|
-
|
|
436
|
+
let o = 0;
|
|
437
|
+
for (let n = -E; n <= E; n++) {
|
|
438
|
+
const e = t + n;
|
|
439
|
+
if (e >= 0 && e < A) {
|
|
440
|
+
const a = I[s + e];
|
|
441
|
+
a > o && (o = a);
|
|
442
442
|
}
|
|
443
443
|
}
|
|
444
|
-
B[s + t] =
|
|
444
|
+
B[s + t] = o;
|
|
445
445
|
}
|
|
446
446
|
}
|
|
447
447
|
for (let i = 0; i < A; i++)
|
|
448
448
|
for (let s = 0; s < g; s++) {
|
|
449
449
|
let t = 0;
|
|
450
|
-
for (let
|
|
451
|
-
const
|
|
452
|
-
if (
|
|
453
|
-
const
|
|
454
|
-
|
|
450
|
+
for (let o = -E; o <= E; o++) {
|
|
451
|
+
const n = s + o;
|
|
452
|
+
if (n >= 0 && n < g) {
|
|
453
|
+
const e = B[n * A + i];
|
|
454
|
+
e > t && (t = e);
|
|
455
455
|
}
|
|
456
456
|
}
|
|
457
457
|
Q[s * A + i] = t;
|
|
458
458
|
}
|
|
459
459
|
return Q;
|
|
460
460
|
}
|
|
461
|
-
async function
|
|
461
|
+
async function SA(I, A = {}) {
|
|
462
462
|
const g = [], C = performance.now(), { width: E, height: B } = I;
|
|
463
463
|
let Q = A.lowThreshold !== void 0 ? A.lowThreshold : 75, i = A.highThreshold !== void 0 ? A.highThreshold : 200;
|
|
464
|
-
const s = A.kernelSize || 5, t = A.sigma || 0,
|
|
464
|
+
const s = A.kernelSize || 5, t = A.sigma || 0, o = A.L2gradient === void 0 ? !1 : A.L2gradient, n = A.applyDilation !== void 0 ? A.applyDilation : !0, e = A.dilationKernelSize || 5, a = A.useWasmHysteresis !== void 0 ? A.useWasmHysteresis : !1;
|
|
465
465
|
Q >= i && (console.warn(`Canny Edge Detector: lowThreshold (${Q}) should be lower than highThreshold (${i}). Swapping them.`), [Q, i] = [i, Q]);
|
|
466
466
|
let D = performance.now();
|
|
467
467
|
const h = wA(I);
|
|
@@ -483,46 +483,46 @@ async function dA(I, A = {}) {
|
|
|
483
483
|
y = performance.now(), g.push({ step: "Gradients", ms: (y - D).toFixed(2) }), D = performance.now();
|
|
484
484
|
let F;
|
|
485
485
|
try {
|
|
486
|
-
await u, F = await cA(r, w, E, B,
|
|
486
|
+
await u, F = await cA(r, w, E, B, o);
|
|
487
487
|
} catch {
|
|
488
|
-
F = lA(r, w, E, B,
|
|
488
|
+
F = lA(r, w, E, B, o);
|
|
489
489
|
}
|
|
490
490
|
y = performance.now(), g.push({ step: "Non-Max Suppression", ms: (y - D).toFixed(2) }), D = performance.now();
|
|
491
|
-
const R =
|
|
492
|
-
let
|
|
493
|
-
if (
|
|
491
|
+
const R = o ? Q * Q : Q, d = o ? i * i : i;
|
|
492
|
+
let S;
|
|
493
|
+
if (a)
|
|
494
494
|
try {
|
|
495
|
-
await u,
|
|
495
|
+
await u, S = eA(F, E, B, R, d);
|
|
496
496
|
} catch (l) {
|
|
497
|
-
console.warn("WASM hysteresis failed, falling back to JS:", l),
|
|
497
|
+
console.warn("WASM hysteresis failed, falling back to JS:", l), S = v(F, E, B, R, d);
|
|
498
498
|
}
|
|
499
499
|
else
|
|
500
|
-
|
|
500
|
+
S = v(F, E, B, R, d);
|
|
501
501
|
y = performance.now(), g.push({ step: "Hysteresis", ms: (y - D).toFixed(2) }), D = performance.now();
|
|
502
502
|
const G = new Uint8ClampedArray(E * B);
|
|
503
|
-
for (let l = 0; l <
|
|
504
|
-
G[l] =
|
|
503
|
+
for (let l = 0; l < S.length; l++)
|
|
504
|
+
G[l] = S[l] === 2 ? 255 : 0;
|
|
505
505
|
y = performance.now(), g.push({ step: "Binary Image", ms: (y - D).toFixed(2) }), D = performance.now();
|
|
506
506
|
let f = G;
|
|
507
|
-
if (
|
|
507
|
+
if (n)
|
|
508
508
|
try {
|
|
509
|
-
await u, f = sA(G, E, B,
|
|
509
|
+
await u, f = sA(G, E, B, e);
|
|
510
510
|
} catch {
|
|
511
|
-
f = GA(G, E, B,
|
|
511
|
+
f = GA(G, E, B, e);
|
|
512
512
|
}
|
|
513
513
|
if (y = performance.now(), g.push({ step: "Dilation", ms: (y - D).toFixed(2) }), A.debug) {
|
|
514
514
|
A.debug.dx = r, A.debug.dy = w;
|
|
515
515
|
const l = new Float32Array(E * B);
|
|
516
516
|
for (let J = 0; J < r.length; J++) {
|
|
517
517
|
const U = r[J], H = w[J];
|
|
518
|
-
l[J] =
|
|
518
|
+
l[J] = o ? Math.sqrt(U * U + H * H) : Math.abs(U) + Math.abs(H);
|
|
519
519
|
}
|
|
520
|
-
A.debug.magnitude = l, A.debug.suppressed = F, A.debug.edgeMap =
|
|
520
|
+
A.debug.magnitude = l, A.debug.suppressed = F, A.debug.edgeMap = S, A.debug.cannyEdges = G, A.debug.finalEdges = f, A.debug.timings = g;
|
|
521
521
|
}
|
|
522
522
|
const j = performance.now();
|
|
523
523
|
return g.unshift({ step: "Total", ms: (j - C).toFixed(2) }), console.table(g), f;
|
|
524
524
|
}
|
|
525
|
-
class
|
|
525
|
+
class dA {
|
|
526
526
|
constructor(A = {}) {
|
|
527
527
|
this.options = {
|
|
528
528
|
targetFPS: A.targetFPS || 10,
|
|
@@ -738,11 +738,11 @@ function fA(I, A = 800) {
|
|
|
738
738
|
};
|
|
739
739
|
const B = A / E, Q = Math.round(g * B), i = Math.round(C * B), s = document.createElement("canvas");
|
|
740
740
|
s.width = g, s.height = C, s.getContext("2d").putImageData(I, 0, 0);
|
|
741
|
-
const
|
|
742
|
-
|
|
743
|
-
const
|
|
744
|
-
return
|
|
745
|
-
scaledImageData:
|
|
741
|
+
const o = document.createElement("canvas");
|
|
742
|
+
o.width = Q, o.height = i;
|
|
743
|
+
const n = o.getContext("2d");
|
|
744
|
+
return n.imageSmoothingEnabled = !0, n.imageSmoothingQuality = "high", n.drawImage(s, 0, 0, g, C, 0, 0, Q, i), {
|
|
745
|
+
scaledImageData: n.getImageData(0, 0, Q, i),
|
|
746
746
|
scaleFactor: 1 / B,
|
|
747
747
|
// Return inverse for compatibility with existing code
|
|
748
748
|
originalDimensions: { width: g, height: C },
|
|
@@ -757,7 +757,7 @@ async function JA(I, A = {}) {
|
|
|
757
757
|
scaleFactor: B,
|
|
758
758
|
maxProcessingDimension: C
|
|
759
759
|
});
|
|
760
|
-
const { width: s, height: t } = E,
|
|
760
|
+
const { width: s, height: t } = E, o = await SA(E, {
|
|
761
761
|
lowThreshold: A.lowThreshold || 75,
|
|
762
762
|
// Match OpenCV values
|
|
763
763
|
highThreshold: A.highThreshold || 200,
|
|
@@ -766,32 +766,32 @@ async function JA(I, A = {}) {
|
|
|
766
766
|
// Match OpenCV value
|
|
767
767
|
dilationIterations: A.dilationIterations || 1,
|
|
768
768
|
debug: g
|
|
769
|
-
}),
|
|
769
|
+
}), n = V(o, {
|
|
770
770
|
minArea: (A.minArea || 1e3) / (B * B),
|
|
771
771
|
// Adjust minArea for scaled image
|
|
772
772
|
debug: g,
|
|
773
773
|
width: s,
|
|
774
774
|
height: t
|
|
775
775
|
});
|
|
776
|
-
if (!
|
|
776
|
+
if (!n || n.length === 0)
|
|
777
777
|
return console.log("No document detected"), {
|
|
778
778
|
success: !1,
|
|
779
779
|
message: "No document detected",
|
|
780
780
|
debug: g
|
|
781
781
|
};
|
|
782
|
-
const
|
|
782
|
+
const e = n[0], a = EA(e, {
|
|
783
783
|
epsilon: A.epsilon
|
|
784
784
|
// Pass epsilon for approximation
|
|
785
785
|
});
|
|
786
|
-
let D =
|
|
786
|
+
let D = a;
|
|
787
787
|
return B !== 1 && (D = {
|
|
788
|
-
topLeft: { x:
|
|
789
|
-
topRight: { x:
|
|
790
|
-
bottomRight: { x:
|
|
791
|
-
bottomLeft: { x:
|
|
788
|
+
topLeft: { x: a.topLeft.x * B, y: a.topLeft.y * B },
|
|
789
|
+
topRight: { x: a.topRight.x * B, y: a.topRight.y * B },
|
|
790
|
+
bottomRight: { x: a.bottomRight.x * B, y: a.bottomRight.y * B },
|
|
791
|
+
bottomLeft: { x: a.bottomLeft.x * B, y: a.bottomLeft.y * B }
|
|
792
792
|
}), {
|
|
793
793
|
success: !0,
|
|
794
|
-
contour:
|
|
794
|
+
contour: e,
|
|
795
795
|
corners: D,
|
|
796
796
|
debug: g
|
|
797
797
|
};
|
|
@@ -799,9 +799,9 @@ async function JA(I, A = {}) {
|
|
|
799
799
|
function MA(I, A) {
|
|
800
800
|
function g(s) {
|
|
801
801
|
const t = [];
|
|
802
|
-
for (let
|
|
803
|
-
const [
|
|
804
|
-
t.push([
|
|
802
|
+
for (let o = 0; o < 4; o++) {
|
|
803
|
+
const [n, e] = s[o];
|
|
804
|
+
t.push([n, e, 1, 0, 0, 0, -n * A[o][0], -e * A[o][0]]), t.push([0, 0, 0, n, e, 1, -n * A[o][1], -e * A[o][1]]);
|
|
805
805
|
}
|
|
806
806
|
return t;
|
|
807
807
|
}
|
|
@@ -816,25 +816,25 @@ function MA(I, A) {
|
|
|
816
816
|
A[3][1]
|
|
817
817
|
];
|
|
818
818
|
function B(s, t) {
|
|
819
|
-
const
|
|
820
|
-
for (let h = 0; h <
|
|
819
|
+
const o = s.length, n = s[0].length, e = s.map((h) => h.slice()), a = t.slice();
|
|
820
|
+
for (let h = 0; h < n; h++) {
|
|
821
821
|
let y = h;
|
|
822
|
-
for (let c = h + 1; c <
|
|
823
|
-
Math.abs(
|
|
824
|
-
[
|
|
825
|
-
for (let c = h + 1; c <
|
|
826
|
-
const r =
|
|
827
|
-
for (let w = h; w <
|
|
828
|
-
|
|
829
|
-
|
|
822
|
+
for (let c = h + 1; c < o; c++)
|
|
823
|
+
Math.abs(e[c][h]) > Math.abs(e[y][h]) && (y = c);
|
|
824
|
+
[e[h], e[y]] = [e[y], e[h]], [a[h], a[y]] = [a[y], a[h]];
|
|
825
|
+
for (let c = h + 1; c < o; c++) {
|
|
826
|
+
const r = e[c][h] / e[h][h];
|
|
827
|
+
for (let w = h; w < n; w++)
|
|
828
|
+
e[c][w] -= r * e[h][w];
|
|
829
|
+
a[c] -= r * a[h];
|
|
830
830
|
}
|
|
831
831
|
}
|
|
832
|
-
const D = new Array(
|
|
833
|
-
for (let h =
|
|
834
|
-
let y =
|
|
835
|
-
for (let c = h + 1; c <
|
|
836
|
-
y -=
|
|
837
|
-
D[h] = y /
|
|
832
|
+
const D = new Array(n);
|
|
833
|
+
for (let h = n - 1; h >= 0; h--) {
|
|
834
|
+
let y = a[h];
|
|
835
|
+
for (let c = h + 1; c < n; c++)
|
|
836
|
+
y -= e[h][c] * D[c];
|
|
837
|
+
D[h] = y / e[h][h];
|
|
838
838
|
}
|
|
839
839
|
return D;
|
|
840
840
|
}
|
|
@@ -846,9 +846,9 @@ function MA(I, A) {
|
|
|
846
846
|
];
|
|
847
847
|
}
|
|
848
848
|
function LA(I, A, g) {
|
|
849
|
-
const { topLeft: C, topRight: E, bottomRight: B, bottomLeft: Q } = g, i = Math.hypot(B.x - Q.x, B.y - Q.y), s = Math.hypot(E.x - C.x, E.y - C.y), t = Math.round(Math.max(i, s)),
|
|
850
|
-
I.canvas.width = t, I.canvas.height =
|
|
851
|
-
const
|
|
849
|
+
const { topLeft: C, topRight: E, bottomRight: B, bottomLeft: Q } = g, i = Math.hypot(B.x - Q.x, B.y - Q.y), s = Math.hypot(E.x - C.x, E.y - C.y), t = Math.round(Math.max(i, s)), o = Math.hypot(E.x - B.x, E.y - B.y), n = Math.hypot(C.x - Q.x, C.y - Q.y), e = Math.round(Math.max(o, n));
|
|
850
|
+
I.canvas.width = t, I.canvas.height = e;
|
|
851
|
+
const a = [
|
|
852
852
|
[C.x, C.y],
|
|
853
853
|
[E.x, E.y],
|
|
854
854
|
[B.x, B.y],
|
|
@@ -856,18 +856,18 @@ function LA(I, A, g) {
|
|
|
856
856
|
], D = [
|
|
857
857
|
[0, 0],
|
|
858
858
|
[t - 1, 0],
|
|
859
|
-
[t - 1,
|
|
860
|
-
[0,
|
|
861
|
-
], h = MA(
|
|
862
|
-
YA(I, A, h, t,
|
|
859
|
+
[t - 1, e - 1],
|
|
860
|
+
[0, e - 1]
|
|
861
|
+
], h = MA(a, D);
|
|
862
|
+
YA(I, A, h, t, e);
|
|
863
863
|
}
|
|
864
864
|
function mA(I) {
|
|
865
|
-
const A = I[0][0], g = I[0][1], C = I[0][2], E = I[1][0], B = I[1][1], Q = I[1][2], i = I[2][0], s = I[2][1], t = I[2][2],
|
|
865
|
+
const A = I[0][0], g = I[0][1], C = I[0][2], E = I[1][0], B = I[1][1], Q = I[1][2], i = I[2][0], s = I[2][1], t = I[2][2], o = B * t - Q * s, n = -(E * t - Q * i), e = E * s - B * i, a = -(g * t - C * s), D = A * t - C * i, h = -(A * s - g * i), y = g * Q - C * B, c = -(A * Q - C * E), r = A * B - g * E, w = A * o + g * n + C * e;
|
|
866
866
|
if (w === 0) throw new Error("Singular matrix");
|
|
867
867
|
return [
|
|
868
|
-
[
|
|
869
|
-
[
|
|
870
|
-
[
|
|
868
|
+
[o / w, a / w, y / w],
|
|
869
|
+
[n / w, D / w, c / w],
|
|
870
|
+
[e / w, h / w, r / w]
|
|
871
871
|
];
|
|
872
872
|
}
|
|
873
873
|
function YA(I, A, g, C, E) {
|
|
@@ -876,12 +876,12 @@ function YA(I, A, g, C, E) {
|
|
|
876
876
|
const i = Q.getContext("2d");
|
|
877
877
|
i.drawImage(A, 0, 0, Q.width, Q.height);
|
|
878
878
|
const s = i.getImageData(0, 0, Q.width, Q.height), t = I.createImageData(C, E);
|
|
879
|
-
for (let
|
|
880
|
-
for (let
|
|
881
|
-
const
|
|
879
|
+
for (let o = 0; o < E; o++)
|
|
880
|
+
for (let n = 0; n < C; n++) {
|
|
881
|
+
const e = B[2][0] * n + B[2][1] * o + B[2][2], a = (B[0][0] * n + B[0][1] * o + B[0][2]) / e, D = (B[1][0] * n + B[1][1] * o + B[1][2]) / e, h = Math.max(0, Math.min(Q.width - 2, a)), y = Math.max(0, Math.min(Q.height - 2, D)), c = Math.floor(h), r = Math.floor(y), w = h - c, F = y - r;
|
|
882
882
|
for (let R = 0; R < 4; R++) {
|
|
883
|
-
const
|
|
884
|
-
t.data[(
|
|
883
|
+
const d = s.data[(r * Q.width + c) * 4 + R], S = s.data[(r * Q.width + (c + 1)) * 4 + R], G = s.data[((r + 1) * Q.width + c) * 4 + R], f = s.data[((r + 1) * Q.width + (c + 1)) * 4 + R];
|
|
884
|
+
t.data[(o * C + n) * 4 + R] = (1 - w) * (1 - F) * d + w * (1 - F) * S + (1 - w) * F * G + w * F * f;
|
|
885
885
|
}
|
|
886
886
|
}
|
|
887
887
|
I.putImageData(t, 0, 0);
|
|
@@ -889,51 +889,50 @@ function YA(I, A, g, C, E) {
|
|
|
889
889
|
async function X(I, A = {}) {
|
|
890
890
|
const g = A.mode || "detect", C = A.output || "canvas";
|
|
891
891
|
A.debug;
|
|
892
|
-
let E
|
|
892
|
+
let E;
|
|
893
893
|
if (I instanceof ImageData)
|
|
894
|
-
E = I,
|
|
894
|
+
E = I, I.width, I.height;
|
|
895
895
|
else {
|
|
896
|
-
const
|
|
897
|
-
|
|
898
|
-
const
|
|
899
|
-
|
|
896
|
+
const s = document.createElement("canvas");
|
|
897
|
+
s.width = I.width || I.naturalWidth, s.height = I.height || I.naturalHeight;
|
|
898
|
+
const t = s.getContext("2d");
|
|
899
|
+
t.drawImage(I, 0, 0, s.width, s.height), E = t.getImageData(0, 0, s.width, s.height), s.width, s.height;
|
|
900
900
|
}
|
|
901
|
-
const
|
|
902
|
-
if (!
|
|
901
|
+
const B = await JA(E, A);
|
|
902
|
+
if (!B.success)
|
|
903
903
|
return {
|
|
904
904
|
output: null,
|
|
905
905
|
corners: null,
|
|
906
906
|
contour: null,
|
|
907
|
-
debug:
|
|
907
|
+
debug: B.debug,
|
|
908
908
|
success: !1,
|
|
909
|
-
message:
|
|
909
|
+
message: B.message || "No document detected"
|
|
910
910
|
};
|
|
911
|
-
let
|
|
911
|
+
let Q, i;
|
|
912
912
|
if (g === "detect")
|
|
913
|
-
|
|
913
|
+
i = null;
|
|
914
914
|
else if (g === "extract") {
|
|
915
|
-
|
|
916
|
-
const
|
|
917
|
-
LA(
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
debug: i.debug,
|
|
915
|
+
Q = document.createElement("canvas");
|
|
916
|
+
const s = Q.getContext("2d");
|
|
917
|
+
LA(s, I, B.corners);
|
|
918
|
+
}
|
|
919
|
+
return g !== "detect" && Q && (C === "canvas" ? i = Q : C === "imagedata" ? i = Q.getContext("2d").getImageData(0, 0, Q.width, Q.height) : C === "dataurl" ? i = Q.toDataURL() : i = Q), {
|
|
920
|
+
output: i,
|
|
921
|
+
corners: B.corners,
|
|
922
|
+
contour: B.contour,
|
|
923
|
+
debug: B.debug,
|
|
925
924
|
success: !0,
|
|
926
925
|
message: "Document detected"
|
|
927
926
|
};
|
|
928
927
|
}
|
|
929
928
|
const uA = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
930
929
|
__proto__: null,
|
|
931
|
-
LiveScanner:
|
|
930
|
+
LiveScanner: dA,
|
|
932
931
|
checkWebcamAvailability: kA,
|
|
933
932
|
scanDocument: X
|
|
934
933
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
935
934
|
export {
|
|
936
|
-
|
|
935
|
+
dA as LiveScanner,
|
|
937
936
|
kA as checkWebcamAvailability,
|
|
938
937
|
X as scanDocument
|
|
939
938
|
};
|