@idmission/websdk-core 0.2.0-rc.2 → 0.5.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.
Files changed (209) hide show
  1. package/README.md +27 -1
  2. package/dist/attestation/clientAttestation.d.ts +5 -0
  3. package/dist/attestation/clientAttestation.d.ts.map +1 -1
  4. package/dist/attestation/clientAttestation.js +4 -4
  5. package/dist/attestation/collectClientAttestation.d.ts +3 -1
  6. package/dist/attestation/collectClientAttestation.d.ts.map +1 -1
  7. package/dist/attestation/collectClientAttestation.js +4 -3
  8. package/dist/camera/acquireCamera.d.ts +5 -0
  9. package/dist/camera/acquireCamera.d.ts.map +1 -1
  10. package/dist/camera/acquireCamera.js +25 -5
  11. package/dist/captureProgress/actor.d.ts +30 -0
  12. package/dist/captureProgress/actor.d.ts.map +1 -0
  13. package/dist/captureProgress/actor.js +754 -0
  14. package/dist/captureProgress/encoder.d.ts +7 -0
  15. package/dist/captureProgress/encoder.d.ts.map +1 -0
  16. package/dist/captureProgress/encoder.js +195 -0
  17. package/dist/captureProgress/index.d.ts +4 -0
  18. package/dist/captureProgress/index.d.ts.map +1 -0
  19. package/dist/captureProgress/index.js +45 -0
  20. package/dist/captureProgress/internal.d.ts +3 -0
  21. package/dist/captureProgress/internal.d.ts.map +1 -0
  22. package/dist/captureProgress/internal.js +1 -0
  23. package/dist/captureProgress/protocol.d.ts +224 -0
  24. package/dist/captureProgress/protocol.d.ts.map +1 -0
  25. package/dist/captureProgress/protocol.js +106 -0
  26. package/dist/captureProgress/recorder.d.ts +32 -0
  27. package/dist/captureProgress/recorder.d.ts.map +1 -0
  28. package/dist/captureProgress/recorder.js +554 -0
  29. package/dist/captureProgress/transport.d.ts +33 -0
  30. package/dist/captureProgress/transport.d.ts.map +1 -0
  31. package/dist/captureProgress/transport.js +109 -0
  32. package/dist/captureProgress/types.d.ts +35 -0
  33. package/dist/captureProgress/types.d.ts.map +1 -0
  34. package/dist/captureProgress/types.js +27 -0
  35. package/dist/client/IdmissionClient.d.ts +8 -1
  36. package/dist/client/IdmissionClient.d.ts.map +1 -1
  37. package/dist/client/IdmissionClient.js +96 -27
  38. package/dist/client/approval.d.ts +15 -0
  39. package/dist/client/approval.d.ts.map +1 -0
  40. package/dist/client/approval.js +19 -0
  41. package/dist/client/artifacts.d.ts +2 -0
  42. package/dist/client/artifacts.d.ts.map +1 -1
  43. package/dist/client/documentFileMetadata.d.ts +13 -0
  44. package/dist/client/documentFileMetadata.d.ts.map +1 -0
  45. package/dist/client/documentFileMetadata.js +51 -0
  46. package/dist/client/index.d.ts +3 -0
  47. package/dist/client/index.d.ts.map +1 -1
  48. package/dist/client/index.js +2 -0
  49. package/dist/client/payload.d.ts +3 -0
  50. package/dist/client/payload.d.ts.map +1 -1
  51. package/dist/client/payload.js +3 -1
  52. package/dist/client/submissionMetadata.d.ts +4 -1
  53. package/dist/client/submissionMetadata.d.ts.map +1 -1
  54. package/dist/client/submissionMetadata.js +13 -8
  55. package/dist/client/types.d.ts +8 -0
  56. package/dist/client/types.d.ts.map +1 -1
  57. package/dist/client/validateSession.d.ts +2 -0
  58. package/dist/client/validateSession.d.ts.map +1 -1
  59. package/dist/client/validateSession.js +53 -24
  60. package/dist/documentCapture/autoCaptureEngine.d.ts +106 -0
  61. package/dist/documentCapture/autoCaptureEngine.d.ts.map +1 -0
  62. package/dist/documentCapture/autoCaptureEngine.js +475 -0
  63. package/dist/documentCapture/autoCaptureGeometry.d.ts +514 -0
  64. package/dist/documentCapture/autoCaptureGeometry.d.ts.map +1 -0
  65. package/dist/documentCapture/autoCaptureGeometry.js +475 -0
  66. package/dist/documentCapture/autoCaptureGuidance.d.ts +61 -0
  67. package/dist/documentCapture/autoCaptureGuidance.d.ts.map +1 -0
  68. package/dist/documentCapture/autoCaptureGuidance.js +73 -0
  69. package/dist/documentCapture/autoCaptureMetadata.d.ts +193 -0
  70. package/dist/documentCapture/autoCaptureMetadata.d.ts.map +1 -0
  71. package/dist/documentCapture/autoCaptureMetadata.js +200 -0
  72. package/dist/documentCapture/autoCaptureOptions.d.ts +211 -0
  73. package/dist/documentCapture/autoCaptureOptions.d.ts.map +1 -0
  74. package/dist/documentCapture/autoCaptureOptions.js +177 -0
  75. package/dist/documentCapture/autoCaptureShutter.d.ts +162 -0
  76. package/dist/documentCapture/autoCaptureShutter.d.ts.map +1 -0
  77. package/dist/documentCapture/autoCaptureShutter.js +183 -0
  78. package/dist/documentCapture/controller.d.ts +38 -1
  79. package/dist/documentCapture/controller.d.ts.map +1 -1
  80. package/dist/documentCapture/controller.js +620 -40
  81. package/dist/documentCapture/index.d.ts +8 -2
  82. package/dist/documentCapture/index.d.ts.map +1 -1
  83. package/dist/documentCapture/index.js +6 -0
  84. package/dist/documentCapture/reducer.d.ts +26 -1
  85. package/dist/documentCapture/reducer.d.ts.map +1 -1
  86. package/dist/documentCapture/reducer.js +43 -0
  87. package/dist/documentCapture/state.d.ts +118 -0
  88. package/dist/documentCapture/state.d.ts.map +1 -1
  89. package/dist/documentCapture/state.js +24 -0
  90. package/dist/idCapture/controller.d.ts +24 -1
  91. package/dist/idCapture/controller.d.ts.map +1 -1
  92. package/dist/idCapture/controller.js +111 -13
  93. package/dist/idCapture/index.d.ts +1 -1
  94. package/dist/idCapture/index.d.ts.map +1 -1
  95. package/dist/idCapture/index.js +12 -1
  96. package/dist/idCapture/pipeline.d.ts +30 -0
  97. package/dist/idCapture/pipeline.d.ts.map +1 -1
  98. package/dist/idCapture/pipeline.js +33 -10
  99. package/dist/idCapture/state.d.ts +1 -3
  100. package/dist/idCapture/state.d.ts.map +1 -1
  101. package/dist/idCapture/submit.d.ts.map +1 -1
  102. package/dist/idCapture/submit.js +35 -1
  103. package/dist/idCapture/types.d.ts +2 -0
  104. package/dist/idCapture/types.d.ts.map +1 -1
  105. package/dist/index.d.ts +2 -0
  106. package/dist/index.d.ts.map +1 -1
  107. package/dist/index.js +1 -0
  108. package/dist/liveness/challengeGeometry.d.ts +48 -17
  109. package/dist/liveness/challengeGeometry.d.ts.map +1 -1
  110. package/dist/liveness/challengeGeometry.js +101 -54
  111. package/dist/models/DocumentDetection.d.ts +29 -1
  112. package/dist/models/DocumentDetection.d.ts.map +1 -1
  113. package/dist/models/DocumentDetection.js +75 -7
  114. package/dist/models/defaults/DocumentDetectorLiteRT.d.ts +2 -2
  115. package/dist/models/defaults/DocumentDetectorLiteRT.d.ts.map +1 -1
  116. package/dist/models/defaults/DocumentDetectorLiteRT.js +27 -2
  117. package/dist/models/defaults/HandLandmarkLiteRT.d.ts +3 -0
  118. package/dist/models/defaults/HandLandmarkLiteRT.d.ts.map +1 -0
  119. package/dist/models/defaults/HandLandmarkLiteRT.js +5 -0
  120. package/dist/models/defaults/IrisLandmarkLiteRT.d.ts +3 -0
  121. package/dist/models/defaults/IrisLandmarkLiteRT.d.ts.map +1 -0
  122. package/dist/models/defaults/IrisLandmarkLiteRT.js +5 -0
  123. package/dist/models/defaults/PalmDetectionLiteRT.d.ts +3 -0
  124. package/dist/models/defaults/PalmDetectionLiteRT.d.ts.map +1 -0
  125. package/dist/models/defaults/PalmDetectionLiteRT.js +5 -0
  126. package/dist/models/defaults/PoaDocumentDetectorLiteRT.d.ts +3 -0
  127. package/dist/models/defaults/PoaDocumentDetectorLiteRT.d.ts.map +1 -0
  128. package/dist/models/defaults/PoaDocumentDetectorLiteRT.js +19 -0
  129. package/dist/models/defaults/index.d.ts +13 -1
  130. package/dist/models/defaults/index.d.ts.map +1 -1
  131. package/dist/models/defaults/index.js +16 -0
  132. package/dist/models/preloadModels.d.ts +24 -16
  133. package/dist/models/preloadModels.d.ts.map +1 -1
  134. package/dist/models/preloadModels.js +33 -3
  135. package/dist/models/providers/litert/FaceLandmarker.d.ts +8 -1
  136. package/dist/models/providers/litert/FaceLandmarker.d.ts.map +1 -1
  137. package/dist/models/providers/litert/FaceLandmarker.js +95 -3
  138. package/dist/models/providers/litert/HandOverFaceDetector.d.ts +27 -0
  139. package/dist/models/providers/litert/HandOverFaceDetector.d.ts.map +1 -0
  140. package/dist/models/providers/litert/HandOverFaceDetector.js +162 -0
  141. package/dist/models/providers/litert/blazeFacePrePost.js +4 -2
  142. package/dist/models/providers/litert/docDetectPrePost.d.ts.map +1 -1
  143. package/dist/models/providers/litert/docDetectPrePost.js +20 -3
  144. package/dist/models/providers/litert/handLandmarkPrePost.d.ts +20 -0
  145. package/dist/models/providers/litert/handLandmarkPrePost.d.ts.map +1 -0
  146. package/dist/models/providers/litert/handLandmarkPrePost.js +72 -0
  147. package/dist/models/providers/litert/handOverFace.d.ts +44 -0
  148. package/dist/models/providers/litert/handOverFace.d.ts.map +1 -0
  149. package/dist/models/providers/litert/handOverFace.js +139 -0
  150. package/dist/models/providers/litert/irisPrePost.d.ts +38 -0
  151. package/dist/models/providers/litert/irisPrePost.d.ts.map +1 -0
  152. package/dist/models/providers/litert/irisPrePost.js +101 -0
  153. package/dist/models/providers/litert/palmPrePost.d.ts +55 -0
  154. package/dist/models/providers/litert/palmPrePost.d.ts.map +1 -0
  155. package/dist/models/providers/litert/palmPrePost.js +151 -0
  156. package/dist/sdkVersions.d.ts +18 -0
  157. package/dist/sdkVersions.d.ts.map +1 -0
  158. package/dist/sdkVersions.js +8 -0
  159. package/dist/selfieCapture/captureQuality.d.ts +43 -0
  160. package/dist/selfieCapture/captureQuality.d.ts.map +1 -0
  161. package/dist/selfieCapture/captureQuality.js +87 -0
  162. package/dist/selfieCapture/challengeRunner.d.ts +9 -2
  163. package/dist/selfieCapture/challengeRunner.d.ts.map +1 -1
  164. package/dist/selfieCapture/challengeRunner.js +2 -5
  165. package/dist/selfieCapture/controller.d.ts +62 -1
  166. package/dist/selfieCapture/controller.d.ts.map +1 -1
  167. package/dist/selfieCapture/controller.js +470 -105
  168. package/dist/selfieCapture/faceTracking.d.ts +69 -1
  169. package/dist/selfieCapture/faceTracking.d.ts.map +1 -1
  170. package/dist/selfieCapture/faceTracking.js +134 -6
  171. package/dist/selfieCapture/guidance.d.ts +64 -0
  172. package/dist/selfieCapture/guidance.d.ts.map +1 -1
  173. package/dist/selfieCapture/guidance.js +74 -0
  174. package/dist/selfieCapture/index.d.ts +4 -3
  175. package/dist/selfieCapture/index.d.ts.map +1 -1
  176. package/dist/selfieCapture/index.js +2 -2
  177. package/dist/selfieCapture/livenessMachine.d.ts +2 -0
  178. package/dist/selfieCapture/livenessMachine.d.ts.map +1 -1
  179. package/dist/selfieCapture/livenessMachine.js +8 -0
  180. package/dist/selfieCapture/pipeline.d.ts +8 -0
  181. package/dist/selfieCapture/pipeline.d.ts.map +1 -1
  182. package/dist/selfieCapture/pipeline.js +2 -2
  183. package/dist/selfieCapture/reducer.d.ts +7 -0
  184. package/dist/selfieCapture/reducer.d.ts.map +1 -1
  185. package/dist/selfieCapture/reducer.js +45 -4
  186. package/dist/selfieCapture/state.d.ts +6 -0
  187. package/dist/selfieCapture/state.d.ts.map +1 -1
  188. package/dist/selfieCapture/state.js +1 -0
  189. package/dist/signature/frameDecode.d.ts.map +1 -1
  190. package/dist/signature/frameDecode.js +29 -0
  191. package/dist/signature/metadata.d.ts +10 -1
  192. package/dist/signature/metadata.d.ts.map +1 -1
  193. package/dist/signature/quality.d.ts +14 -6
  194. package/dist/signature/quality.d.ts.map +1 -1
  195. package/dist/signature/quality.js +13 -4
  196. package/dist/signatureCapture/controller.d.ts +2 -1
  197. package/dist/signatureCapture/controller.d.ts.map +1 -1
  198. package/dist/signatureCapture/controller.js +118 -23
  199. package/dist/signatureCapture/pipeline.d.ts +14 -2
  200. package/dist/signatureCapture/pipeline.d.ts.map +1 -1
  201. package/dist/signatureCapture/pipeline.js +36 -8
  202. package/dist/signatureCapture/state.d.ts +15 -0
  203. package/dist/signatureCapture/state.d.ts.map +1 -1
  204. package/dist/version.d.ts +1 -1
  205. package/dist/version.d.ts.map +1 -1
  206. package/dist/version.js +1 -1
  207. package/dist/videoIdCapture/controller.d.ts.map +1 -1
  208. package/dist/videoIdCapture/controller.js +64 -3
  209. package/package.json +10 -2
@@ -0,0 +1,162 @@
1
+ // The hand-over-face check selfie capture runs on the frame it is about to
2
+ // send (docs/research/palm-detection-occlusion.md):
3
+ // palm detection, each palm confirmed by the hand-landmark model's
4
+ // hand-presence flag, measured against the head box (handOverFace.ts).
5
+ //
6
+ // Both models run on WASM whatever delegate the device probed. The check runs
7
+ // once per auto-capture, where tens of milliseconds do not matter, and WASM
8
+ // sidesteps the Adreno WebGPU mis-runs that pinned the face mesh to it
9
+ // (FaceLandmarker.ts).
10
+ import { Tensor } from '@litertjs/core';
11
+ import { defaultModelPaths } from '../../defaults';
12
+ import { getCachedModelBuffer, preloadModelDependencies, } from '../../preloadModels';
13
+ import { HAND_LANDMARK_INPUT_SIZE, HAND_PRESENCE_OUTPUT, HAND_PRESENCE_THRESHOLD, handRoiFromPalm, preprocessHandRoi, } from './handLandmarkPrePost';
14
+ import { measureHandOverFace, scaleBox } from './handOverFace';
15
+ import { PALM_INPUT_SIZE, decodePalms, preprocessPalmInput, } from './palmPrePost';
16
+ import { loadModel } from './runtime';
17
+ // Hands come from below and beside the face, so the face-crop pass looks at
18
+ // the face box grown to twice its size (doc 10 §3, variant B).
19
+ const FACE_CROP_SCALE = 2;
20
+ /** Downloads (SRI-verified), compiles and warms both models. */
21
+ export async function loadHandOverFaceDetector() {
22
+ await Promise.all([
23
+ preloadModelDependencies('palmDetectionLiteRT', 'litert'),
24
+ preloadModelDependencies('handLandmarkLiteRT', 'litert'),
25
+ ]);
26
+ const palm = await compile(defaultModelPaths.palmDetectionLiteRT);
27
+ try {
28
+ const landmark = await compile(defaultModelPaths.handLandmarkLiteRT);
29
+ try {
30
+ // A first run pays one-off setup; pay it here rather than on the
31
+ // user's first capture.
32
+ await warmUp(palm, PALM_INPUT_SIZE);
33
+ await warmUp(landmark, HAND_LANDMARK_INPUT_SIZE);
34
+ }
35
+ catch (e) {
36
+ landmark.delete();
37
+ throw e;
38
+ }
39
+ return createDetector(palm, landmark);
40
+ }
41
+ catch (e) {
42
+ palm.delete();
43
+ throw e;
44
+ }
45
+ }
46
+ async function compile(path) {
47
+ const { model } = await loadModel(getCachedModelBuffer(path) ?? path, 'wasm');
48
+ return model;
49
+ }
50
+ // Runs `model` on `input` and hands its outputs to `read`. Both tensors are
51
+ // freed however it ends, a rejected run included.
52
+ async function run(model, input, read) {
53
+ const outputs = [];
54
+ try {
55
+ outputs.push(...(await model.run(input)));
56
+ return await read(outputs);
57
+ }
58
+ finally {
59
+ input.delete();
60
+ outputs.forEach((t) => t.delete());
61
+ }
62
+ }
63
+ async function warmUp(model, size) {
64
+ const input = Tensor.fromTypedArray(new Float32Array(size * size * 3), [
65
+ 1,
66
+ size,
67
+ size,
68
+ 3,
69
+ ]);
70
+ await run(model, input, async () => { });
71
+ }
72
+ function createDetector(palm, landmark) {
73
+ // Checks run one at a time, and disposal waits for the last of them.
74
+ let queue = Promise.resolve();
75
+ let disposed = false;
76
+ return {
77
+ check(frame, face, threshold) {
78
+ if (disposed)
79
+ return Promise.reject(new Error('hand-over-face detector disposed'));
80
+ const result = queue.then(() => measure(palm, landmark, frame, face, threshold));
81
+ queue = result.catch(() => { });
82
+ return result;
83
+ },
84
+ dispose() {
85
+ if (disposed)
86
+ return;
87
+ disposed = true;
88
+ void queue.then(() => {
89
+ palm.delete();
90
+ landmark.delete();
91
+ });
92
+ },
93
+ };
94
+ }
95
+ // The face crop runs first: at selfie framing a hand over the mouth can be too
96
+ // small to detect in the whole frame. The whole frame catches the hands that
97
+ // crop cuts through, and only has to run when the crop found nothing.
98
+ async function measure(palm, landmark, frame, face, threshold) {
99
+ const t0 = performance.now();
100
+ const size = { width: frame.width, height: frame.height };
101
+ const crop = scaleBox(face, FACE_CROP_SCALE);
102
+ const hands = async (within) => confirmHands(landmark, frame, await detectPalms(palm, frame, within));
103
+ let result = measureHandOverFace(await hands(crop), face, size, {
104
+ threshold,
105
+ crop,
106
+ });
107
+ if (!result.handOverFace) {
108
+ const whole = measureHandOverFace(await hands(), face, size, { threshold });
109
+ if (whole.coverage > result.coverage)
110
+ result = whole;
111
+ }
112
+ return {
113
+ coverage: result.coverage,
114
+ handOverFace: result.handOverFace,
115
+ inferenceMs: performance.now() - t0,
116
+ };
117
+ }
118
+ /** Palms in `source` (or `crop` of it), in source-frame [0, 1] coordinates. */
119
+ export async function detectPalms(model, source, crop, minScore) {
120
+ const { data, transform } = preprocessPalmInput(source, crop);
121
+ const input = Tensor.fromTypedArray(data, [
122
+ 1,
123
+ PALM_INPUT_SIZE,
124
+ PALM_INPUT_SIZE,
125
+ 3,
126
+ ]);
127
+ return run(model, input, async (outputs) => {
128
+ const [a, b] = await Promise.all(outputs.map((t) => t.data()));
129
+ // Identity [1,2016,18] regressors, Identity_1 [1,2016,1] logits.
130
+ const [regressors, classifiers] = a.length > b.length ? [a, b] : [b, a];
131
+ return decodePalms(regressors, classifiers, transform, minScore);
132
+ });
133
+ }
134
+ /** The hand-landmark model's hand-presence probability for `palm`'s hand. */
135
+ export async function handPresence(model, source, palm) {
136
+ const data = preprocessHandRoi(source, handRoiFromPalm(palm, frameSize(source)));
137
+ const size = HAND_LANDMARK_INPUT_SIZE;
138
+ const input = Tensor.fromTypedArray(data, [1, size, size, 3]);
139
+ return run(model, input, async (outputs) => {
140
+ const i = model
141
+ .getOutputDetails()
142
+ .findIndex((d) => d.name === HAND_PRESENCE_OUTPUT);
143
+ if (i < 0)
144
+ throw new Error(`hand landmark model has no ${HAND_PRESENCE_OUTPUT} output`);
145
+ return (await outputs[i].data())[0];
146
+ });
147
+ }
148
+ // The palm detector alone fires on faces; only palms the hand-landmark model
149
+ // also sees a hand in count.
150
+ async function confirmHands(landmark, source, palms) {
151
+ const hands = [];
152
+ for (const palm of palms) {
153
+ if ((await handPresence(landmark, source, palm)) >= HAND_PRESENCE_THRESHOLD)
154
+ hands.push(palm);
155
+ }
156
+ return hands;
157
+ }
158
+ function frameSize(source) {
159
+ return source instanceof HTMLVideoElement
160
+ ? { width: source.videoWidth, height: source.videoHeight }
161
+ : { width: source.width, height: source.height };
162
+ }
@@ -183,14 +183,16 @@ function blendCluster(cluster) {
183
183
  let yMin = 0;
184
184
  let xMax = 0;
185
185
  let yMax = 0;
186
- const kp = Array.from({ length: NUM_KEYPOINTS }, () => ({ x: 0, y: 0 }));
186
+ // Keypoint count comes from the detections, so the 7-keypoint palm detector
187
+ // (palmPrePost.ts) shares this NMS.
188
+ const kp = Array.from({ length: cluster[0].keypoints.length }, () => ({ x: 0, y: 0 }));
187
189
  for (const d of cluster) {
188
190
  const w = d.score / totalScore;
189
191
  xMin += d.xMin * w;
190
192
  yMin += d.yMin * w;
191
193
  xMax += d.xMax * w;
192
194
  yMax += d.yMax * w;
193
- for (let k = 0; k < NUM_KEYPOINTS; k++) {
195
+ for (let k = 0; k < kp.length; k++) {
194
196
  kp[k].x += d.keypoints[k].x * w;
195
197
  kp[k].y += d.keypoints[k].y * w;
196
198
  }
@@ -1 +1 @@
1
- {"version":3,"file":"docDetectPrePost.d.ts","sourceRoot":"","sources":["../../../../src/models/providers/litert/docDetectPrePost.ts"],"names":[],"mappings":"AAgCA,eAAO,MAAM,qBAAqB,MAAM,CAAC;AAYzC,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CA0BvD,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CACpD,CAAC;AAEzB,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IAGd,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAyBF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,gBAAgB,GAAG,iBAAiB,GAC3C,UAAU,CAAC,WAAW,CAAC,CAgBzB;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,YAAY,EAAE,YAAY,CAAC;IAC3B,gBAAgB,EAAE,YAAY,CAAC;IAC/B,OAAO,EAAE,YAAY,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;CAC1D,CAAC;AA4BF,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,gBAAgB,GACxB,YAAY,EAAE,CA2FhB"}
1
+ {"version":3,"file":"docDetectPrePost.d.ts","sourceRoot":"","sources":["../../../../src/models/providers/litert/docDetectPrePost.ts"],"names":[],"mappings":"AAqCA,eAAO,MAAM,qBAAqB,MAAM,CAAC;AAYzC,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAsCvD,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CACpD,CAAC;AAEzB,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IAGd,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAyBF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,gBAAgB,GAAG,iBAAiB,GAC3C,UAAU,CAAC,WAAW,CAAC,CAgBzB;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,YAAY,EAAE,YAAY,CAAC;IAC3B,gBAAgB,EAAE,YAAY,CAAC;IAC/B,OAAO,EAAE,YAAY,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;CAC1D,CAAC;AA4BF,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,gBAAgB,GACxB,YAAY,EAAE,CA2FhB"}
@@ -13,7 +13,12 @@
13
13
  // `output_N`) and reordered them, so the loader identifies them by shape+dtype,
14
14
  // not name (see providers/litert/DocumentDetection.ts):
15
15
  // box encodings — float32 [1, 1917, 4] (ty, tx, th, tw) deltas
16
- // class predictions — float32 [1, 1917, 23] per-anchor scores (class 0 = background)
16
+ // class predictions — float32 [1, 1917, C] per-anchor scores (class 0 = background)
17
+ // C is export-dependent — the shared
18
+ // detector ships 22, the POA detector 24 —
19
+ // and is derived from the tensor, never
20
+ // assumed. Both are loaded through this
21
+ // same decoder.
17
22
  // anchors — float32 [1917, 4] (ycenter, xcenter, h, w) priors
18
23
  //
19
24
  // This export is NOT quantized — outputs are float32 and consumed directly,
@@ -35,8 +40,8 @@ export const DOC_DETECT_INPUT_SIZE = 300;
35
40
  // embedded in its .tflite. The two are kept separate on purpose: the models
36
41
  // number their classes differently, so the only safe interop surface is the
37
42
  // resulting label *string* (matched in the shared DocumentDetection.ts).
38
- // The raw model emits class indices 0..22 where 0 is background; the
39
- // remaining 1..22 map directly to these labels (no further offset needed —
43
+ // The raw model emits class indices 0..C-1 where 0 is background; the
44
+ // remaining 1..C-1 map directly to these labels (no further offset needed —
40
45
  // the team's "+1" instruction applies to a different indexing convention
41
46
  // that we don't use here, since our loop already skips background and
42
47
  // preserves the model's 1-based class index).
@@ -65,7 +70,19 @@ export const DOC_DETECT_LABEL_MAP = {
65
70
  // "Single page" resolves to detectedDocumentType="singlePage" in the shared
66
71
  // post-processor (DocumentDetection.ts). The string must match the MediaPipe
67
72
  // model's label for the same concept so both runtimes route identically.
73
+ //
74
+ // ⚠️ NOT carried by the current shared LiteRT export (22 classes since
75
+ // 20260904, so 22 is out of range) nor by the POA one. Under the litert runtime this
76
+ // label is currently unreachable and 'singlePage' never resolves; mediapipe
77
+ // still emits it. Kept mapped so a future export that restores the class
78
+ // routes correctly without a code change.
68
79
  22: "Single page",
80
+ // Added by the 20260801 retrain (#339). Verified in a real browser at
81
+ // classId 23, score 0.777, box [0.025, 0, 0.996, 1.0] on a full POA sheet.
82
+ // Carried ONLY by the POA export (24 classes). The shared export stops at
83
+ // 21, so this is inert for idCapture/passport/selfie and live for
84
+ // documentCapture. An unmapped class would surface as `class_23`.
85
+ 23: "POA",
69
86
  };
70
87
  export const DEFAULT_SCALE_FACTORS = [10.0, 10.0, 5.0, 5.0];
71
88
  let preprocessCanvas = null;
@@ -0,0 +1,20 @@
1
+ import type { Detection } from './blazeFacePrePost';
2
+ export declare const HAND_LANDMARK_INPUT_SIZE = 224;
3
+ export declare const HAND_PRESENCE_OUTPUT = "Identity_1";
4
+ /** MediaPipe's default `min_hand_presence_confidence`. */
5
+ export declare const HAND_PRESENCE_THRESHOLD = 0.5;
6
+ /** Rotated square in frame pixels; `rotation` turns the fingers to the top of the crop. */
7
+ export type HandRoi = {
8
+ cx: number;
9
+ cy: number;
10
+ side: number;
11
+ rotation: number;
12
+ };
13
+ /** MediaPipe's palm → hand ROI, for a palm in frame [0, 1] coordinates. */
14
+ export declare function handRoiFromPalm(palm: Detection, frame: {
15
+ width: number;
16
+ height: number;
17
+ }): HandRoi;
18
+ /** Warp `roi` upright into a 224×224 NHWC float32 buffer in [0, 1]. */
19
+ export declare function preprocessHandRoi(source: HTMLVideoElement | HTMLCanvasElement, roi: HandRoi): Float32Array;
20
+ //# sourceMappingURL=handLandmarkPrePost.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handLandmarkPrePost.d.ts","sourceRoot":"","sources":["../../../../src/models/providers/litert/handLandmarkPrePost.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAGnD,eAAO,MAAM,wBAAwB,MAAM,CAAA;AAC3C,eAAO,MAAM,oBAAoB,eAAe,CAAA;AAChD,0DAA0D;AAC1D,eAAO,MAAM,uBAAuB,MAAM,CAAA;AAO1C,2FAA2F;AAC3F,MAAM,MAAM,OAAO,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAA;AAEhF,2EAA2E;AAC3E,wBAAgB,eAAe,CAC7B,IAAI,EAAE,SAAS,EACf,KAAK,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACvC,OAAO,CAeT;AAgBD,uEAAuE;AACvE,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,gBAAgB,GAAG,iBAAiB,EAC5C,GAAG,EAAE,OAAO,GACX,YAAY,CAoBd"}
@@ -0,0 +1,72 @@
1
+ // MediaPipe hand landmark (`hand_landmark_lite` / `_full`) pre-processing for
2
+ // direct .tflite inference via @litertjs/core — used for its hand-presence
3
+ // flag, the stage MediaPipe's hand pipeline relies on to reject false palms.
4
+ // The palm detector alone fires on faces: a bare face scored 0.5–0.8 against
5
+ // real hands' 0.8–0.97, so no palm-score cutoff separates them. Measured
6
+ // 2026-09-13, hand presence scored 0.986–0.997 on every real hand and
7
+ // 0.001–0.027 on every false palm.
8
+ //
9
+ // Input `input_1` float32 [1, 224, 224, 3] in [0, 1], like the palm detector
10
+ // ([-1, 1] let one false palm reach 0.43). Outputs: `Identity` [1, 63] image
11
+ // landmarks, `Identity_1` [1, 1] hand presence — already a probability —
12
+ // `Identity_2` handedness, `Identity_3` world landmarks.
13
+ import { PALM_MIDDLE_MCP, PALM_WRIST } from './palmPrePost';
14
+ export const HAND_LANDMARK_INPUT_SIZE = 224;
15
+ export const HAND_PRESENCE_OUTPUT = 'Identity_1';
16
+ /** MediaPipe's default `min_hand_presence_confidence`. */
17
+ export const HAND_PRESENCE_THRESHOLD = 0.5;
18
+ // palm_detection_detection_to_roi.pbtxt: a square 2.6× the palm, shifted half
19
+ // a palm toward the fingers.
20
+ const ROI_SCALE = 2.6;
21
+ const ROI_SHIFT = 0.5;
22
+ /** MediaPipe's palm → hand ROI, for a palm in frame [0, 1] coordinates. */
23
+ export function handRoiFromPalm(palm, frame) {
24
+ const w = (palm.xMax - palm.xMin) * frame.width;
25
+ const h = (palm.yMax - palm.yMin) * frame.height;
26
+ const wrist = palm.keypoints[PALM_WRIST];
27
+ const mcp = palm.keypoints[PALM_MIDDLE_MCP];
28
+ // Target angle 90°: fingers straight up in the image → rotation 0.
29
+ const rotation = Math.PI / 2 -
30
+ Math.atan2(-(mcp.y - wrist.y) * frame.height, (mcp.x - wrist.x) * frame.width);
31
+ return {
32
+ cx: ((palm.xMin + palm.xMax) / 2) * frame.width + ROI_SHIFT * h * Math.sin(rotation),
33
+ cy: ((palm.yMin + palm.yMax) / 2) * frame.height - ROI_SHIFT * h * Math.cos(rotation),
34
+ side: Math.max(w, h) * ROI_SCALE,
35
+ rotation,
36
+ };
37
+ }
38
+ let preprocessCtx = null;
39
+ function getPreprocessContext() {
40
+ if (!preprocessCtx) {
41
+ const canvas = document.createElement('canvas');
42
+ canvas.width = HAND_LANDMARK_INPUT_SIZE;
43
+ canvas.height = HAND_LANDMARK_INPUT_SIZE;
44
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
45
+ if (!ctx)
46
+ throw new Error('handLandmarkPrePost: 2d context unavailable');
47
+ preprocessCtx = ctx;
48
+ }
49
+ return preprocessCtx;
50
+ }
51
+ /** Warp `roi` upright into a 224×224 NHWC float32 buffer in [0, 1]. */
52
+ export function preprocessHandRoi(source, roi) {
53
+ const S = HAND_LANDMARK_INPUT_SIZE;
54
+ const ctx = getPreprocessContext();
55
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
56
+ ctx.fillStyle = 'black';
57
+ ctx.fillRect(0, 0, S, S);
58
+ // Frame → crop: centre the ROI, scale it to the input, undo its rotation.
59
+ ctx.translate(S / 2, S / 2);
60
+ ctx.scale(S / roi.side, S / roi.side);
61
+ ctx.rotate(-roi.rotation);
62
+ ctx.translate(-roi.cx, -roi.cy);
63
+ ctx.drawImage(source, 0, 0);
64
+ const pixels = ctx.getImageData(0, 0, S, S).data;
65
+ const data = new Float32Array(S * S * 3);
66
+ for (let i = 0, j = 0; i < pixels.length; i += 4, j += 3) {
67
+ data[j] = pixels[i] / 255;
68
+ data[j + 1] = pixels[i + 1] / 255;
69
+ data[j + 2] = pixels[i + 2] / 255;
70
+ }
71
+ return data;
72
+ }
@@ -0,0 +1,44 @@
1
+ import type { Detection } from './blazeFacePrePost';
2
+ import { type Box } from './palmPrePost';
3
+ type Point = {
4
+ x: number;
5
+ y: number;
6
+ };
7
+ export declare const DEFAULT_HEAD_EXTENSION = 0.45;
8
+ export declare const DEFAULT_HAND_COVERAGE_THRESHOLD = 0.1;
9
+ export type HandOverFace = {
10
+ /** Largest fraction of the head box (`headBox`) covered by any detected hand; 0 when none. */
11
+ coverage: number;
12
+ handOverFace: boolean;
13
+ /**
14
+ * Pixel-space quad of the hand that produced `coverage` — or of the first
15
+ * detected hand when none overlaps, so a diagnostic can tell "not detected"
16
+ * from "detected, not covering". Null only when no palm was detected.
17
+ */
18
+ region: Point[] | null;
19
+ /** Palm detection score behind `region`. */
20
+ score: number | null;
21
+ };
22
+ /**
23
+ * `palms` in frame [0, 1] coordinates (`decodePalms`), `face` in frame pixels
24
+ * (`Face.box`). Pass the variant-B `crop` (frame pixels) so a palm whose wrist
25
+ * fell outside it keeps its un-extended box — the orientation estimate needs
26
+ * the wrist keypoint, which a crop can cut off.
27
+ */
28
+ export declare function measureHandOverFace(palms: Detection[], face: Box, frame: {
29
+ width: number;
30
+ height: number;
31
+ }, options?: {
32
+ threshold?: number;
33
+ crop?: Box;
34
+ headExtension?: number;
35
+ }): HandOverFace;
36
+ /**
37
+ * The region coverage is measured on: the face box extended upward over the
38
+ * forehead and hair by `extension` face heights, clamped to the top of the frame.
39
+ */
40
+ export declare function headBox(face: Box, extension?: number): Box;
41
+ /** `b` scaled by `s` about its centre. */
42
+ export declare function scaleBox(b: Box, s: number): Box;
43
+ export {};
44
+ //# sourceMappingURL=handOverFace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handOverFace.d.ts","sourceRoot":"","sources":["../../../../src/models/providers/litert/handOverFace.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,KAAK,GAAG,EAA+B,MAAM,eAAe,CAAA;AAErE,KAAK,KAAK,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAcrC,eAAO,MAAM,sBAAsB,OAAO,CAAA;AAC1C,eAAO,MAAM,+BAA+B,MAAM,CAAA;AAElD,MAAM,MAAM,YAAY,GAAG;IACzB,8FAA8F;IAC9F,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,EAAE,OAAO,CAAA;IACrB;;;;OAIG;IACH,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAA;IACtB,4CAA4C;IAC5C,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CACrB,CAAA;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,SAAS,EAAE,EAClB,IAAI,EAAE,GAAG,EACT,KAAK,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EACxC,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,GAAG,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAO,GACvE,YAAY,CA0Bd;AAoBD;;;GAGG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,SAAyB,GAAG,GAAG,CAG1E;AAED,0CAA0C;AAC1C,wBAAgB,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,GAAG,CAI/C"}
@@ -0,0 +1,139 @@
1
+ // Hand-over-face signal for the palm-detection occlusion experiment
2
+ // (docs/research/palm-detection-occlusion.md §3).
3
+ //
4
+ // Palm detection boxes the palm/back of the hand, not the hand, so fingers
5
+ // pressed over a mouth or eyes sit outside the box. The box is grown into a
6
+ // hand region along the wrist → middle-MCP direction, the same orientation
7
+ // MediaPipe builds its hand ROI from; MediaPipe's ROI is a 2.6× square because
8
+ // it is a landmark crop with margin, a gate wants the hand itself.
9
+ //
10
+ // Coverage alone decides. Doc 10 also asked for the palm centre to sit inside
11
+ // the face box grown 25%, against phone-to-ear false positives, but a hand
12
+ // over the mouth or chin puts its palm BELOW the face with only the fingers on
13
+ // it — the guard rejected exactly that pose on a live camera. A hand beside
14
+ // the face with its fingers up does not overlap the face box, so the coverage
15
+ // threshold already covers the phone-to-ear case.
16
+ import { PALM_MIDDLE_MCP, PALM_WRIST } from './palmPrePost';
17
+ // Hand region in palm-box sides: 1.3 wide, 2 long (palm + fingers), centred
18
+ // half a palm toward the fingers.
19
+ const HAND_WIDTH = 1.3;
20
+ const HAND_LENGTH = 2;
21
+ const FINGER_SHIFT = 0.5;
22
+ // The face box is the 468-point mesh's bbox, which stops mid-forehead, so a
23
+ // hand resting on the hair covered nothing it measured. Published head
24
+ // measurements put the skull top 0.38 face-box heights above the box for the
25
+ // average man and 0.46 for the average woman (MediaPipe's canonical mesh scaled
26
+ // to adult eye-corner distances); hair tops on one live webcam sat 0.34–0.43.
27
+ // 0.45 reaches the average woman's skull — a box a little high on a man only
28
+ // counts a hand hovering just above his head, the rarer mistake.
29
+ export const DEFAULT_HEAD_EXTENSION = 0.45;
30
+ export const DEFAULT_HAND_COVERAGE_THRESHOLD = 0.1;
31
+ /**
32
+ * `palms` in frame [0, 1] coordinates (`decodePalms`), `face` in frame pixels
33
+ * (`Face.box`). Pass the variant-B `crop` (frame pixels) so a palm whose wrist
34
+ * fell outside it keeps its un-extended box — the orientation estimate needs
35
+ * the wrist keypoint, which a crop can cut off.
36
+ */
37
+ export function measureHandOverFace(palms, face, frame, options = {}) {
38
+ const threshold = options.threshold ?? DEFAULT_HAND_COVERAGE_THRESHOLD;
39
+ const head = headBox(face, options.headExtension);
40
+ const headArea = (head.xMax - head.xMin) * (head.yMax - head.yMin);
41
+ let best = { coverage: 0, handOverFace: false, region: null, score: null };
42
+ if (headArea <= 0)
43
+ return best;
44
+ for (const palm of palms) {
45
+ const toPx = (p) => ({ x: p.x * frame.width, y: p.y * frame.height });
46
+ const min = toPx({ x: palm.xMin, y: palm.yMin });
47
+ const max = toPx({ x: palm.xMax, y: palm.yMax });
48
+ const centre = { x: (min.x + max.x) / 2, y: (min.y + max.y) / 2 };
49
+ const wrist = toPx(palm.keypoints[PALM_WRIST]);
50
+ const mcp = toPx(palm.keypoints[PALM_MIDDLE_MCP]);
51
+ const wristCut = options.crop !== undefined && !inside(wrist, options.crop);
52
+ const region = wristCut
53
+ ? boxCorners({ xMin: min.x, yMin: min.y, xMax: max.x, yMax: max.y })
54
+ : handRegion(centre, Math.max(max.x - min.x, max.y - min.y), wrist, mcp);
55
+ const coverage = polygonArea(clipToBox(region, head)) / headArea;
56
+ if (best.region === null || coverage > best.coverage) {
57
+ best = { coverage, handOverFace: coverage > threshold, region, score: palm.score };
58
+ }
59
+ }
60
+ return best;
61
+ }
62
+ function handRegion(centre, side, wrist, mcp) {
63
+ const dx = mcp.x - wrist.x;
64
+ const dy = mcp.y - wrist.y;
65
+ const len = Math.hypot(dx, dy);
66
+ // Degenerate keypoints: assume an upright hand (fingers up in the image).
67
+ const u = len > 1e-6 ? { x: dx / len, y: dy / len } : { x: 0, y: -1 };
68
+ const v = { x: -u.y, y: u.x };
69
+ const c = { x: centre.x + FINGER_SHIFT * side * u.x, y: centre.y + FINGER_SHIFT * side * u.y };
70
+ const hl = (HAND_LENGTH * side) / 2;
71
+ const hw = (HAND_WIDTH * side) / 2;
72
+ return [
73
+ { x: c.x + hl * u.x + hw * v.x, y: c.y + hl * u.y + hw * v.y },
74
+ { x: c.x + hl * u.x - hw * v.x, y: c.y + hl * u.y - hw * v.y },
75
+ { x: c.x - hl * u.x - hw * v.x, y: c.y - hl * u.y - hw * v.y },
76
+ { x: c.x - hl * u.x + hw * v.x, y: c.y - hl * u.y + hw * v.y },
77
+ ];
78
+ }
79
+ /**
80
+ * The region coverage is measured on: the face box extended upward over the
81
+ * forehead and hair by `extension` face heights, clamped to the top of the frame.
82
+ */
83
+ export function headBox(face, extension = DEFAULT_HEAD_EXTENSION) {
84
+ const yMin = Math.max(0, face.yMin - extension * (face.yMax - face.yMin));
85
+ return { xMin: face.xMin, yMin, xMax: face.xMax, yMax: face.yMax };
86
+ }
87
+ /** `b` scaled by `s` about its centre. */
88
+ export function scaleBox(b, s) {
89
+ const gx = ((b.xMax - b.xMin) * (s - 1)) / 2;
90
+ const gy = ((b.yMax - b.yMin) * (s - 1)) / 2;
91
+ return { xMin: b.xMin - gx, yMin: b.yMin - gy, xMax: b.xMax + gx, yMax: b.yMax + gy };
92
+ }
93
+ function inside(p, b) {
94
+ return p.x >= b.xMin && p.x <= b.xMax && p.y >= b.yMin && p.y <= b.yMax;
95
+ }
96
+ function boxCorners(b) {
97
+ return [
98
+ { x: b.xMin, y: b.yMin },
99
+ { x: b.xMax, y: b.yMin },
100
+ { x: b.xMax, y: b.yMax },
101
+ { x: b.xMin, y: b.yMax },
102
+ ];
103
+ }
104
+ // Sutherland–Hodgman against the box's four half-planes; the region is convex.
105
+ function clipToBox(polygon, b) {
106
+ const distances = [
107
+ (p) => p.x - b.xMin,
108
+ (p) => b.xMax - p.x,
109
+ (p) => p.y - b.yMin,
110
+ (p) => b.yMax - p.y,
111
+ ];
112
+ let out = polygon;
113
+ for (const dist of distances) {
114
+ const input = out;
115
+ out = [];
116
+ for (let i = 0; i < input.length; i++) {
117
+ const a = input[i];
118
+ const c = input[(i + 1) % input.length];
119
+ const da = dist(a);
120
+ const dc = dist(c);
121
+ if (da >= 0)
122
+ out.push(a);
123
+ if (da >= 0 !== dc >= 0) {
124
+ const t = da / (da - dc);
125
+ out.push({ x: a.x + t * (c.x - a.x), y: a.y + t * (c.y - a.y) });
126
+ }
127
+ }
128
+ }
129
+ return out;
130
+ }
131
+ function polygonArea(polygon) {
132
+ let twice = 0;
133
+ for (let i = 0; i < polygon.length; i++) {
134
+ const a = polygon[i];
135
+ const c = polygon[(i + 1) % polygon.length];
136
+ twice += a.x * c.y - c.x * a.y;
137
+ }
138
+ return Math.abs(twice) / 2;
139
+ }
@@ -0,0 +1,38 @@
1
+ export declare const IRIS_INPUT_SIZE = 64;
2
+ /** Length of the eye-contour output tensor: 71 points x (x, y, z). */
3
+ export declare const EYE_CONTOUR_LENGTH: number;
4
+ type Point = {
5
+ x: number;
6
+ y: number;
7
+ };
8
+ /** One eye as the iris model sees it. */
9
+ export type IrisEye = {
10
+ /** Mesh indices of the two corners the crop is built from. */
11
+ corners: readonly [number, number];
12
+ /** Mirror the crop (MediaPipe's IS_RIGHT_EYE). */
13
+ flip: boolean;
14
+ /**
15
+ * Mesh indices the first 16 contour points replace, in contour order: the
16
+ * lower lid corner to corner, then the upper lid between the corners.
17
+ */
18
+ meshIndices: readonly number[];
19
+ };
20
+ export declare const IRIS_EYES: readonly IrisEye[];
21
+ /** A square eye crop in source-frame pixels, rotated to level the corners. */
22
+ export type EyeRoi = {
23
+ cx: number;
24
+ cy: number;
25
+ side: number;
26
+ angle: number;
27
+ flip: boolean;
28
+ };
29
+ export declare function eyeRoi(a: Point, b: Point, flip: boolean): EyeRoi;
30
+ /** Draws the eye's ROI from `source` into a 64x64 crop, as [0, 1] RGB. */
31
+ export declare function preprocessEye(source: HTMLVideoElement | HTMLCanvasElement, roi: EyeRoi): Float32Array;
32
+ /**
33
+ * Maps the first `count` contour points from crop pixels back to source-frame
34
+ * pixels: the inverse of preprocessEye's transform.
35
+ */
36
+ export declare function projectEyeContour(contour: Float32Array, roi: EyeRoi, count: number): Point[];
37
+ export {};
38
+ //# sourceMappingURL=irisPrePost.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"irisPrePost.d.ts","sourceRoot":"","sources":["../../../../src/models/providers/litert/irisPrePost.ts"],"names":[],"mappings":"AAkBA,eAAO,MAAM,eAAe,KAAK,CAAA;AACjC,sEAAsE;AACtE,eAAO,MAAM,kBAAkB,QAAS,CAAA;AAGxC,KAAK,KAAK,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAErC,yCAAyC;AACzC,MAAM,MAAM,OAAO,GAAG;IACpB,8DAA8D;IAC9D,OAAO,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAClC,kDAAkD;IAClD,IAAI,EAAE,OAAO,CAAA;IACb;;;OAGG;IACH,WAAW,EAAE,SAAS,MAAM,EAAE,CAAA;CAC/B,CAAA;AAED,eAAO,MAAM,SAAS,EAAE,SAAS,OAAO,EAiBvC,CAAA;AAED,8EAA8E;AAC9E,MAAM,MAAM,MAAM,GAAG;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,OAAO,CAAA;CACd,CAAA;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,GAAG,MAAM,CAQhE;AAgBD,0EAA0E;AAC1E,wBAAgB,aAAa,CAC3B,MAAM,EAAE,gBAAgB,GAAG,iBAAiB,EAC5C,GAAG,EAAE,MAAM,GACV,YAAY,CAsBd;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,YAAY,EACrB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,GACZ,KAAK,EAAE,CAWT"}
@@ -0,0 +1,101 @@
1
+ // Pre/post-processing for MediaPipe's `iris_landmark.tflite`, run on each eye
2
+ // after the 468-point face mesh to sharpen its eye outlines.
3
+ //
4
+ // The hosted mesh (face_landmark.tflite) moves both eyelids together: a wink
5
+ // reads nearly as open as the open eye, so no eye-openness threshold on the
6
+ // mesh alone can see a single closed eye. MediaPipe's own pipeline solves this
7
+ // by running this model on a crop of each eye and writing its eyelid contour
8
+ // back over the mesh (update_face_landmarks_calculator.cc); this module is the
9
+ // geometry for that step.
10
+ //
11
+ // The contract comes from MediaPipe's graphs, not guesswork:
12
+ // - ROI (iris_landmark_landmarks_to_roi.pbtxt): a square 2.3x the span of
13
+ // the eye's two corner landmarks, rotated so the corners are level.
14
+ // - Input (iris_landmark_cpu.pbtxt): 64x64 RGB in [0, 1]
15
+ // (`zero_center: false`); the 362/263 eye is mirrored horizontally.
16
+ // - Output: 71 eye-contour points (x, y, z) in crop pixels, plus 5 iris
17
+ // points in a second tensor. The first 16 contour points are the eyelids.
18
+ export const IRIS_INPUT_SIZE = 64;
19
+ /** Length of the eye-contour output tensor: 71 points x (x, y, z). */
20
+ export const EYE_CONTOUR_LENGTH = 71 * 3;
21
+ const ROI_SCALE = 2.3;
22
+ export const IRIS_EYES = [
23
+ {
24
+ corners: [33, 133],
25
+ flip: false,
26
+ meshIndices: [
27
+ 33, 7, 163, 144, 145, 153, 154, 155, 133, 246, 161, 160, 159, 158, 157,
28
+ 173,
29
+ ],
30
+ },
31
+ {
32
+ corners: [362, 263],
33
+ flip: true,
34
+ meshIndices: [
35
+ 263, 249, 390, 373, 374, 380, 381, 382, 362, 466, 388, 387, 386, 385,
36
+ 384, 398,
37
+ ],
38
+ },
39
+ ];
40
+ export function eyeRoi(a, b, flip) {
41
+ return {
42
+ cx: (a.x + b.x) / 2,
43
+ cy: (a.y + b.y) / 2,
44
+ side: ROI_SCALE * Math.max(Math.abs(b.x - a.x), Math.abs(b.y - a.y)),
45
+ angle: Math.atan2(b.y - a.y, b.x - a.x),
46
+ flip,
47
+ };
48
+ }
49
+ let cropCtx = null;
50
+ function getCropContext() {
51
+ if (!cropCtx) {
52
+ const canvas = document.createElement('canvas');
53
+ canvas.width = IRIS_INPUT_SIZE;
54
+ canvas.height = IRIS_INPUT_SIZE;
55
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
56
+ if (!ctx)
57
+ throw new Error('irisPrePost: 2d context unavailable');
58
+ cropCtx = ctx;
59
+ }
60
+ return cropCtx;
61
+ }
62
+ /** Draws the eye's ROI from `source` into a 64x64 crop, as [0, 1] RGB. */
63
+ export function preprocessEye(source, roi) {
64
+ const ctx = getCropContext();
65
+ const scale = IRIS_INPUT_SIZE / roi.side;
66
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
67
+ ctx.fillStyle = 'black';
68
+ ctx.fillRect(0, 0, IRIS_INPUT_SIZE, IRIS_INPUT_SIZE);
69
+ ctx.translate(IRIS_INPUT_SIZE / 2, IRIS_INPUT_SIZE / 2);
70
+ if (roi.flip)
71
+ ctx.scale(-1, 1);
72
+ ctx.scale(scale, scale);
73
+ ctx.rotate(-roi.angle);
74
+ ctx.translate(-roi.cx, -roi.cy);
75
+ ctx.drawImage(source, 0, 0);
76
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
77
+ const pixels = ctx.getImageData(0, 0, IRIS_INPUT_SIZE, IRIS_INPUT_SIZE).data;
78
+ const out = new Float32Array(IRIS_INPUT_SIZE * IRIS_INPUT_SIZE * 3);
79
+ for (let i = 0, j = 0; i < pixels.length; i += 4, j += 3) {
80
+ out[j] = pixels[i] / 255;
81
+ out[j + 1] = pixels[i + 1] / 255;
82
+ out[j + 2] = pixels[i + 2] / 255;
83
+ }
84
+ return out;
85
+ }
86
+ /**
87
+ * Maps the first `count` contour points from crop pixels back to source-frame
88
+ * pixels: the inverse of preprocessEye's transform.
89
+ */
90
+ export function projectEyeContour(contour, roi, count) {
91
+ const scale = roi.side / IRIS_INPUT_SIZE;
92
+ const cos = Math.cos(roi.angle);
93
+ const sin = Math.sin(roi.angle);
94
+ const points = new Array(count);
95
+ for (let i = 0; i < count; i++) {
96
+ const u = (contour[i * 3] - IRIS_INPUT_SIZE / 2) * scale * (roi.flip ? -1 : 1);
97
+ const v = (contour[i * 3 + 1] - IRIS_INPUT_SIZE / 2) * scale;
98
+ points[i] = { x: roi.cx + u * cos - v * sin, y: roi.cy + u * sin + v * cos };
99
+ }
100
+ return points;
101
+ }