datakeen-session-react 1.1.202-dev.17 → 1.1.202-dev.18
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/dist/cjs/components/DatakeenSession.js +17 -16
- package/dist/cjs/components/card-capture-guide/CardCaptureGuide.js +139 -0
- package/dist/cjs/components/card-capture-guide/captureTelemetry.js +81 -0
- package/dist/cjs/components/card-capture-guide/guidance.js +48 -0
- package/dist/cjs/components/card-capture-guide/guideCore.js +389 -0
- package/dist/cjs/components/card-capture-guide/guideRuntime.js +118 -0
- package/dist/cjs/components/card-capture-guide/modelLoader.js +46 -0
- package/dist/cjs/components/card-capture-guide/useCaptureGuideThresholds.js +33 -0
- package/dist/cjs/components/card-capture-guide/useCardCaptureGuide.js +117 -0
- package/dist/cjs/components/card-capture-guide/useMinDisplay.js +30 -0
- package/dist/cjs/components/id-check/Photo.js +230 -122
- package/dist/cjs/components/id-check/card-capture/CardCapture.js +270 -0
- package/dist/cjs/components/id-check/card-capture/DynamicCardMask.js +85 -0
- package/dist/cjs/components/id-check/card-capture/geometry.js +119 -0
- package/dist/cjs/components/id-check/card-capture/guideTone.js +15 -0
- package/dist/cjs/components/jdi/JDIDocumentSelection.js +9 -0
- package/dist/cjs/components/session/DocumentCheck.js +19 -2
- package/dist/cjs/components/start-flow/Start.js +4 -0
- package/dist/cjs/context/CardCaptureGuideContext.js +16 -0
- package/dist/cjs/i18n/en.json.js +29 -0
- package/dist/cjs/i18n/es.json.js +29 -0
- package/dist/cjs/i18n/fr.json.js +29 -0
- package/dist/cjs/index.css.js +1 -1
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/services/sessionService.js +43 -0
- package/dist/esm/components/DatakeenSession.js +17 -16
- package/dist/esm/components/card-capture-guide/CardCaptureGuide.js +135 -0
- package/dist/esm/components/card-capture-guide/captureTelemetry.js +78 -0
- package/dist/esm/components/card-capture-guide/guidance.js +44 -0
- package/dist/esm/components/card-capture-guide/guideCore.js +372 -0
- package/dist/esm/components/card-capture-guide/guideRuntime.js +115 -0
- package/dist/esm/components/card-capture-guide/modelLoader.js +42 -0
- package/dist/esm/components/card-capture-guide/useCaptureGuideThresholds.js +31 -0
- package/dist/esm/components/card-capture-guide/useCardCaptureGuide.js +115 -0
- package/dist/esm/components/card-capture-guide/useMinDisplay.js +28 -0
- package/dist/esm/components/id-check/Photo.js +232 -124
- package/dist/esm/components/id-check/card-capture/CardCapture.js +266 -0
- package/dist/esm/components/id-check/card-capture/DynamicCardMask.js +81 -0
- package/dist/esm/components/id-check/card-capture/geometry.js +110 -0
- package/dist/esm/components/id-check/card-capture/guideTone.js +13 -0
- package/dist/esm/components/jdi/JDIDocumentSelection.js +9 -0
- package/dist/esm/components/session/DocumentCheck.js +19 -2
- package/dist/esm/components/start-flow/Start.js +4 -0
- package/dist/esm/context/CardCaptureGuideContext.js +13 -0
- package/dist/esm/i18n/en.json.js +28 -1
- package/dist/esm/i18n/es.json.js +28 -1
- package/dist/esm/i18n/fr.json.js +28 -1
- package/dist/esm/index.css.js +1 -1
- package/dist/esm/index.js +5 -0
- package/dist/esm/services/sessionService.js +42 -1
- package/dist/types/components/card-capture-guide/CardCaptureGuide.d.ts +46 -0
- package/dist/types/components/card-capture-guide/guidance.d.ts +45 -0
- package/dist/types/components/card-capture-guide/guideCore.d.ts +96 -0
- package/dist/types/components/card-capture-guide/guideRuntime.d.ts +13 -0
- package/dist/types/components/card-capture-guide/modelLoader.d.ts +13 -0
- package/dist/types/components/card-capture-guide/useCardCaptureGuide.d.ts +38 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/types/camera.d.ts +3 -0
- package/dist/types/types/session.d.ts +15 -1
- package/package.json +2 -1
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MIRRORED from model-id-capture-guidance/web/src/guide-core.mjs (parity-tested there against the Python reference).
|
|
5
|
+
* Keep both in sync. Pure (DOM-free) logic for the browser card-guide v2: letterbox mapping, output decoding,
|
|
6
|
+
* the geometric quad gate, and the temporal tracker that turns noisy per-frame predictions
|
|
7
|
+
* into stable guidance states. Shared by the React component, the plain-JS demo and the Node
|
|
8
|
+
* unit tests. Keep numerically identical to training_v2/model.py::decode and
|
|
9
|
+
* eval/quad_validation.py (parity is checked in tests/run_core_test.js).
|
|
10
|
+
*
|
|
11
|
+
* Model I/O (card_guide_v2.onnx): input (1,3,256,256) letterboxed + ImageNet-normalised;
|
|
12
|
+
* outputs cls(1,3 softmax: none,id_document,other_card) heat(1,4,64,64) off(1,8,64,64) vis(1,4) glob(1,4,2).
|
|
13
|
+
*/
|
|
14
|
+
var SIZE = 256;
|
|
15
|
+
var HM = 64;
|
|
16
|
+
var AGREE_DIST = 0.08; // heatmap peak must be this close to the global regression (canvas units) to be trusted
|
|
17
|
+
/** Letterbox a (w,h) frame into the SIZExSIZE canvas. */
|
|
18
|
+
function letterbox(w, h) {
|
|
19
|
+
var scale = SIZE / Math.max(w, h);
|
|
20
|
+
var nw = Math.round(w * scale), nh = Math.round(h * scale);
|
|
21
|
+
return { scale: scale, nw: nw, nh: nh, ox: Math.floor((SIZE - nw) / 2), oy: Math.floor((SIZE - nh) / 2) };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Decode raw model outputs. Returns presence (P(id)+P(other)) and 4 corners (TL,TR,BR,BL)
|
|
25
|
+
* normalised to the *source frame* (may fall outside [0,1] when a corner is out of frame).
|
|
26
|
+
*/
|
|
27
|
+
function decode(out, srcW, srcH) {
|
|
28
|
+
var cls = out.cls, heat = out.heat, off = out.off, vis = out.vis, glob = out.glob;
|
|
29
|
+
var lb = letterbox(srcW, srcH), pts = [];
|
|
30
|
+
for (var k = 0; k < 4; k++) {
|
|
31
|
+
var cx = glob[k * 2], cy = glob[k * 2 + 1];
|
|
32
|
+
if (vis[k] > 0.5) {
|
|
33
|
+
var best = -1, bi = 0;
|
|
34
|
+
var base = k * HM * HM;
|
|
35
|
+
for (var i = 0; i < HM * HM; i++)
|
|
36
|
+
if (heat[base + i] > best) {
|
|
37
|
+
best = heat[base + i];
|
|
38
|
+
bi = i;
|
|
39
|
+
}
|
|
40
|
+
var iy = Math.floor(bi / HM), ix = bi % HM;
|
|
41
|
+
var px = (ix + 0.5 + off[(2 * k) * HM * HM + bi]) / HM;
|
|
42
|
+
var py = (iy + 0.5 + off[(2 * k + 1) * HM * HM + bi]) / HM;
|
|
43
|
+
if (Math.hypot(px - cx, py - cy) < AGREE_DIST) {
|
|
44
|
+
cx = px;
|
|
45
|
+
cy = py;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
pts.push([((cx * SIZE - lb.ox) / lb.scale) / srcW, ((cy * SIZE - lb.oy) / lb.scale) / srcH]);
|
|
49
|
+
}
|
|
50
|
+
return { presence: cls[1] + cls[2], idProb: cls[1], otherProb: cls[2], corners: pts };
|
|
51
|
+
}
|
|
52
|
+
var MIN_AREA_FRACTION = 0.01, MIN_ASPECT = 1.1, MAX_ASPECT = 2.1, MIN_ANGLE = 40, MAX_ANGLE = 140, MIN_EDGE_RATIO = 0.3;
|
|
53
|
+
/** Port of quad_rejection_reason. quad in pixels. Returns null if plausible, else a reason. */
|
|
54
|
+
function quadRejectionReason(quad, w, h) {
|
|
55
|
+
var q = quad;
|
|
56
|
+
if (!q.every(function (_a) {
|
|
57
|
+
var x = _a[0], y = _a[1];
|
|
58
|
+
return isFinite(x) && isFinite(y);
|
|
59
|
+
}))
|
|
60
|
+
return "non-finite";
|
|
61
|
+
var signs = [];
|
|
62
|
+
for (var i = 0; i < 4; i++) {
|
|
63
|
+
var a = q[i], b = q[(i + 1) % 4], c = q[(i + 2) % 4];
|
|
64
|
+
signs.push(Math.sign((b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0])));
|
|
65
|
+
}
|
|
66
|
+
if (signs.includes(0) || new Set(signs).size !== 1)
|
|
67
|
+
return "not convex";
|
|
68
|
+
var area = 0;
|
|
69
|
+
for (var i = 0; i < 4; i++)
|
|
70
|
+
area += q[i][0] * q[(i + 1) % 4][1] - q[(i + 1) % 4][0] * q[i][1];
|
|
71
|
+
area = Math.abs(area) / 2;
|
|
72
|
+
if (area < MIN_AREA_FRACTION * w * h)
|
|
73
|
+
return "area too small";
|
|
74
|
+
var edges = q.map(function (p, i) { return Math.hypot(q[(i + 1) % 4][0] - p[0], q[(i + 1) % 4][1] - p[1]); });
|
|
75
|
+
if (Math.min.apply(Math, edges) / Math.max.apply(Math, edges) < MIN_EDGE_RATIO)
|
|
76
|
+
return "degenerate edge ratio";
|
|
77
|
+
for (var i = 0; i < 4; i++) {
|
|
78
|
+
var p = q[i], a = q[(i + 3) % 4], b = q[(i + 1) % 4];
|
|
79
|
+
var u = [a[0] - p[0], a[1] - p[1]], v = [b[0] - p[0], b[1] - p[1]];
|
|
80
|
+
var cos = (u[0] * v[0] + u[1] * v[1]) / (Math.hypot.apply(Math, u) * Math.hypot.apply(Math, v) + 1e-9);
|
|
81
|
+
var ang = (Math.acos(Math.max(-1, Math.min(1, cos))) * 180) / Math.PI;
|
|
82
|
+
if (ang < MIN_ANGLE || ang > MAX_ANGLE)
|
|
83
|
+
return "corner angle";
|
|
84
|
+
}
|
|
85
|
+
var wm = (edges[0] + edges[2]) / 2, hm = (edges[1] + edges[3]) / 2;
|
|
86
|
+
var aspect = Math.max(wm, hm) / Math.max(Math.min(wm, hm), 1e-9);
|
|
87
|
+
if (aspect < MIN_ASPECT || aspect > MAX_ASPECT)
|
|
88
|
+
return "aspect";
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
92
|
+
// Capture-quality metrics (blur + glare), measured on the card quad warped to a fixed-size patch.
|
|
93
|
+
// Reference implementation + calibration: eval/quality_metrics.py (parity-tested in tests/run_core_test.js).
|
|
94
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
95
|
+
var WARP_W = 320;
|
|
96
|
+
var SAT_THR = 245; // a pixel is "saturated" when R, G and B are all >= this
|
|
97
|
+
var CELL = 8;
|
|
98
|
+
/** Solve the 3x3 homography H (h33 = 1) that maps each src[i] -> dst[i] (4 point pairs). */
|
|
99
|
+
function homography(src, dst) {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
var A = [], b = [];
|
|
102
|
+
for (var i = 0; i < 4; i++) {
|
|
103
|
+
var _c = src[i], x = _c[0], y = _c[1], _d = dst[i], u = _d[0], v = _d[1];
|
|
104
|
+
A.push([x, y, 1, 0, 0, 0, -u * x, -u * y]);
|
|
105
|
+
b.push(u);
|
|
106
|
+
A.push([0, 0, 0, x, y, 1, -v * x, -v * y]);
|
|
107
|
+
b.push(v);
|
|
108
|
+
}
|
|
109
|
+
for (var c = 0; c < 8; c++) { // gaussian elimination with partial pivoting
|
|
110
|
+
var piv = c;
|
|
111
|
+
for (var r = c + 1; r < 8; r++)
|
|
112
|
+
if (Math.abs(A[r][c]) > Math.abs(A[piv][c]))
|
|
113
|
+
piv = r;
|
|
114
|
+
_a = [A[piv], A[c]], A[c] = _a[0], A[piv] = _a[1];
|
|
115
|
+
_b = [b[piv], b[c]], b[c] = _b[0], b[piv] = _b[1];
|
|
116
|
+
for (var r = c + 1; r < 8; r++) {
|
|
117
|
+
var f = A[r][c] / A[c][c];
|
|
118
|
+
for (var k = c; k < 8; k++)
|
|
119
|
+
A[r][k] -= f * A[c][k];
|
|
120
|
+
b[r] -= f * b[c];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
var h = new Array(8);
|
|
124
|
+
for (var r = 7; r >= 0; r--) {
|
|
125
|
+
var s = b[r];
|
|
126
|
+
for (var k = r + 1; k < 8; k++)
|
|
127
|
+
s -= A[r][k] * h[k];
|
|
128
|
+
h[r] = s / A[r][r];
|
|
129
|
+
}
|
|
130
|
+
return h;
|
|
131
|
+
}
|
|
132
|
+
/** Perspective-warp the quad (TL,TR,BR,BL in frame pixels) out of an RGBA frame into a WARP_W-wide RGB patch (bilinear, edge-clamped). */
|
|
133
|
+
function warpCard(frame, quad) {
|
|
134
|
+
var data = frame.data, W = frame.width, H = frame.height, q = quad;
|
|
135
|
+
var d = function (a, b) { return Math.hypot(q[a][0] - q[b][0], q[a][1] - q[b][1]); };
|
|
136
|
+
var w = (d(0, 1) + d(3, 2)) / 2, h = (d(0, 3) + d(1, 2)) / 2;
|
|
137
|
+
var asp = Math.min(1.8, Math.max(1.2, Math.max(w, h) / Math.max(Math.min(w, h), 1e-6)));
|
|
138
|
+
var pw = WARP_W, ph = Math.round(WARP_W / asp);
|
|
139
|
+
var hh = homography([[0, 0], [pw - 1, 0], [pw - 1, ph - 1], [0, ph - 1]], q); // dst -> src
|
|
140
|
+
var out = new Uint8ClampedArray(pw * ph * 3);
|
|
141
|
+
for (var y = 0; y < ph; y++)
|
|
142
|
+
for (var x = 0; x < pw; x++) {
|
|
143
|
+
var den = hh[6] * x + hh[7] * y + 1;
|
|
144
|
+
var sx = (hh[0] * x + hh[1] * y + hh[2]) / den, sy = (hh[3] * x + hh[4] * y + hh[5]) / den;
|
|
145
|
+
sx = Math.min(W - 1, Math.max(0, sx));
|
|
146
|
+
sy = Math.min(H - 1, Math.max(0, sy));
|
|
147
|
+
var x0 = Math.floor(sx), y0 = Math.floor(sy), x1 = Math.min(W - 1, x0 + 1), y1 = Math.min(H - 1, y0 + 1), fx = sx - x0, fy = sy - y0;
|
|
148
|
+
for (var c = 0; c < 3; c++) {
|
|
149
|
+
var v = (data[(y0 * W + x0) * 4 + c] * (1 - fx) + data[(y0 * W + x1) * 4 + c] * fx) * (1 - fy) + (data[(y1 * W + x0) * 4 + c] * (1 - fx) + data[(y1 * W + x1) * 4 + c] * fx) * fy;
|
|
150
|
+
out[(y * pw + x) * 3 + c] = Math.round(v);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { data: out, width: pw, height: ph };
|
|
154
|
+
}
|
|
155
|
+
/** Mean luma (0..255) of an RGB patch. Card patch < ~55 (or whole frame < ~45) = not enough light; normal webcam scenes sit at 95-165. */
|
|
156
|
+
function brightnessScore(patch) {
|
|
157
|
+
var data = patch.data, w = patch.width, h = patch.height;
|
|
158
|
+
var sum = 0;
|
|
159
|
+
for (var i = 0; i < w * h; i++)
|
|
160
|
+
sum += 0.299 * data[i * 3] + 0.587 * data[i * 3 + 1] + 0.114 * data[i * 3 + 2];
|
|
161
|
+
return sum / (w * h);
|
|
162
|
+
}
|
|
163
|
+
/** Mean luma of a RGBA frame region - used on the model's letterboxed canvas so it works even when no card is in view. */
|
|
164
|
+
function frameBrightness(rgba, width, rect) {
|
|
165
|
+
if (rect === void 0) { rect = null; }
|
|
166
|
+
var x0 = rect ? rect.x : 0, y0 = rect ? rect.y : 0, rw = rect ? rect.w : width, rh = rect ? rect.h : rgba.length / 4 / width;
|
|
167
|
+
var sum = 0;
|
|
168
|
+
for (var y = y0; y < y0 + rh; y++)
|
|
169
|
+
for (var x = x0; x < x0 + rw; x++) {
|
|
170
|
+
var i = (y * width + x) * 4;
|
|
171
|
+
sum += 0.299 * rgba[i] + 0.587 * rgba[i + 1] + 0.114 * rgba[i + 2];
|
|
172
|
+
}
|
|
173
|
+
return sum / (rw * rh);
|
|
174
|
+
}
|
|
175
|
+
/** Variance of the 3x3 Laplacian on the gray patch (higher = sharper). ~<400 blurry, >=600 sharp (calibrated on webcam video). */
|
|
176
|
+
function blurScore(patch) {
|
|
177
|
+
var data = patch.data, w = patch.width, h = patch.height, g = new Float32Array(w * h);
|
|
178
|
+
for (var i = 0; i < w * h; i++)
|
|
179
|
+
g[i] = Math.round(0.299 * data[i * 3] + 0.587 * data[i * 3 + 1] + 0.114 * data[i * 3 + 2]);
|
|
180
|
+
var sum = 0, sum2 = 0, n = 0;
|
|
181
|
+
for (var y = 1; y < h - 1; y++)
|
|
182
|
+
for (var x = 1; x < w - 1; x++) {
|
|
183
|
+
var i = y * w + x, l = -4 * g[i] + g[i - 1] + g[i + 1] + g[i - w] + g[i + w];
|
|
184
|
+
sum += l;
|
|
185
|
+
sum2 += l * l;
|
|
186
|
+
n++;
|
|
187
|
+
}
|
|
188
|
+
var mean = sum / n;
|
|
189
|
+
return sum2 / n - mean * mean;
|
|
190
|
+
}
|
|
191
|
+
/** Glare: share of 8x8 cells that are mostly saturated-white (blob), plus the raw saturated-pixel share. blob >= 0.012 means a highlight >~1.5% of the card. */
|
|
192
|
+
function glareScore(patch) {
|
|
193
|
+
var data = patch.data, w = patch.width, h = patch.height, cw = Math.floor(w / CELL), ch = Math.floor(h / CELL);
|
|
194
|
+
var sat = 0, cellsHit = 0;
|
|
195
|
+
for (var cy = 0; cy < ch; cy++)
|
|
196
|
+
for (var cx = 0; cx < cw; cx++) {
|
|
197
|
+
var s = 0;
|
|
198
|
+
for (var y = 0; y < CELL; y++)
|
|
199
|
+
for (var x = 0; x < CELL; x++) {
|
|
200
|
+
var i = ((cy * CELL + y) * w + cx * CELL + x) * 3;
|
|
201
|
+
if (Math.min(data[i], data[i + 1], data[i + 2]) >= SAT_THR)
|
|
202
|
+
s++;
|
|
203
|
+
}
|
|
204
|
+
if (s / (CELL * CELL) > 0.5)
|
|
205
|
+
cellsHit++;
|
|
206
|
+
}
|
|
207
|
+
for (var i = 0; i < w * h; i++)
|
|
208
|
+
if (Math.min(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]) >= SAT_THR)
|
|
209
|
+
sat++;
|
|
210
|
+
return { sat: sat / (w * h), blob: cellsHit / (cw * ch) };
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* One call for the browser glue: frame = RGBA ImageData of the video (any scale), quad = the 4 corners in THAT frame's pixels,
|
|
214
|
+
* nativeScale = (frame px per original video px) so `nativeLong` reports the card's long side in true camera pixels.
|
|
215
|
+
*/
|
|
216
|
+
function measureQuality(frame, quad, nativeScale) {
|
|
217
|
+
if (nativeScale === void 0) { nativeScale = 1; }
|
|
218
|
+
var patch = warpCard(frame, quad), g = glareScore(patch);
|
|
219
|
+
var d = function (a, b) { return Math.hypot(quad[a][0] - quad[b][0], quad[a][1] - quad[b][1]); };
|
|
220
|
+
var nativeLong = Math.max(d(0, 1), d(3, 2), d(0, 3), d(1, 2)) / nativeScale;
|
|
221
|
+
return { blur: blurScore(patch), sat: g.sat, blob: g.blob, nativeLong: nativeLong, brightness: brightnessScore(patch) };
|
|
222
|
+
}
|
|
223
|
+
var State = Object.freeze({ NO_CARD: "no_card", TOO_FAR: "too_far", TOO_DARK: "too_dark", CARD_DETECTED: "card_detected", HOLD_STILL: "hold_still", TOO_BLURRY: "too_blurry", BRIGHT_SPOT: "bright_spot", READY: "ready" });
|
|
224
|
+
var MESSAGES = {
|
|
225
|
+
no_card: "Position your ID card in frame",
|
|
226
|
+
too_far: "Move the card closer",
|
|
227
|
+
too_dark: "Not enough light — increase the brightness",
|
|
228
|
+
card_detected: "Card detected — keep it in view",
|
|
229
|
+
hold_still: "Hold still…",
|
|
230
|
+
too_blurry: "Image is blurry — adjust the focus and hold steady",
|
|
231
|
+
bright_spot: "Reflection detected — tilt the card to avoid the bright spot",
|
|
232
|
+
ready: "Perfect — capturing",
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Temporal tracker. Feed it one decode() result per inference; it returns a stable guidance state.
|
|
236
|
+
* - hysteresis: card must score >= enterThr on `enterFrames` consecutive frames to appear, and is only
|
|
237
|
+
* dropped after `exitFrames` consecutive frames below exitThr (isolated bad frames do not flicker);
|
|
238
|
+
* - corners are EMA-smoothed; a rejected quad keeps the last good one during the grace period;
|
|
239
|
+
* - optional quality (blur/glare from measureQuality) is median-filtered over `qualityWindow` inferences: GLARE when a bright
|
|
240
|
+
* highlight covers the card, BLURRY when the card is still but soft, TOO_FAR when it is under `minNativeLong` camera pixels;
|
|
241
|
+
* READY additionally requires sharp + no glare, so the auto-capture never fires on a blurry or reflective frame;
|
|
242
|
+
* - HOLD_STILL when mean corner motion < stillMotion (fraction of card diagonal) for `stillFrames`;
|
|
243
|
+
* READY once it has been still for `holdMs` -- `onReady` fires once per card presence.
|
|
244
|
+
*/
|
|
245
|
+
var GuideTracker = /** @class */ (function () {
|
|
246
|
+
function GuideTracker(opts) {
|
|
247
|
+
if (opts === void 0) { opts = {}; }
|
|
248
|
+
this.fb = [];
|
|
249
|
+
this.present = false;
|
|
250
|
+
this.hi = 0;
|
|
251
|
+
this.lo = 0;
|
|
252
|
+
this.corners = null;
|
|
253
|
+
this.stillCount = 0;
|
|
254
|
+
this.bad = 0;
|
|
255
|
+
this.stillSince = null;
|
|
256
|
+
this.fired = false;
|
|
257
|
+
this.qual = [];
|
|
258
|
+
this.state = State.NO_CARD;
|
|
259
|
+
this.o = Object.assign({ enterThr: 0.5, exitThr: 0.3, enterFrames: 2, exitFrames: 4, ema: 0.5, stillMotion: 0.015, stillFrames: 3, holdMs: 600, stillExitMotion: 0.04, stillGraceFrames: 2, minAreaFrac: 0.06, minBlur: 450, maxGlareBlob: 0.012, minNativeLong: 300, minCardBrightness: 55, minFrameBrightness: 45, qualityWindow: 3 }, opts);
|
|
260
|
+
this.fb = [];
|
|
261
|
+
this.reset();
|
|
262
|
+
}
|
|
263
|
+
GuideTracker.prototype.reset = function () { this.present = false; this.hi = 0; this.lo = 0; this.corners = null; this.stillCount = 0; this.stillSince = null; this.bad = 0; this.fired = false; this.qual = []; this.state = State.NO_CARD; };
|
|
264
|
+
GuideTracker.prototype.update = function (dec, srcW, srcH, tMs, quality, frameBrightness) {
|
|
265
|
+
if (quality === void 0) { quality = null; }
|
|
266
|
+
if (frameBrightness === void 0) { frameBrightness = null; }
|
|
267
|
+
if (frameBrightness !== null) {
|
|
268
|
+
this.fb.push(frameBrightness);
|
|
269
|
+
if (this.fb.length > this.o.qualityWindow)
|
|
270
|
+
this.fb.shift();
|
|
271
|
+
}
|
|
272
|
+
if (quality) {
|
|
273
|
+
this.qual.push(quality);
|
|
274
|
+
if (this.qual.length > this.o.qualityWindow)
|
|
275
|
+
this.qual.shift();
|
|
276
|
+
}
|
|
277
|
+
var o = this.o, px = dec.corners.map(function (_a) {
|
|
278
|
+
var x = _a[0], y = _a[1];
|
|
279
|
+
return [x * srcW, y * srcH];
|
|
280
|
+
});
|
|
281
|
+
var valid = quadRejectionReason(px, srcW, srcH) === null;
|
|
282
|
+
if (dec.presence >= o.enterThr && valid) {
|
|
283
|
+
this.hi++;
|
|
284
|
+
}
|
|
285
|
+
else
|
|
286
|
+
this.hi = 0;
|
|
287
|
+
if (dec.presence < o.exitThr)
|
|
288
|
+
this.lo++;
|
|
289
|
+
else
|
|
290
|
+
this.lo = 0;
|
|
291
|
+
if (!this.present && this.hi >= o.enterFrames)
|
|
292
|
+
this.present = true;
|
|
293
|
+
if (this.present && this.lo >= o.exitFrames) {
|
|
294
|
+
this.reset();
|
|
295
|
+
return this._out(dec, srcW, srcH);
|
|
296
|
+
}
|
|
297
|
+
if (!this.present)
|
|
298
|
+
return this._out(dec, srcW, srcH);
|
|
299
|
+
var motion = Infinity;
|
|
300
|
+
if (valid && dec.presence >= o.exitThr) {
|
|
301
|
+
var prev_1 = this.corners;
|
|
302
|
+
var next = prev_1 ? prev_1.map(function (p, i) { return [p[0] + o.ema * (dec.corners[i][0] - p[0]), p[1] + o.ema * (dec.corners[i][1] - p[1])]; }) : dec.corners.map(function (p) { return [p[0], p[1]]; });
|
|
303
|
+
if (prev_1) {
|
|
304
|
+
var diag = Math.hypot((next[0][0] - next[2][0]) * srcW, (next[0][1] - next[2][1]) * srcH) || 1;
|
|
305
|
+
motion = next.reduce(function (s, p, i) { return s + Math.hypot((p[0] - prev_1[i][0]) * srcW, (p[1] - prev_1[i][1]) * srcH); }, 0) / 4 / diag;
|
|
306
|
+
}
|
|
307
|
+
this.corners = next;
|
|
308
|
+
}
|
|
309
|
+
// Hold-still hysteresis: entering "still" needs motion < stillMotion; once the hold threshold (stillFrames) is reached we are LOCKED and
|
|
310
|
+
// only give up on a clearly different image (motion >= stillExitMotion) or after more than stillGraceFrames consecutive bad frames
|
|
311
|
+
// (a dropped detection, an invalid quad, a hand tremor). Minor changes never invalidate a capture that is about to fire.
|
|
312
|
+
var locked = this.stillCount >= o.stillFrames;
|
|
313
|
+
if (motion < (locked ? o.stillExitMotion : o.stillMotion)) {
|
|
314
|
+
this.stillCount++;
|
|
315
|
+
this.bad = 0;
|
|
316
|
+
if (this.stillSince === null)
|
|
317
|
+
this.stillSince = tMs;
|
|
318
|
+
}
|
|
319
|
+
else if (locked && this.bad < o.stillGraceFrames) {
|
|
320
|
+
this.bad++;
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
this.stillCount = 0;
|
|
324
|
+
this.stillSince = null;
|
|
325
|
+
this.bad = 0;
|
|
326
|
+
}
|
|
327
|
+
return this._out(dec, srcW, srcH, tMs);
|
|
328
|
+
};
|
|
329
|
+
GuideTracker.prototype._out = function (dec, srcW, srcH, tMs) {
|
|
330
|
+
var _this = this;
|
|
331
|
+
if (tMs === void 0) { tMs = 0; }
|
|
332
|
+
var state = State.NO_CARD, readyNow = false;
|
|
333
|
+
if (this.present && this.corners) {
|
|
334
|
+
var c = this.corners;
|
|
335
|
+
var a = 0;
|
|
336
|
+
for (var i = 0; i < 4; i++) {
|
|
337
|
+
var j = (i + 1) % 4;
|
|
338
|
+
a += c[i][0] * c[j][1] - c[j][0] * c[i][1];
|
|
339
|
+
}
|
|
340
|
+
var areaFrac = Math.abs(a) / 2; // corners are normalised, so this is already a fraction of the frame
|
|
341
|
+
var med = function (k) { var v = _this.qual.map(function (x) { return x[k]; }).filter(function (x) { return typeof x === "number"; }).sort(function (a, b) { return a - b; }); return v.length ? v[Math.floor(v.length / 2)] : null; };
|
|
342
|
+
var blur_1 = med("blur"), blob = med("blob"), nat = med("nativeLong"), bri = med("brightness"), still = this.stillCount >= this.o.stillFrames;
|
|
343
|
+
if (areaFrac < this.o.minAreaFrac || (nat !== null && nat < this.o.minNativeLong))
|
|
344
|
+
state = State.TOO_FAR;
|
|
345
|
+
else if (bri !== null && bri < this.o.minCardBrightness)
|
|
346
|
+
state = State.TOO_DARK;
|
|
347
|
+
else if (blob !== null && blob >= this.o.maxGlareBlob)
|
|
348
|
+
state = State.BRIGHT_SPOT;
|
|
349
|
+
else if (still && blur_1 !== null && blur_1 < this.o.minBlur)
|
|
350
|
+
state = State.TOO_BLURRY;
|
|
351
|
+
else if (still && this.stillSince !== null && tMs - this.stillSince >= this.o.holdMs) {
|
|
352
|
+
state = State.READY;
|
|
353
|
+
if (!this.fired) {
|
|
354
|
+
this.fired = true;
|
|
355
|
+
readyNow = true;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else if (still)
|
|
359
|
+
state = State.HOLD_STILL;
|
|
360
|
+
else
|
|
361
|
+
state = State.CARD_DETECTED;
|
|
362
|
+
}
|
|
363
|
+
if (state === State.NO_CARD && this.fb.length) {
|
|
364
|
+
var f = this.fb.slice().sort(function (x, y) { return x - y; })[Math.floor(this.fb.length / 2)];
|
|
365
|
+
if (f < this.o.minFrameBrightness)
|
|
366
|
+
state = State.TOO_DARK;
|
|
367
|
+
}
|
|
368
|
+
this.state = state;
|
|
369
|
+
return { state: state, message: MESSAGES[state], corners: this.present ? this.corners : null, presence: dec.presence, readyNow: readyNow, quality: this.qual.length ? this.qual[this.qual.length - 1] : null, frameBrightness: this.fb.length ? this.fb[this.fb.length - 1] : null };
|
|
370
|
+
};
|
|
371
|
+
return GuideTracker;
|
|
372
|
+
}());
|
|
373
|
+
|
|
374
|
+
exports.GuideTracker = GuideTracker;
|
|
375
|
+
exports.HM = HM;
|
|
376
|
+
exports.MESSAGES = MESSAGES;
|
|
377
|
+
exports.SAT_THR = SAT_THR;
|
|
378
|
+
exports.SIZE = SIZE;
|
|
379
|
+
exports.State = State;
|
|
380
|
+
exports.WARP_W = WARP_W;
|
|
381
|
+
exports.blurScore = blurScore;
|
|
382
|
+
exports.brightnessScore = brightnessScore;
|
|
383
|
+
exports.decode = decode;
|
|
384
|
+
exports.frameBrightness = frameBrightness;
|
|
385
|
+
exports.glareScore = glareScore;
|
|
386
|
+
exports.letterbox = letterbox;
|
|
387
|
+
exports.measureQuality = measureQuality;
|
|
388
|
+
exports.quadRejectionReason = quadRejectionReason;
|
|
389
|
+
exports.warpCard = warpCard;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib_es6 = require('../../node_modules/tslib/tslib.es6.js');
|
|
4
|
+
var guideCore = require('./guideCore.js');
|
|
5
|
+
var modelLoader = require('./modelLoader.js');
|
|
6
|
+
|
|
7
|
+
var IMAGENET_MEAN = [0.485, 0.456, 0.406];
|
|
8
|
+
var IMAGENET_STD = [0.229, 0.224, 0.225];
|
|
9
|
+
var PAD = 114; // letterbox gray, same as training
|
|
10
|
+
var QUALITY_MAX_SIDE = 640; // card long side (px) of the crop the quality metrics are measured on
|
|
11
|
+
var QUALITY_MIN_PRESENCE = 0.3;
|
|
12
|
+
function createGuideRuntime() {
|
|
13
|
+
return tslib_es6.__awaiter(this, arguments, void 0, function (options) {
|
|
14
|
+
function preprocess(video, w, h) {
|
|
15
|
+
var lb = guideCore.letterbox(w, h);
|
|
16
|
+
preCtx.fillStyle = "rgb(".concat(PAD, ",").concat(PAD, ",").concat(PAD, ")");
|
|
17
|
+
preCtx.fillRect(0, 0, guideCore.SIZE, guideCore.SIZE);
|
|
18
|
+
preCtx.drawImage(video, 0, 0, w, h, lb.ox, lb.oy, lb.nw, lb.nh);
|
|
19
|
+
var data = preCtx.getImageData(0, 0, guideCore.SIZE, guideCore.SIZE).data;
|
|
20
|
+
var n = guideCore.SIZE * guideCore.SIZE;
|
|
21
|
+
var chw = new Float32Array(3 * n);
|
|
22
|
+
for (var i = 0; i < n; i++) {
|
|
23
|
+
for (var c = 0; c < 3; c++)
|
|
24
|
+
chw[c * n + i] = (data[i * 4 + c] / 255 - IMAGENET_MEAN[c]) / IMAGENET_STD[c];
|
|
25
|
+
}
|
|
26
|
+
return { tensor: new ort.Tensor("float32", chw, [1, 3, guideCore.SIZE, guideCore.SIZE]), brightness: guideCore.frameBrightness(data, guideCore.SIZE, { x: lb.ox, y: lb.oy, w: lb.nw, h: lb.nh }) };
|
|
27
|
+
}
|
|
28
|
+
/** Crop the card's bounding box out of the video at <= QUALITY_MAX_SIDE px and measure blur/glare on it. */
|
|
29
|
+
function sampleQuality(video, w, h, corners) {
|
|
30
|
+
var px = corners.map(function (_a) {
|
|
31
|
+
var x = _a[0], y = _a[1];
|
|
32
|
+
return [x * w, y * h];
|
|
33
|
+
});
|
|
34
|
+
var xs = px.map(function (p) { return p[0]; });
|
|
35
|
+
var ys = px.map(function (p) { return p[1]; });
|
|
36
|
+
var pad = 0.04 * Math.max(Math.max.apply(Math, xs) - Math.min.apply(Math, xs), Math.max.apply(Math, ys) - Math.min.apply(Math, ys));
|
|
37
|
+
var bx = Math.max(0, Math.floor(Math.min.apply(Math, xs) - pad));
|
|
38
|
+
var by = Math.max(0, Math.floor(Math.min.apply(Math, ys) - pad));
|
|
39
|
+
var cw = Math.min(w, Math.ceil(Math.max.apply(Math, xs) + pad)) - bx;
|
|
40
|
+
var ch = Math.min(h, Math.ceil(Math.max.apply(Math, ys) + pad)) - by;
|
|
41
|
+
if (cw < 16 || ch < 16)
|
|
42
|
+
return null;
|
|
43
|
+
var s = Math.min(1, QUALITY_MAX_SIDE / Math.max(cw, ch));
|
|
44
|
+
crop.width = Math.max(1, Math.round(cw * s));
|
|
45
|
+
crop.height = Math.max(1, Math.round(ch * s));
|
|
46
|
+
cropCtx.drawImage(video, bx, by, cw, ch, 0, 0, crop.width, crop.height);
|
|
47
|
+
var frame = cropCtx.getImageData(0, 0, crop.width, crop.height);
|
|
48
|
+
var quad = px.map(function (_a) {
|
|
49
|
+
var x = _a[0], y = _a[1];
|
|
50
|
+
return [(x - bx) * s, (y - by) * s];
|
|
51
|
+
});
|
|
52
|
+
return guideCore.measureQuality(frame, quad, s);
|
|
53
|
+
}
|
|
54
|
+
var ort, modelBytes, session, preCanvas, preCtx, crop, cropCtx;
|
|
55
|
+
var _a, _b;
|
|
56
|
+
if (options === void 0) { options = {}; }
|
|
57
|
+
return tslib_es6.__generator(this, function (_c) {
|
|
58
|
+
switch (_c.label) {
|
|
59
|
+
case 0: return [4 /*yield*/, import('onnxruntime-web')];
|
|
60
|
+
case 1:
|
|
61
|
+
ort = _c.sent();
|
|
62
|
+
// The .wasm must match the installed JS glue exactly: pin the CDN fallback to the runtime's own version.
|
|
63
|
+
ort.env.wasm.wasmPaths =
|
|
64
|
+
(_a = options.wasmPaths) !== null && _a !== void 0 ? _a : "https://cdn.jsdelivr.net/npm/onnxruntime-web@".concat(ort.env.versions.web, "/dist/");
|
|
65
|
+
return [4 /*yield*/, modelLoader.loadModelBytes((_b = options.modelUrl) !== null && _b !== void 0 ? _b : modelLoader.DEFAULT_MODEL_URL)];
|
|
66
|
+
case 2:
|
|
67
|
+
modelBytes = _c.sent();
|
|
68
|
+
return [4 /*yield*/, ort.InferenceSession.create(modelBytes, {
|
|
69
|
+
executionProviders: ["wasm"],
|
|
70
|
+
graphOptimizationLevel: "all",
|
|
71
|
+
})];
|
|
72
|
+
case 3:
|
|
73
|
+
session = _c.sent();
|
|
74
|
+
preCanvas = document.createElement("canvas");
|
|
75
|
+
preCanvas.width = guideCore.SIZE;
|
|
76
|
+
preCanvas.height = guideCore.SIZE;
|
|
77
|
+
preCtx = preCanvas.getContext("2d", { willReadFrequently: true });
|
|
78
|
+
crop = document.createElement("canvas");
|
|
79
|
+
cropCtx = crop.getContext("2d", { willReadFrequently: true });
|
|
80
|
+
if (!preCtx || !cropCtx)
|
|
81
|
+
throw new Error("2D canvas context unavailable");
|
|
82
|
+
return [2 /*return*/, {
|
|
83
|
+
run: function (video) {
|
|
84
|
+
return tslib_es6.__awaiter(this, void 0, void 0, function () {
|
|
85
|
+
var w, h, pre, out, decoded, quality;
|
|
86
|
+
return tslib_es6.__generator(this, function (_a) {
|
|
87
|
+
switch (_a.label) {
|
|
88
|
+
case 0:
|
|
89
|
+
w = video.videoWidth;
|
|
90
|
+
h = video.videoHeight;
|
|
91
|
+
pre = preprocess(video, w, h);
|
|
92
|
+
return [4 /*yield*/, session.run({ input: pre.tensor })];
|
|
93
|
+
case 1:
|
|
94
|
+
out = _a.sent();
|
|
95
|
+
decoded = guideCore.decode({
|
|
96
|
+
cls: out.cls.data,
|
|
97
|
+
heat: out.heat.data,
|
|
98
|
+
off: out.off.data,
|
|
99
|
+
vis: out.vis.data,
|
|
100
|
+
glob: out.glob.data,
|
|
101
|
+
}, w, h);
|
|
102
|
+
quality = decoded.presence >= QUALITY_MIN_PRESENCE ? sampleQuality(video, w, h, decoded.corners) : null;
|
|
103
|
+
return [2 /*return*/, { decoded: decoded, quality: quality, frameBrightness: pre.brightness }];
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
dispose: function () {
|
|
109
|
+
void session.release();
|
|
110
|
+
},
|
|
111
|
+
}];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
exports.DEFAULT_MODEL_URL = modelLoader.DEFAULT_MODEL_URL;
|
|
118
|
+
exports.createGuideRuntime = createGuideRuntime;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Model download for the card guide, kept apart from the ONNX runtime so a screen can preload the weights
|
|
5
|
+
* (13.5 MB, the main wait on mobile) without pulling onnxruntime-web or the guide logic.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* card-guide-v2b-20261006-weights.onnx (SHA-256 5eae0785…f48b), public Datakeen bucket, CORS `*`.
|
|
9
|
+
* Temporary location for testing: APP3-2118 moves it to the Datakeen CDN with a versioned name and a long cache.
|
|
10
|
+
*/
|
|
11
|
+
var DEFAULT_MODEL_URL = "https://datakeen-public.s3.eu-west-3.amazonaws.com/FmyFye6vkg8rrzrtQkW3QADx6eyner1q2WAEVUSB/xyG08fbEmVzYFwezy8zzJvHRbfgWpA8spA0TNeJQ.onnx";
|
|
12
|
+
var cache = new Map();
|
|
13
|
+
/** Downloads the model once per URL and shares the bytes between the preload and the runtime. */
|
|
14
|
+
function loadModelBytes(url) {
|
|
15
|
+
if (url === void 0) { url = DEFAULT_MODEL_URL; }
|
|
16
|
+
var cached = cache.get(url);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
var pending = fetch(url, { mode: "cors", credentials: "omit" })
|
|
20
|
+
.then(function (res) {
|
|
21
|
+
if (!res.ok)
|
|
22
|
+
throw new Error("Card guide model download failed (".concat(res.status, ")"));
|
|
23
|
+
return res.arrayBuffer();
|
|
24
|
+
})
|
|
25
|
+
.then(function (buf) { return new Uint8Array(buf); });
|
|
26
|
+
// a failed download must not poison the cache: the next attempt retries
|
|
27
|
+
pending.catch(function () { return cache.delete(url); });
|
|
28
|
+
cache.set(url, pending);
|
|
29
|
+
return pending;
|
|
30
|
+
}
|
|
31
|
+
/** Starts the download ahead of the capture screen. Never throws: the capture screen retries or falls back. */
|
|
32
|
+
function preloadCardGuideModel(url) {
|
|
33
|
+
if (url === void 0) { url = DEFAULT_MODEL_URL; }
|
|
34
|
+
if (typeof fetch !== "function")
|
|
35
|
+
return;
|
|
36
|
+
try {
|
|
37
|
+
loadModelBytes(url).catch(function () { });
|
|
38
|
+
}
|
|
39
|
+
catch (_a) {
|
|
40
|
+
// a non-standard fetch can throw synchronously: preloading is best effort
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.DEFAULT_MODEL_URL = DEFAULT_MODEL_URL;
|
|
45
|
+
exports.loadModelBytes = loadModelBytes;
|
|
46
|
+
exports.preloadCardGuideModel = preloadCardGuideModel;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var sessionService = require('../../services/sessionService.js');
|
|
5
|
+
|
|
6
|
+
var EMPTY = {};
|
|
7
|
+
/**
|
|
8
|
+
* Card guide thresholds configured on the server (APP3-2119), read once per session. Any failure (older backend,
|
|
9
|
+
* network) leaves `{}`: the SDK defaults apply and the capture is never blocked.
|
|
10
|
+
*/
|
|
11
|
+
function useCaptureGuideThresholds(sessionId) {
|
|
12
|
+
var _a = React.useState(EMPTY), thresholds = _a[0], setThresholds = _a[1];
|
|
13
|
+
React.useEffect(function () {
|
|
14
|
+
if (!sessionId || typeof sessionService.getCaptureGuideConfig !== "function")
|
|
15
|
+
return;
|
|
16
|
+
var cancelled = false;
|
|
17
|
+
sessionService.getCaptureGuideConfig(sessionId)
|
|
18
|
+
.then(function (config) {
|
|
19
|
+
if (!cancelled)
|
|
20
|
+
setThresholds(config.thresholds);
|
|
21
|
+
})
|
|
22
|
+
.catch(function () {
|
|
23
|
+
if (!cancelled)
|
|
24
|
+
setThresholds(EMPTY);
|
|
25
|
+
});
|
|
26
|
+
return function () {
|
|
27
|
+
cancelled = true;
|
|
28
|
+
};
|
|
29
|
+
}, [sessionId]);
|
|
30
|
+
return thresholds;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
exports.useCaptureGuideThresholds = useCaptureGuideThresholds;
|