|
@@ -4,506 +4,7 @@
|
|
|
<meta charset="UTF-8" />
|
|
<meta charset="UTF-8" />
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
<title>Eye Tracking Demo · MediaPipe Iris</title>
|
|
<title>Eye Tracking Demo · MediaPipe Iris</title>
|
|
|
-<style>
|
|
|
|
|
- :root {
|
|
|
|
|
- --bg: #0a0a1a;
|
|
|
|
|
- --grid: rgba(0, 255, 255, 0.04);
|
|
|
|
|
- --cyan: #00e5ff;
|
|
|
|
|
- --cyan-dim: #00b8d4;
|
|
|
|
|
- --text: #e0f7fa;
|
|
|
|
|
- --text-dim: #80deea;
|
|
|
|
|
- --danger: #ff5252;
|
|
|
|
|
- --ok: #00e676;
|
|
|
|
|
- --warn: #ffb74d;
|
|
|
|
|
- --panel: rgba(10, 20, 40, 0.55);
|
|
|
|
|
- --panel-border: rgba(0, 229, 255, 0.35);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- * { box-sizing: border-box; margin: 0; padding: 0; }
|
|
|
|
|
-
|
|
|
|
|
- html, body {
|
|
|
|
|
- width: 100%;
|
|
|
|
|
- height: 100%;
|
|
|
|
|
- overflow: hidden;
|
|
|
|
|
- background: var(--bg);
|
|
|
|
|
- color: var(--text);
|
|
|
|
|
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "SF Mono", "Roboto Mono", Menlo, Consolas, monospace;
|
|
|
|
|
- -webkit-font-smoothing: antialiased;
|
|
|
|
|
- user-select: none;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- body {
|
|
|
|
|
- background-color: var(--bg);
|
|
|
|
|
- background-image:
|
|
|
|
|
- linear-gradient(var(--grid) 1px, transparent 1px),
|
|
|
|
|
- linear-gradient(90deg, var(--grid) 1px, transparent 1px);
|
|
|
|
|
- background-size: 40px 40px;
|
|
|
|
|
- background-position: -1px -1px;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* Subtle radial vignette */
|
|
|
|
|
- body::before {
|
|
|
|
|
- content: "";
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- inset: 0;
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- background: radial-gradient(ellipse at center, transparent 0%, rgba(0,0,0,0.6) 100%);
|
|
|
|
|
- z-index: 1;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #stage {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- inset: 0;
|
|
|
|
|
- z-index: 2;
|
|
|
|
|
- cursor: crosshair;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* ---------- Webcam preview ---------- */
|
|
|
|
|
- #preview-wrap {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- left: 24px;
|
|
|
|
|
- bottom: 64px;
|
|
|
|
|
- width: 240px;
|
|
|
|
|
- height: 180px;
|
|
|
|
|
- border-radius: 14px;
|
|
|
|
|
- overflow: hidden;
|
|
|
|
|
- background: #000;
|
|
|
|
|
- border: 1px solid var(--panel-border);
|
|
|
|
|
- box-shadow:
|
|
|
|
|
- 0 0 0 1px rgba(0, 229, 255, 0.08) inset,
|
|
|
|
|
- 0 8px 32px rgba(0, 229, 255, 0.12),
|
|
|
|
|
- 0 2px 8px rgba(0, 0, 0, 0.6);
|
|
|
|
|
- z-index: 10;
|
|
|
|
|
- backdrop-filter: blur(4px);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #preview-wrap::before {
|
|
|
|
|
- content: "LIVE · IRIS";
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- top: 8px;
|
|
|
|
|
- left: 10px;
|
|
|
|
|
- font-size: 10px;
|
|
|
|
|
- letter-spacing: 0.15em;
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- text-shadow: 0 0 8px rgba(0, 229, 255, 0.7);
|
|
|
|
|
- z-index: 12;
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #preview-wrap::after {
|
|
|
|
|
- content: "";
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- inset: 0;
|
|
|
|
|
- border-radius: 14px;
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- box-shadow: 0 0 24px rgba(0, 229, 255, 0.18) inset;
|
|
|
|
|
- z-index: 11;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #webcam, #overlay {
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- inset: 0;
|
|
|
|
|
- width: 100%;
|
|
|
|
|
- height: 100%;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #webcam {
|
|
|
|
|
- object-fit: cover;
|
|
|
|
|
- transform: scaleX(-1); /* mirrored */
|
|
|
|
|
- z-index: 1;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #overlay {
|
|
|
|
|
- z-index: 2;
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* ---------- Status bar ---------- */
|
|
|
|
|
- #status-bar {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- left: 0;
|
|
|
|
|
- right: 0;
|
|
|
|
|
- bottom: 0;
|
|
|
|
|
- height: 48px;
|
|
|
|
|
- display: flex;
|
|
|
|
|
- align-items: center;
|
|
|
|
|
- gap: 24px;
|
|
|
|
|
- padding: 0 20px;
|
|
|
|
|
- background: var(--panel);
|
|
|
|
|
- border-top: 1px solid var(--panel-border);
|
|
|
|
|
- backdrop-filter: blur(8px);
|
|
|
|
|
- font-size: 12px;
|
|
|
|
|
- letter-spacing: 0.05em;
|
|
|
|
|
- z-index: 20;
|
|
|
|
|
- color: var(--text-dim);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- .stat {
|
|
|
|
|
- display: flex;
|
|
|
|
|
- align-items: center;
|
|
|
|
|
- gap: 8px;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- .stat .label {
|
|
|
|
|
- color: rgba(128, 222, 234, 0.55);
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- font-size: 10px;
|
|
|
|
|
- letter-spacing: 0.18em;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- .stat .value {
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- text-shadow: 0 0 8px rgba(0, 229, 255, 0.5);
|
|
|
|
|
- font-variant-numeric: tabular-nums;
|
|
|
|
|
- min-width: 60px;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- .stat .value.ok { color: var(--ok); text-shadow: 0 0 8px rgba(0, 230, 118, 0.4); }
|
|
|
|
|
- .stat .value.warn { color: #ffb74d; text-shadow: 0 0 8px rgba(255, 183, 77, 0.4); }
|
|
|
|
|
- .stat .value.err { color: var(--danger); text-shadow: 0 0 8px rgba(255, 82, 82, 0.4); }
|
|
|
|
|
-
|
|
|
|
|
- .dot {
|
|
|
|
|
- width: 8px;
|
|
|
|
|
- height: 8px;
|
|
|
|
|
- border-radius: 50%;
|
|
|
|
|
- background: var(--cyan-dim);
|
|
|
|
|
- box-shadow: 0 0 8px var(--cyan-dim);
|
|
|
|
|
- animation: pulse 1.6s ease-in-out infinite;
|
|
|
|
|
- }
|
|
|
|
|
- .dot.ok { background: var(--ok); box-shadow: 0 0 10px var(--ok); }
|
|
|
|
|
- .dot.warn { background: #ffb74d; box-shadow: 0 0 10px #ffb74d; }
|
|
|
|
|
- .dot.err { background: var(--danger); box-shadow: 0 0 10px var(--danger); animation: none; }
|
|
|
|
|
-
|
|
|
|
|
- @keyframes pulse {
|
|
|
|
|
- 0%, 100% { opacity: 1; transform: scale(1); }
|
|
|
|
|
- 50% { opacity: 0.4; transform: scale(0.8); }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* Small inline button (used for "Recalibrate" in the status bar) */
|
|
|
|
|
- .btn-mini {
|
|
|
|
|
- font-family: inherit;
|
|
|
|
|
- font-size: 10px;
|
|
|
|
|
- letter-spacing: 0.18em;
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- background: transparent;
|
|
|
|
|
- border: 1px solid var(--panel-border);
|
|
|
|
|
- border-radius: 4px;
|
|
|
|
|
- padding: 4px 10px;
|
|
|
|
|
- cursor: pointer;
|
|
|
|
|
- transition: background 0.2s, box-shadow 0.2s, color 0.2s;
|
|
|
|
|
- }
|
|
|
|
|
- .btn-mini:hover {
|
|
|
|
|
- background: rgba(0, 229, 255, 0.1);
|
|
|
|
|
- box-shadow: 0 0 10px rgba(0, 229, 255, 0.3);
|
|
|
|
|
- color: #ffffff;
|
|
|
|
|
- }
|
|
|
|
|
- .btn-mini:disabled {
|
|
|
|
|
- color: rgba(128, 222, 234, 0.35);
|
|
|
|
|
- border-color: rgba(0, 229, 255, 0.15);
|
|
|
|
|
- cursor: not-allowed;
|
|
|
|
|
- background: transparent;
|
|
|
|
|
- box-shadow: none;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* ---------- Title ---------- */
|
|
|
|
|
- #title {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- top: 18px;
|
|
|
|
|
- left: 24px;
|
|
|
|
|
- z-index: 15;
|
|
|
|
|
- font-size: 11px;
|
|
|
|
|
- letter-spacing: 0.3em;
|
|
|
|
|
- color: rgba(0, 229, 255, 0.7);
|
|
|
|
|
- text-shadow: 0 0 8px rgba(0, 229, 255, 0.4);
|
|
|
|
|
- }
|
|
|
|
|
- #title .accent { color: var(--cyan); }
|
|
|
|
|
-
|
|
|
|
|
- /* ---------- Loading overlay ---------- */
|
|
|
|
|
- #loader {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- inset: 0;
|
|
|
|
|
- z-index: 100;
|
|
|
|
|
- display: flex;
|
|
|
|
|
- flex-direction: column;
|
|
|
|
|
- align-items: center;
|
|
|
|
|
- justify-content: center;
|
|
|
|
|
- gap: 28px;
|
|
|
|
|
- background: rgba(10, 10, 26, 0.92);
|
|
|
|
|
- backdrop-filter: blur(6px);
|
|
|
|
|
- transition: opacity 0.5s ease, visibility 0.5s;
|
|
|
|
|
- }
|
|
|
|
|
- #loader.hidden {
|
|
|
|
|
- opacity: 0;
|
|
|
|
|
- visibility: hidden;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- .spinner {
|
|
|
|
|
- width: 64px;
|
|
|
|
|
- height: 64px;
|
|
|
|
|
- border: 2px solid rgba(0, 229, 255, 0.15);
|
|
|
|
|
- border-top-color: var(--cyan);
|
|
|
|
|
- border-right-color: var(--cyan);
|
|
|
|
|
- border-radius: 50%;
|
|
|
|
|
- animation: spin 1s linear infinite;
|
|
|
|
|
- box-shadow: 0 0 24px rgba(0, 229, 255, 0.3);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @keyframes spin {
|
|
|
|
|
- to { transform: rotate(360deg); }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- .loader-text {
|
|
|
|
|
- font-size: 12px;
|
|
|
|
|
- letter-spacing: 0.4em;
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- text-shadow: 0 0 10px rgba(0, 229, 255, 0.6);
|
|
|
|
|
- }
|
|
|
|
|
- .loader-sub {
|
|
|
|
|
- font-size: 11px;
|
|
|
|
|
- color: var(--text-dim);
|
|
|
|
|
- letter-spacing: 0.15em;
|
|
|
|
|
- max-width: 320px;
|
|
|
|
|
- text-align: center;
|
|
|
|
|
- line-height: 1.6;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* ---------- Error overlay ---------- */
|
|
|
|
|
- #error-overlay {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- inset: 0;
|
|
|
|
|
- z-index: 90;
|
|
|
|
|
- display: none;
|
|
|
|
|
- flex-direction: column;
|
|
|
|
|
- align-items: center;
|
|
|
|
|
- justify-content: center;
|
|
|
|
|
- gap: 18px;
|
|
|
|
|
- background: rgba(10, 10, 26, 0.88);
|
|
|
|
|
- backdrop-filter: blur(6px);
|
|
|
|
|
- padding: 40px;
|
|
|
|
|
- text-align: center;
|
|
|
|
|
- }
|
|
|
|
|
- #error-overlay.show { display: flex; }
|
|
|
|
|
-
|
|
|
|
|
- #error-overlay .icon {
|
|
|
|
|
- font-size: 48px;
|
|
|
|
|
- filter: drop-shadow(0 0 12px rgba(255, 82, 82, 0.6));
|
|
|
|
|
- }
|
|
|
|
|
- #error-overlay h2 {
|
|
|
|
|
- font-size: 18px;
|
|
|
|
|
- letter-spacing: 0.15em;
|
|
|
|
|
- color: var(--danger);
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- }
|
|
|
|
|
- #error-overlay p {
|
|
|
|
|
- color: var(--text-dim);
|
|
|
|
|
- font-size: 13px;
|
|
|
|
|
- max-width: 520px;
|
|
|
|
|
- line-height: 1.7;
|
|
|
|
|
- }
|
|
|
|
|
- #error-overlay button {
|
|
|
|
|
- margin-top: 12px;
|
|
|
|
|
- padding: 10px 24px;
|
|
|
|
|
- background: transparent;
|
|
|
|
|
- border: 1px solid var(--cyan);
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- font-family: inherit;
|
|
|
|
|
- font-size: 12px;
|
|
|
|
|
- letter-spacing: 0.2em;
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- cursor: pointer;
|
|
|
|
|
- border-radius: 6px;
|
|
|
|
|
- transition: background 0.2s, box-shadow 0.2s;
|
|
|
|
|
- }
|
|
|
|
|
- #error-overlay button:hover {
|
|
|
|
|
- background: rgba(0, 229, 255, 0.1);
|
|
|
|
|
- box-shadow: 0 0 16px rgba(0, 229, 255, 0.3);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* ---------- Debug panel (top-left, collapsible) ---------- */
|
|
|
|
|
- #debug-panel {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- top: 48px;
|
|
|
|
|
- left: 24px;
|
|
|
|
|
- z-index: 30;
|
|
|
|
|
- width: 260px;
|
|
|
|
|
- max-width: calc(100vw - 48px);
|
|
|
|
|
- background: var(--panel);
|
|
|
|
|
- border: 1px solid var(--panel-border);
|
|
|
|
|
- border-radius: 10px;
|
|
|
|
|
- backdrop-filter: blur(8px);
|
|
|
|
|
- box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
|
|
|
|
|
- font-size: 11px;
|
|
|
|
|
- color: var(--text);
|
|
|
|
|
- overflow: hidden;
|
|
|
|
|
- transition: max-height 0.25s ease;
|
|
|
|
|
- }
|
|
|
|
|
- #debug-panel.collapsed { max-height: 32px; }
|
|
|
|
|
- #debug-panel.expanded { max-height: 480px; }
|
|
|
|
|
-
|
|
|
|
|
- #debug-header {
|
|
|
|
|
- display: flex;
|
|
|
|
|
- align-items: center;
|
|
|
|
|
- justify-content: space-between;
|
|
|
|
|
- padding: 8px 12px;
|
|
|
|
|
- cursor: pointer;
|
|
|
|
|
- user-select: none;
|
|
|
|
|
- background: rgba(0, 229, 255, 0.06);
|
|
|
|
|
- border-bottom: 1px solid var(--panel-border);
|
|
|
|
|
- font-size: 10px;
|
|
|
|
|
- letter-spacing: 0.2em;
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- }
|
|
|
|
|
- #debug-panel.collapsed #debug-body { display: none; }
|
|
|
|
|
- #debug-panel.collapsed #debug-header { border-bottom: none; }
|
|
|
|
|
- #debug-toggle {
|
|
|
|
|
- font-family: inherit;
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- font-size: 14px;
|
|
|
|
|
- line-height: 1;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #debug-body {
|
|
|
|
|
- padding: 10px 12px 12px;
|
|
|
|
|
- display: flex;
|
|
|
|
|
- flex-direction: column;
|
|
|
|
|
- gap: 6px;
|
|
|
|
|
- max-height: 440px;
|
|
|
|
|
- overflow-y: auto;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- .dbg-row {
|
|
|
|
|
- display: flex;
|
|
|
|
|
- align-items: center;
|
|
|
|
|
- justify-content: space-between;
|
|
|
|
|
- gap: 8px;
|
|
|
|
|
- font-size: 11px;
|
|
|
|
|
- line-height: 1.4;
|
|
|
|
|
- word-break: break-all;
|
|
|
|
|
- }
|
|
|
|
|
- .dbg-row .k {
|
|
|
|
|
- color: rgba(128, 222, 234, 0.7);
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- letter-spacing: 0.1em;
|
|
|
|
|
- font-size: 10px;
|
|
|
|
|
- flex-shrink: 0;
|
|
|
|
|
- }
|
|
|
|
|
- .dbg-row .v {
|
|
|
|
|
- color: var(--text);
|
|
|
|
|
- text-align: right;
|
|
|
|
|
- font-variant-numeric: tabular-nums;
|
|
|
|
|
- }
|
|
|
|
|
- .dbg-row .v.ok { color: var(--ok); }
|
|
|
|
|
- .dbg-row .v.warn { color: #ffb74d; }
|
|
|
|
|
- .dbg-row .v.err { color: var(--danger); }
|
|
|
|
|
- .dbg-row .v.run { color: var(--cyan); }
|
|
|
|
|
-
|
|
|
|
|
- #debug-err {
|
|
|
|
|
- margin-top: 6px;
|
|
|
|
|
- padding: 8px 10px;
|
|
|
|
|
- background: rgba(255, 82, 82, 0.08);
|
|
|
|
|
- border: 1px solid rgba(255, 82, 82, 0.35);
|
|
|
|
|
- border-radius: 6px;
|
|
|
|
|
- color: var(--danger);
|
|
|
|
|
- font-size: 10px;
|
|
|
|
|
- line-height: 1.5;
|
|
|
|
|
- word-break: break-word;
|
|
|
|
|
- display: none;
|
|
|
|
|
- max-height: 120px;
|
|
|
|
|
- overflow-y: auto;
|
|
|
|
|
- }
|
|
|
|
|
- #debug-err.show { display: block; }
|
|
|
|
|
-
|
|
|
|
|
- /* ---------- Calibration overlay (9-point) ---------- */
|
|
|
|
|
- #calibration-overlay {
|
|
|
|
|
- position: fixed;
|
|
|
|
|
- inset: 0;
|
|
|
|
|
- z-index: 80;
|
|
|
|
|
- display: none;
|
|
|
|
|
- align-items: center;
|
|
|
|
|
- justify-content: center;
|
|
|
|
|
- background: rgba(10, 10, 26, 0.92);
|
|
|
|
|
- backdrop-filter: blur(6px);
|
|
|
|
|
- }
|
|
|
|
|
- #calibration-overlay.show { display: flex; }
|
|
|
|
|
-
|
|
|
|
|
- #cal-dot {
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- width: 36px;
|
|
|
|
|
- height: 36px;
|
|
|
|
|
- margin-left: -18px;
|
|
|
|
|
- margin-top: -18px;
|
|
|
|
|
- border-radius: 50%;
|
|
|
|
|
- background: radial-gradient(circle at center,
|
|
|
|
|
- rgba(255, 255, 255, 1) 0%,
|
|
|
|
|
- rgba(0, 229, 255, 1) 35%,
|
|
|
|
|
- rgba(0, 229, 255, 0.6) 65%,
|
|
|
|
|
- rgba(0, 229, 255, 0) 100%);
|
|
|
|
|
- box-shadow:
|
|
|
|
|
- 0 0 24px rgba(0, 229, 255, 0.9),
|
|
|
|
|
- 0 0 60px rgba(0, 229, 255, 0.5);
|
|
|
|
|
- animation: calPulse 1s ease-in-out infinite;
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- transform-origin: center;
|
|
|
|
|
- }
|
|
|
|
|
- #cal-dot::after {
|
|
|
|
|
- content: "";
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- inset: 12px;
|
|
|
|
|
- border-radius: 50%;
|
|
|
|
|
- border: 2px solid rgba(0, 229, 255, 0.6);
|
|
|
|
|
- }
|
|
|
|
|
- @keyframes calPulse {
|
|
|
|
|
- 0%, 100% { transform: scale(1); opacity: 1; }
|
|
|
|
|
- 50% { transform: scale(1.3); opacity: 0.7; }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #cal-text {
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- top: 32px;
|
|
|
|
|
- left: 50%;
|
|
|
|
|
- transform: translateX(-50%);
|
|
|
|
|
- font-size: 13px;
|
|
|
|
|
- letter-spacing: 0.3em;
|
|
|
|
|
- text-transform: uppercase;
|
|
|
|
|
- color: var(--cyan);
|
|
|
|
|
- text-shadow: 0 0 10px rgba(0, 229, 255, 0.7);
|
|
|
|
|
- text-align: center;
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- }
|
|
|
|
|
- #cal-sub {
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- top: 58px;
|
|
|
|
|
- left: 50%;
|
|
|
|
|
- transform: translateX(-50%);
|
|
|
|
|
- font-size: 10px;
|
|
|
|
|
- letter-spacing: 0.2em;
|
|
|
|
|
- color: var(--text-dim);
|
|
|
|
|
- text-align: center;
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- }
|
|
|
|
|
- #cal-cancel {
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- bottom: 32px;
|
|
|
|
|
- left: 50%;
|
|
|
|
|
- transform: translateX(-50%);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /* Faint preview ghost dots for upcoming calibration points */
|
|
|
|
|
- .cal-ghost {
|
|
|
|
|
- position: absolute;
|
|
|
|
|
- width: 14px;
|
|
|
|
|
- height: 14px;
|
|
|
|
|
- margin-left: -7px;
|
|
|
|
|
- margin-top: -7px;
|
|
|
|
|
- border-radius: 50%;
|
|
|
|
|
- border: 1px solid rgba(0, 229, 255, 0.25);
|
|
|
|
|
- background: rgba(0, 229, 255, 0.05);
|
|
|
|
|
- pointer-events: none;
|
|
|
|
|
- }
|
|
|
|
|
-</style>
|
|
|
|
|
|
|
+<link rel="stylesheet" href="css/style.css">
|
|
|
</head>
|
|
</head>
|
|
|
<body>
|
|
<body>
|
|
|
|
|
|
|
@@ -574,6 +75,10 @@
|
|
|
<div class="dbg-row"><span class="k">stage</span><span class="v" id="dbg-stage">…</span></div>
|
|
<div class="dbg-row"><span class="k">stage</span><span class="v" id="dbg-stage">…</span></div>
|
|
|
<div class="dbg-row"><span class="k">calibration</span><span class="v" id="dbg-cal">…</span></div>
|
|
<div class="dbg-row"><span class="k">calibration</span><span class="v" id="dbg-cal">…</span></div>
|
|
|
<div class="dbg-row"><span class="k">frame ms</span><span class="v" id="dbg-frame">--</span></div>
|
|
<div class="dbg-row"><span class="k">frame ms</span><span class="v" id="dbg-frame">--</span></div>
|
|
|
|
|
+ <div class="dbg-row"><span class="k">EAR</span><span class="v" id="dbg-ear">--</span></div>
|
|
|
|
|
+ <div class="dbg-row"><span class="k">blink</span><span class="v" id="dbg-blink">--</span></div>
|
|
|
|
|
+ <div class="dbg-row"><span class="k">pitch</span><span class="v" id="dbg-pitch">--</span></div>
|
|
|
|
|
+ <div class="dbg-row"><span class="k">yaw</span><span class="v" id="dbg-yaw">--</span></div>
|
|
|
<div class="dbg-row"><span class="k">downloaded</span><span class="v" id="dbg-bytes">0 KB</span></div>
|
|
<div class="dbg-row"><span class="k">downloaded</span><span class="v" id="dbg-bytes">0 KB</span></div>
|
|
|
<div id="debug-err"></div>
|
|
<div id="debug-err"></div>
|
|
|
</div>
|
|
</div>
|
|
@@ -593,7 +98,8 @@
|
|
|
<button class="btn-mini" id="cal-cancel">Cancel</button>
|
|
<button class="btn-mini" id="cal-cancel">Cancel</button>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
-<!-- MediaPipe CDN -->
|
|
|
|
|
|
|
+<!-- MediaPipe CDN: these are UMD globals (window.FaceMesh / window.Camera / window.drawConnectors),
|
|
|
|
|
+ so they stay as classic <script> tags and are NOT imported as ES modules. -->
|
|
|
<script>
|
|
<script>
|
|
|
// Mark CDN load state on the global scope so the inline app can read it.
|
|
// Mark CDN load state on the global scope so the inline app can read it.
|
|
|
window.__cdnStatus = window.__cdnStatus || {};
|
|
window.__cdnStatus = window.__cdnStatus || {};
|
|
@@ -628,1447 +134,7 @@
|
|
|
onload="__cdnOk('face_mesh')"
|
|
onload="__cdnOk('face_mesh')"
|
|
|
onerror="__cdnErr('face_mesh', this.src)"></script>
|
|
onerror="__cdnErr('face_mesh', this.src)"></script>
|
|
|
|
|
|
|
|
-<script>
|
|
|
|
|
-(function () {
|
|
|
|
|
- 'use strict';
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Config
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- const TRAIL_LENGTH = 30; // # of trail points
|
|
|
|
|
- const ORB_RADIUS = 40; // px
|
|
|
|
|
- const GRID_DRAW_INTERVAL_MS = 33; // ~30fps redraw
|
|
|
|
|
-
|
|
|
|
|
- // Kalman filter (replaces EMA + outlier threshold — Kalman handles noise naturally via R)
|
|
|
|
|
- // No constant needed here; see kalUpdate / kalInit below.
|
|
|
|
|
-
|
|
|
|
|
- // Calibration config
|
|
|
|
|
- const CAL_SAMPLES_PER_POINT = 30; // samples per point (first 3 are discarded)
|
|
|
|
|
- const CAL_SAMPLE_INTERVAL_MS = 50; // ms between samples
|
|
|
|
|
- const CAL_MARGIN = 0.15; // 15% margin on each edge
|
|
|
|
|
- const CAL_STORAGE_KEY = 'eye-tracking-calibration';
|
|
|
|
|
- const CAL_VERSION = 2; // bump when ratioY normalization or coef layout changes; old payloads are discarded
|
|
|
|
|
-
|
|
|
|
|
- // Iris / eye landmark indices (MediaPipe Face Mesh)
|
|
|
|
|
- // Left eye
|
|
|
|
|
- const L_OUTER = 33, L_INNER = 133;
|
|
|
|
|
- const L_TOP = 159, L_BOT = 145;
|
|
|
|
|
- const L_IRIS = 468;
|
|
|
|
|
- // Right eye
|
|
|
|
|
- const R_OUTER = 362, R_INNER = 263;
|
|
|
|
|
- const R_TOP = 386, R_BOT = 374;
|
|
|
|
|
- const R_IRIS = 473;
|
|
|
|
|
- // Iris contour points
|
|
|
|
|
- const IRIS_LANDMARKS = [
|
|
|
|
|
- 468, 469, 470, 471, 472, // left iris
|
|
|
|
|
- 473, 474, 475, 476, 477 // right iris
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
- // Face mesh tesselation connection subsets (subset of FACE_TESSELATION)
|
|
|
|
|
- // Use eye / brow / lip / nose silhouettes to keep it light
|
|
|
|
|
- const FACE_OVAL = [10, 338, 297, 332, 284, 251, 389, 356, 454, 323, 361, 288, 397, 365, 379, 378, 400, 377, 152, 148, 176, 149, 150, 136, 172, 58, 132, 93, 234, 127, 162, 21, 54, 103, 67, 109];
|
|
|
|
|
- const LIPS = [61, 146, 91, 181, 84, 17, 314, 405, 321, 375, 291, 308, 324, 318, 402, 317, 14, 87, 178, 88, 95, 78];
|
|
|
|
|
- const L_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246];
|
|
|
|
|
- const R_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398];
|
|
|
|
|
- const L_BROW = [46, 53, 52, 65, 55, 107, 66, 105, 63, 70];
|
|
|
|
|
- const R_BROW = [276, 283, 282, 295, 285, 336, 296, 334, 293, 300];
|
|
|
|
|
-
|
|
|
|
|
- // 13-point calibration grid (center + 4 corners + 4 edges + 4 mid-diagonal)
|
|
|
|
|
- // The drawing is mirrored via CSS scaleX(-1), so these coords are
|
|
|
|
|
- // intentionally in the *displayed* (mirrored) frame of reference.
|
|
|
|
|
- // 5 rows × variable cols (5+3 cols) — center column used in rows 0,2,4 only.
|
|
|
|
|
- const CAL_GRID = [
|
|
|
|
|
- // row 0 (top)
|
|
|
|
|
- { x: 0.1, y: 0.1 }, // TL
|
|
|
|
|
- { x: 0.5, y: 0.1 }, // TM
|
|
|
|
|
- { x: 0.9, y: 0.1 }, // TR
|
|
|
|
|
- // row 1 (upper mid)
|
|
|
|
|
- { x: 0.1, y: 0.3 }, // UL-mid
|
|
|
|
|
- { x: 0.9, y: 0.3 }, // UR-mid
|
|
|
|
|
- // row 2 (middle)
|
|
|
|
|
- { x: 0.1, y: 0.5 }, // ML
|
|
|
|
|
- { x: 0.5, y: 0.5 }, // MM (center)
|
|
|
|
|
- { x: 0.9, y: 0.5 }, // MR
|
|
|
|
|
- // row 3 (lower mid)
|
|
|
|
|
- { x: 0.1, y: 0.7 }, // LL-mid
|
|
|
|
|
- { x: 0.9, y: 0.7 }, // LR-mid
|
|
|
|
|
- // row 4 (bottom)
|
|
|
|
|
- { x: 0.1, y: 0.9 }, // BL
|
|
|
|
|
- { x: 0.5, y: 0.9 }, // BM
|
|
|
|
|
- { x: 0.9, y: 0.9 } // BR
|
|
|
|
|
- ];
|
|
|
|
|
- // Order: row-major traversal
|
|
|
|
|
- const CAL_ORDER = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // DOM
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- const stage = document.getElementById('stage');
|
|
|
|
|
- const ctx = stage.getContext('2d');
|
|
|
|
|
- const video = document.getElementById('webcam');
|
|
|
|
|
- const overlay = document.getElementById('overlay');
|
|
|
|
|
- const octx = overlay.getContext('2d');
|
|
|
|
|
- const loader = document.getElementById('loader');
|
|
|
|
|
- const loaderText = document.getElementById('loader-text');
|
|
|
|
|
- const loaderSub = document.getElementById('loader-sub');
|
|
|
|
|
- const errOverlay = document.getElementById('error-overlay');
|
|
|
|
|
- const errTitle = document.getElementById('err-title');
|
|
|
|
|
- const errMsg = document.getElementById('err-msg');
|
|
|
|
|
- const errRetry = document.getElementById('err-retry');
|
|
|
|
|
- const statusDot = document.getElementById('status-dot');
|
|
|
|
|
- const statusText = document.getElementById('status-text');
|
|
|
|
|
- const fpsEl = document.getElementById('fps');
|
|
|
|
|
- const gxEl = document.getElementById('gaze-x');
|
|
|
|
|
- const gyEl = document.getElementById('gaze-y');
|
|
|
|
|
- const ilEl = document.getElementById('iris-l');
|
|
|
|
|
- const irEl = document.getElementById('iris-r');
|
|
|
|
|
- const calDotStatus = document.getElementById('cal-dot-status');
|
|
|
|
|
- const calStatusText = document.getElementById('cal-status-text');
|
|
|
|
|
- const recalibrateBtn = document.getElementById('recalibrate-btn');
|
|
|
|
|
-
|
|
|
|
|
- // Debug panel
|
|
|
|
|
- const dbgPanel = document.getElementById('debug-panel');
|
|
|
|
|
- const dbgHeader = document.getElementById('debug-header');
|
|
|
|
|
- const dbgToggle = document.getElementById('debug-toggle');
|
|
|
|
|
- const dbgCdn1 = document.getElementById('dbg-cdn1');
|
|
|
|
|
- const dbgCdn2 = document.getElementById('dbg-cdn2');
|
|
|
|
|
- const dbgCdn3 = document.getElementById('dbg-cdn3');
|
|
|
|
|
- const dbgModel = document.getElementById('dbg-model');
|
|
|
|
|
- const dbgCamera = document.getElementById('dbg-camera');
|
|
|
|
|
- const dbgStage = document.getElementById('dbg-stage');
|
|
|
|
|
- const dbgCal = document.getElementById('dbg-cal');
|
|
|
|
|
- const dbgFrame = document.getElementById('dbg-frame');
|
|
|
|
|
- const dbgBytes = document.getElementById('dbg-bytes');
|
|
|
|
|
- const dbgErr = document.getElementById('debug-err');
|
|
|
|
|
-
|
|
|
|
|
- // Calibration overlay
|
|
|
|
|
- const calOverlay = document.getElementById('calibration-overlay');
|
|
|
|
|
- const calDot = document.getElementById('cal-dot');
|
|
|
|
|
- const calText = document.getElementById('cal-text');
|
|
|
|
|
- const calSub = document.getElementById('cal-sub');
|
|
|
|
|
- const calCancel = document.getElementById('cal-cancel');
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Debug panel helpers
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function dbgSet(el, text, cls) {
|
|
|
|
|
- if (!el) return;
|
|
|
|
|
- el.textContent = text;
|
|
|
|
|
- el.className = 'v' + (cls ? ' ' + cls : '');
|
|
|
|
|
- }
|
|
|
|
|
- function dbgSetError(msg) {
|
|
|
|
|
- if (!dbgErr) return;
|
|
|
|
|
- if (!msg) { dbgErr.classList.remove('show'); dbgErr.textContent = ''; return; }
|
|
|
|
|
- dbgErr.textContent = msg;
|
|
|
|
|
- dbgErr.classList.add('show');
|
|
|
|
|
- }
|
|
|
|
|
- if (dbgHeader) {
|
|
|
|
|
- dbgHeader.addEventListener('click', () => {
|
|
|
|
|
- const collapsed = dbgPanel.classList.toggle('collapsed');
|
|
|
|
|
- dbgPanel.classList.toggle('expanded', !collapsed);
|
|
|
|
|
- dbgToggle.textContent = collapsed ? '+' : '–';
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- // Initialize CDN cells from the global status that the loader scripts set
|
|
|
|
|
- try {
|
|
|
|
|
- const s = (window.__cdnStatus) || {};
|
|
|
|
|
- dbgSet(dbgCdn1, s.camera_utils || (s.camera_utils === 'error' ? 'FAIL' : '…'),
|
|
|
|
|
- s.camera_utils === 'ok' ? 'ok' : s.camera_utils === 'error' ? 'err' : 'run');
|
|
|
|
|
- dbgSet(dbgCdn2, s.drawing_utils || (s.drawing_utils === 'error' ? 'FAIL' : '…'),
|
|
|
|
|
- s.drawing_utils === 'ok' ? 'ok' : s.drawing_utils === 'error' ? 'err' : 'run');
|
|
|
|
|
- dbgSet(dbgCdn3, s.face_mesh || (s.face_mesh === 'error' ? 'FAIL' : '…'),
|
|
|
|
|
- s.face_mesh === 'ok' ? 'ok' : s.face_mesh === 'error' ? 'err' : 'run');
|
|
|
|
|
- } catch (e) { /* no-op */ }
|
|
|
|
|
- // Live-update on CDN load / failure events
|
|
|
|
|
- window.addEventListener('cdnOk', (e) => {
|
|
|
|
|
- const n = e && e.detail && e.detail.name;
|
|
|
|
|
- if (n === 'camera_utils') dbgSet(dbgCdn1, 'OK', 'ok');
|
|
|
|
|
- if (n === 'drawing_utils') dbgSet(dbgCdn2, 'OK', 'ok');
|
|
|
|
|
- if (n === 'face_mesh') dbgSet(dbgCdn3, 'OK', 'ok');
|
|
|
|
|
- });
|
|
|
|
|
- window.addEventListener('cdnError', (e) => {
|
|
|
|
|
- const n = e && e.detail && e.detail.name;
|
|
|
|
|
- if (n === 'camera_utils') { dbgSet(dbgCdn1, 'FAIL', 'err'); dbgSetError('CDN camera_utils failed to load'); }
|
|
|
|
|
- if (n === 'drawing_utils') { dbgSet(dbgCdn2, 'FAIL', 'err'); dbgSetError('CDN drawing_utils failed to load'); }
|
|
|
|
|
- if (n === 'face_mesh') { dbgSet(dbgCdn3, 'FAIL', 'err'); dbgSetError('CDN face_mesh failed to load'); }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- function setLoaderText(main, sub) {
|
|
|
|
|
- if (loaderText) loaderText.textContent = main;
|
|
|
|
|
- if (loaderSub) loaderSub.textContent = sub;
|
|
|
|
|
- console.log('[loader]', main, '—', sub);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Utilities
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function clamp(v, lo, hi) { return Math.min(hi, Math.max(lo, v)); }
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // State
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- let W = 0, H = 0;
|
|
|
|
|
- let smoothedX = 0.5, smoothedY = 0.5; // Kalman-smoothed gaze (0..1)
|
|
|
|
|
- // Kalman state replaces emaX/emaY
|
|
|
|
|
- let rawX = 0.5, rawY = 0.5; // raw gaze (0..1) from regression
|
|
|
|
|
- let rawXValid = false; // whether raw gaze has a fresh, valid measurement
|
|
|
|
|
- let trail = []; // array of {x, y}
|
|
|
|
|
- let faceMesh = null;
|
|
|
|
|
- let camera = null;
|
|
|
|
|
- let lastResults = null;
|
|
|
|
|
- let lastTime = performance.now();
|
|
|
|
|
- let fpsSmooth = 0;
|
|
|
|
|
- let irisVisible = { l: false, r: false };
|
|
|
|
|
- let trackingLocked = false;
|
|
|
|
|
- let lastFrameMs = 0; // last face-mesh processing time in ms
|
|
|
|
|
- let lastMeshSendAt = 0;
|
|
|
|
|
- let frameCount = 0; // for frame skipping (process every 2nd frame)
|
|
|
|
|
- let lastDebugLogMs = 0; // throttles the per-second onResults debug print
|
|
|
|
|
-
|
|
|
|
|
- // Calibration state
|
|
|
|
|
- let calibrationData = null; // { coefX:[a0..a5], coefY:[b0..b5], timestamp }
|
|
|
|
|
- let calActive = false; // true while a calibration session is in progress
|
|
|
|
|
- let calPaused = false; // true while in the calibration overlay (raw gaze is ignored)
|
|
|
|
|
- let calCurrentIdx = 0; // index into CAL_ORDER
|
|
|
|
|
- let calSamples = []; // collected {ratioX, ratioY} for the active point
|
|
|
|
|
- let calAllPoints = []; // accumulated {screenX, screenY, samples:[…]} for all 13 points
|
|
|
|
|
- let calCollectTimer = null; // setInterval id for sample collection
|
|
|
|
|
- let pointSampleCount = 0; // per-point sample counter (discard first 3)
|
|
|
|
|
-
|
|
|
|
|
- // Kalman Filter for 2D gaze tracking with velocity prediction
|
|
|
|
|
- // State: [x, y, vx, vy], Measurement: [x, y]
|
|
|
|
|
- var kalX = [0.5, 0.5, 0, 0]; // [x, y, vx, vy] in normalized screen coords
|
|
|
|
|
- var kalP = [1, 0, 0, 0,
|
|
|
|
|
- 0, 1, 0, 0,
|
|
|
|
|
- 0, 0, 1, 0,
|
|
|
|
|
- 0, 0, 0, 1]; // 4x4 covariance matrix (flat: P[i*4+j])
|
|
|
|
|
- // Q bumped up (0.001→0.01 for position, 0.01→0.05 for velocity) so the
|
|
|
|
|
- // Kalman filter trusts new measurements more and responds more quickly to
|
|
|
|
|
- // genuine eye motion. With the old Q the filter over-trusted its own
|
|
|
|
|
- // motion model and the orb lagged noticeably behind saccades.
|
|
|
|
|
- var kalQ = [0.01, 0, 0, 0,
|
|
|
|
|
- 0, 0.01, 0, 0,
|
|
|
|
|
- 0, 0, 0.05, 0,
|
|
|
|
|
- 0, 0, 0, 0.05]; // process noise (symmetric diagonal)
|
|
|
|
|
- var kalR = [0.04, 0, // measurement noise (flat 2x2: [R00,R01,R10,R11])
|
|
|
|
|
- 0, 0.04];
|
|
|
|
|
- // NOTE: measurement matrix H = [[1,0,0,0],[0,1,0,0]] is hardcoded in kalUpdate
|
|
|
|
|
- // (it never changes), so no separate kalH array is needed.
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Calibration: 13-point collection + 6-dim polynomial regression (OLS)
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- //
|
|
|
|
|
- // We model screen position as a quadratic polynomial of iris ratios:
|
|
|
|
|
- // screenX = a0 + a1*rX + a2*rY + a3*rX² + a4*rY² + a5*rX*rY
|
|
|
|
|
- // screenY = b0 + b1*rX + b2*rY + b3*rX² + b4*rY² + b5*rX*rY
|
|
|
|
|
- //
|
|
|
|
|
- // We fit screenX and screenY independently using ordinary least
|
|
|
|
|
- // squares on the N=13 calibration points (≈ 13 × 27 = 351 effective
|
|
|
|
|
- // samples after discarding the first 3 per point). We average each
|
|
|
|
|
- // point's samples first to make one (ratioX, ratioY) per grid
|
|
|
|
|
- // location, then solve the 6×6 normal equation directly (Cramer's
|
|
|
|
|
- // rule — we only have 6 unknowns).
|
|
|
|
|
- // ============================================================
|
|
|
|
|
-
|
|
|
|
|
- // Average a point's raw samples down to a single (ratioX, ratioY).
|
|
|
|
|
- function averageSamples(samples) {
|
|
|
|
|
- if (!samples.length) return { ratioX: 0.5, ratioY: 0.5 };
|
|
|
|
|
- let sx = 0, sy = 0;
|
|
|
|
|
- for (let i = 0; i < samples.length; i++) {
|
|
|
|
|
- sx += samples[i].ratioX;
|
|
|
|
|
- sy += samples[i].ratioY;
|
|
|
|
|
- }
|
|
|
|
|
- return { ratioX: sx / samples.length, ratioY: sy / samples.length };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Solve a 3×3 linear system A x = b via Cramer's rule.
|
|
|
|
|
- // Returns [x0, x1, x2] or null if A is singular.
|
|
|
|
|
- function solve3x3(A, b) {
|
|
|
|
|
- const det = (
|
|
|
|
|
- A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) -
|
|
|
|
|
- A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) +
|
|
|
|
|
- A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0])
|
|
|
|
|
- );
|
|
|
|
|
- if (Math.abs(det) < 1e-9) return null;
|
|
|
|
|
-
|
|
|
|
|
- const det0 = (
|
|
|
|
|
- b[0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) -
|
|
|
|
|
- A[0][1] * (b[1] * A[2][2] - A[1][2] * b[2]) +
|
|
|
|
|
- A[0][2] * (b[1] * A[2][1] - A[1][1] * b[2])
|
|
|
|
|
- );
|
|
|
|
|
- const det1 = (
|
|
|
|
|
- A[0][0] * (b[1] * A[2][2] - A[1][2] * b[2]) -
|
|
|
|
|
- b[0] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) +
|
|
|
|
|
- A[0][2] * (A[1][0] * b[2] - b[1] * A[2][0])
|
|
|
|
|
- );
|
|
|
|
|
- const det2 = (
|
|
|
|
|
- A[0][0] * (A[1][1] * b[2] - b[1] * A[2][1]) -
|
|
|
|
|
- A[0][1] * (A[1][0] * b[2] - b[1] * A[2][0]) +
|
|
|
|
|
- b[0] * (A[1][0] * A[2][1] - A[1][1] * A[2][0])
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- return [det0 / det, det1 / det, det2 / det];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 6x6 Cramer's rule: solve Ax=b where A is 6x6, b is 6x1
|
|
|
|
|
- // A is passed as array of 6 rows, each row is [a0..a5]
|
|
|
|
|
- function solve6x6(A, b) {
|
|
|
|
|
- function det6(m) {
|
|
|
|
|
- // Compute 6x6 determinant via cofactor expansion along first row
|
|
|
|
|
- let d = 0;
|
|
|
|
|
- for (let j = 0; j < 6; j++) {
|
|
|
|
|
- const minor = [];
|
|
|
|
|
- for (let i = 1; i < 6; i++) {
|
|
|
|
|
- const row = [];
|
|
|
|
|
- for (let k = 0; k < 6; k++) {
|
|
|
|
|
- if (k !== j) row.push(m[i][k]);
|
|
|
|
|
- }
|
|
|
|
|
- minor.push(row);
|
|
|
|
|
- }
|
|
|
|
|
- const sign = (j % 2 === 0) ? 1 : -1;
|
|
|
|
|
- d += sign * m[0][j] * det5(minor);
|
|
|
|
|
- }
|
|
|
|
|
- return d;
|
|
|
|
|
- }
|
|
|
|
|
- function det5(m) {
|
|
|
|
|
- let d = 0;
|
|
|
|
|
- for (let j = 0; j < 5; j++) {
|
|
|
|
|
- const minor = [];
|
|
|
|
|
- for (let i = 1; i < 5; i++) {
|
|
|
|
|
- const row = [];
|
|
|
|
|
- for (let k = 0; k < 5; k++) {
|
|
|
|
|
- if (k !== j) row.push(m[i][k]);
|
|
|
|
|
- }
|
|
|
|
|
- minor.push(row);
|
|
|
|
|
- }
|
|
|
|
|
- const sign = (j % 2 === 0) ? 1 : -1;
|
|
|
|
|
- d += sign * m[0][j] * det4(minor);
|
|
|
|
|
- }
|
|
|
|
|
- return d;
|
|
|
|
|
- }
|
|
|
|
|
- function det4(m) {
|
|
|
|
|
- // 4x4 determinant via cofactor expansion
|
|
|
|
|
- let d = 0;
|
|
|
|
|
- for (let j = 0; j < 4; j++) {
|
|
|
|
|
- const minor = [];
|
|
|
|
|
- for (let i = 1; i < 4; i++) {
|
|
|
|
|
- const row = [];
|
|
|
|
|
- for (let k = 0; k < 4; k++) {
|
|
|
|
|
- if (k !== j) row.push(m[i][k]);
|
|
|
|
|
- }
|
|
|
|
|
- minor.push(row);
|
|
|
|
|
- }
|
|
|
|
|
- const sign = (j % 2 === 0) ? 1 : -1;
|
|
|
|
|
- d += sign * m[0][j] * det3(minor);
|
|
|
|
|
- }
|
|
|
|
|
- return d;
|
|
|
|
|
- }
|
|
|
|
|
- function det3(m) {
|
|
|
|
|
- return m[0][0]*(m[1][1]*m[2][2]-m[1][2]*m[2][1])
|
|
|
|
|
- - m[0][1]*(m[1][0]*m[2][2]-m[1][2]*m[2][0])
|
|
|
|
|
- + m[0][2]*(m[1][0]*m[2][1]-m[1][1]*m[2][0]);
|
|
|
|
|
- }
|
|
|
|
|
- const D = det6(A);
|
|
|
|
|
- if (Math.abs(D) < 1e-12) return null; // singular
|
|
|
|
|
- const x = [];
|
|
|
|
|
- for (let j = 0; j < 6; j++) {
|
|
|
|
|
- const Aj = A.map((row, i) => {
|
|
|
|
|
- const r = row.slice();
|
|
|
|
|
- r[j] = b[i];
|
|
|
|
|
- return r;
|
|
|
|
|
- });
|
|
|
|
|
- x.push(det6(Aj) / D);
|
|
|
|
|
- }
|
|
|
|
|
- return x;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Build 6-dim polynomial design row from iris ratios
|
|
|
|
|
- function buildPolyFeatures(rX, rY) {
|
|
|
|
|
- return [1, rX, rY, rX*rX, rY*rY, rX*rY];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Build the design matrix for [a0, a1, …, a5] with rows [1, rX, rY, rX², rY², rX*rY].
|
|
|
|
|
- function buildDesignMatrix(points) {
|
|
|
|
|
- const M = [];
|
|
|
|
|
- for (let i = 0; i < points.length; i++) {
|
|
|
|
|
- M.push(buildPolyFeatures(points[i].ratioX, points[i].ratioY));
|
|
|
|
|
- }
|
|
|
|
|
- return M;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Ordinary least squares for y = X * beta. Returns beta = [b0, …, b5]
|
|
|
|
|
- // (we use Cramer's rule on X^T X and X^T y — small, dense, exact).
|
|
|
|
|
- function olsFit(X, y) {
|
|
|
|
|
- const n = X.length;
|
|
|
|
|
- // X^T X (6x6)
|
|
|
|
|
- const A = [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];
|
|
|
|
|
- // X^T y (6x1)
|
|
|
|
|
- const b = [0, 0, 0, 0, 0, 0];
|
|
|
|
|
- for (let i = 0; i < n; i++) {
|
|
|
|
|
- const xi = X[i];
|
|
|
|
|
- for (let r = 0; r < 6; r++) {
|
|
|
|
|
- for (let c = 0; c < 6; c++) A[r][c] += xi[r] * xi[c];
|
|
|
|
|
- b[r] += xi[r] * y[i];
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return solve6x6(A, b);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // RANSAC-style outlier rejection on ratio features per calibration point.
|
|
|
|
|
- // For each point's samples, drop those whose (ratioX, ratioY) lies > 2.5 std
|
|
|
|
|
- // from the per-point mean. This catches blinks, mid-saccade samples, and
|
|
|
|
|
- // points where the user briefly looked elsewhere while the dot was moving.
|
|
|
|
|
- function ransacCleanPerPoint(point) {
|
|
|
|
|
- if (!point.samples || point.samples.length <= 5) return point.samples || [];
|
|
|
|
|
- const sams = point.samples;
|
|
|
|
|
- const meanX = sams.reduce((a, s) => a + s.ratioX, 0) / sams.length;
|
|
|
|
|
- const meanY = sams.reduce((a, s) => a + s.ratioY, 0) / sams.length;
|
|
|
|
|
- const stdX = Math.sqrt(sams.reduce((a, s) => a + (s.ratioX - meanX) * (s.ratioX - meanX), 0) / sams.length);
|
|
|
|
|
- const stdY = Math.sqrt(sams.reduce((a, s) => a + (s.ratioY - meanY) * (s.ratioY - meanY), 0) / sams.length);
|
|
|
|
|
- // If std is essentially 0 (perfectly steady) just return everything.
|
|
|
|
|
- if (stdX < 1e-6 && stdY < 1e-6) return sams.slice();
|
|
|
|
|
- const kept = [];
|
|
|
|
|
- for (let i = 0; i < sams.length; i++) {
|
|
|
|
|
- const sx = (Math.abs(sams[i].ratioX - meanX) < 2.5 * Math.max(stdX, 1e-6));
|
|
|
|
|
- const sy = (Math.abs(sams[i].ratioY - meanY) < 2.5 * Math.max(stdY, 1e-6));
|
|
|
|
|
- if (sx && sy) kept.push(sams[i]);
|
|
|
|
|
- }
|
|
|
|
|
- // Don't drop more than 40% of samples — std may be underestimated.
|
|
|
|
|
- if (kept.length < sams.length * 0.6) return sams.slice();
|
|
|
|
|
- return kept;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function fitCalibration(allPoints) {
|
|
|
|
|
- // allPoints: [{ screenX, screenY, samples:[…] }, …]
|
|
|
|
|
- // 1) Per-point outlier rejection (RANSAC).
|
|
|
|
|
- // 2) Average remaining samples per point.
|
|
|
|
|
- let totalSamples = 0, totalKept = 0;
|
|
|
|
|
- const pts = allPoints.map(p => {
|
|
|
|
|
- const cleaned = ransacCleanPerPoint(p);
|
|
|
|
|
- totalSamples += (p.samples || []).length;
|
|
|
|
|
- totalKept += cleaned.length;
|
|
|
|
|
- const avg = averageSamples(cleaned);
|
|
|
|
|
- return {
|
|
|
|
|
- screenX: p.screenX,
|
|
|
|
|
- screenY: p.screenY,
|
|
|
|
|
- ratioX: avg.ratioX,
|
|
|
|
|
- ratioY: avg.ratioY
|
|
|
|
|
- };
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const X = buildDesignMatrix(pts);
|
|
|
|
|
- const ysx = pts.map(p => p.screenX);
|
|
|
|
|
- const ysy = pts.map(p => p.screenY);
|
|
|
|
|
-
|
|
|
|
|
- const coefX = olsFit(X, ysx);
|
|
|
|
|
- const coefY = olsFit(X, ysy);
|
|
|
|
|
- if (!coefX || !coefY) return null;
|
|
|
|
|
-
|
|
|
|
|
- // Optional debug log of fit quality.
|
|
|
|
|
- let sseX = 0, sseY = 0;
|
|
|
|
|
- for (let i = 0; i < pts.length; i++) {
|
|
|
|
|
- const px = coefX[0] + coefX[1]*pts[i].ratioX + coefX[2]*pts[i].ratioY + coefX[3]*pts[i].ratioX*pts[i].ratioX + coefX[4]*pts[i].ratioY*pts[i].ratioY + coefX[5]*pts[i].ratioX*pts[i].ratioY;
|
|
|
|
|
- const py = coefY[0] + coefY[1]*pts[i].ratioX + coefY[2]*pts[i].ratioY + coefY[3]*pts[i].ratioX*pts[i].ratioX + coefY[4]*pts[i].ratioY*pts[i].ratioY + coefY[5]*pts[i].ratioX*pts[i].ratioY;
|
|
|
|
|
- sseX += (pts[i].screenX - px) * (pts[i].screenX - px);
|
|
|
|
|
- sseY += (pts[i].screenY - py) * (pts[i].screenY - py);
|
|
|
|
|
- }
|
|
|
|
|
- console.log('[cal] fit quality kept=' + totalKept + '/' + totalSamples +
|
|
|
|
|
- ' RMSE X=' + Math.sqrt(sseX / pts.length).toFixed(4) +
|
|
|
|
|
- ' RMSE Y=' + Math.sqrt(sseY / pts.length).toFixed(4));
|
|
|
|
|
-
|
|
|
|
|
- return {
|
|
|
|
|
- coefX: [coefX[0], coefX[1], coefX[2], coefX[3], coefX[4], coefX[5]],
|
|
|
|
|
- coefY: [coefY[0], coefY[1], coefY[2], coefY[3], coefY[4], coefY[5]]
|
|
|
|
|
- };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function applyRegression(ratioX, ratioY) {
|
|
|
|
|
- if (!calibrationData) return { x: 0.5, y: 0.5 };
|
|
|
|
|
- const cx = calibrationData.coefX;
|
|
|
|
|
- const cy = calibrationData.coefY;
|
|
|
|
|
- const f = buildPolyFeatures(ratioX, ratioY);
|
|
|
|
|
- const x = cx[0] + cx[1]*f[1] + cx[2]*f[2] + cx[3]*f[3] + cx[4]*f[4] + cx[5]*f[5];
|
|
|
|
|
- const y = cy[0] + cy[1]*f[1] + cy[2]*f[2] + cy[3]*f[3] + cy[4]*f[4] + cy[5]*f[5];
|
|
|
|
|
- return { x: clamp(x, -0.05, 1.05), y: clamp(y, -0.05, 1.05) };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function saveCalibration(cal) {
|
|
|
|
|
- try {
|
|
|
|
|
- const payload = {
|
|
|
|
|
- version: CAL_VERSION,
|
|
|
|
|
- coefX: cal.coefX,
|
|
|
|
|
- coefY: cal.coefY,
|
|
|
|
|
- timestamp: Date.now()
|
|
|
|
|
- };
|
|
|
|
|
- localStorage.setItem(CAL_STORAGE_KEY, JSON.stringify(payload));
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.warn('[cal] localStorage save failed', e && e.message);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function loadCalibration() {
|
|
|
|
|
- try {
|
|
|
|
|
- const raw = localStorage.getItem(CAL_STORAGE_KEY);
|
|
|
|
|
- if (!raw) return null;
|
|
|
|
|
- const obj = JSON.parse(raw);
|
|
|
|
|
- if (!obj || !Array.isArray(obj.coefX) || !Array.isArray(obj.coefY)) return null;
|
|
|
|
|
- if (obj.coefX.length !== 6 || obj.coefY.length !== 6) return null;
|
|
|
|
|
- if (obj.version !== CAL_VERSION) {
|
|
|
|
|
- console.warn('[cal] stored calibration version mismatch (got', obj.version, 'expected', CAL_VERSION + '); discarding');
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- return {
|
|
|
|
|
- coefX: obj.coefX.slice(0, 6),
|
|
|
|
|
- coefY: obj.coefY.slice(0, 6),
|
|
|
|
|
- timestamp: obj.timestamp || 0
|
|
|
|
|
- };
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.warn('[cal] localStorage load failed', e && e.message);
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function clearCalibration() {
|
|
|
|
|
- try { localStorage.removeItem(CAL_STORAGE_KEY); } catch (e) { /* no-op */ }
|
|
|
|
|
- calibrationData = null;
|
|
|
|
|
- // Reset Kalman so the orb returns to the center after a recalibration.
|
|
|
|
|
- kalInit();
|
|
|
|
|
- smoothedX = 0.5; smoothedY = 0.5;
|
|
|
|
|
- rawXValid = false;
|
|
|
|
|
- updateCalStatus('uncalibrated');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function updateCalStatus(state) {
|
|
|
|
|
- // state: 'uncalibrated' | 'calibrating' | 'calibrated'
|
|
|
|
|
- if (!calDotStatus || !calStatusText) return;
|
|
|
|
|
- if (state === 'uncalibrated') {
|
|
|
|
|
- calDotStatus.className = 'dot';
|
|
|
|
|
- calStatusText.className = 'value warn';
|
|
|
|
|
- calStatusText.textContent = 'UNCAL';
|
|
|
|
|
- if (recalibrateBtn) recalibrateBtn.disabled = false;
|
|
|
|
|
- dbgSet(dbgCal, 'uncalibrated', 'warn');
|
|
|
|
|
- } else if (state === 'calibrating') {
|
|
|
|
|
- calDotStatus.className = 'dot warn';
|
|
|
|
|
- calStatusText.className = 'value';
|
|
|
|
|
- calStatusText.textContent = 'CAL…';
|
|
|
|
|
- if (recalibrateBtn) recalibrateBtn.disabled = true;
|
|
|
|
|
- dbgSet(dbgCal, 'running', 'run');
|
|
|
|
|
- } else if (state === 'calibrated') {
|
|
|
|
|
- calDotStatus.className = 'dot ok';
|
|
|
|
|
- calStatusText.className = 'value ok';
|
|
|
|
|
- calStatusText.textContent = 'OK';
|
|
|
|
|
- if (recalibrateBtn) recalibrateBtn.disabled = false;
|
|
|
|
|
- dbgSet(dbgCal, 'ready', 'ok');
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Calibration UI flow
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function positionCalDot(idx) {
|
|
|
|
|
- if (!calDot) return;
|
|
|
|
|
- const p = CAL_GRID[CAL_ORDER[idx]];
|
|
|
|
|
- // The body / stage is mirrored via CSS scaleX(-1) (webcam preview only);
|
|
|
|
|
- // the canvas (#stage) is NOT mirrored, so we use window coordinates directly.
|
|
|
|
|
- const cx = p.x * window.innerWidth;
|
|
|
|
|
- const cy = p.y * window.innerHeight;
|
|
|
|
|
- calDot.style.left = cx + 'px';
|
|
|
|
|
- calDot.style.top = cy + 'px';
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function showCalibrationUI(show) {
|
|
|
|
|
- if (!calOverlay) return;
|
|
|
|
|
- if (show) {
|
|
|
|
|
- calOverlay.classList.add('show');
|
|
|
|
|
- calOverlay.setAttribute('aria-hidden', 'false');
|
|
|
|
|
- } else {
|
|
|
|
|
- calOverlay.classList.remove('show');
|
|
|
|
|
- calOverlay.setAttribute('aria-hidden', 'true');
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function startCalibration() {
|
|
|
|
|
- if (calActive) return;
|
|
|
|
|
- calActive = true;
|
|
|
|
|
- calPaused = true; // don't let the orb move while we collect
|
|
|
|
|
- calAllPoints = [];
|
|
|
|
|
- calCurrentIdx = 0;
|
|
|
|
|
- calSamples = [];
|
|
|
|
|
- pointSampleCount = 0;
|
|
|
|
|
- updateCalStatus('calibrating');
|
|
|
|
|
- showCalibrationUI(true);
|
|
|
|
|
- gotoNextCalPoint();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function gotoNextCalPoint() {
|
|
|
|
|
- if (!calActive) return;
|
|
|
|
|
- if (calCurrentIdx >= CAL_ORDER.length) {
|
|
|
|
|
- finishCalibration();
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- positionCalDot(calCurrentIdx);
|
|
|
|
|
- if (calText) {
|
|
|
|
|
- calText.textContent = 'Calibrating (' + (calCurrentIdx + 1) + '/' + CAL_ORDER.length + ')';
|
|
|
|
|
- }
|
|
|
|
|
- if (calSub) {
|
|
|
|
|
- calSub.textContent = 'Follow the dot with your eyes';
|
|
|
|
|
- }
|
|
|
|
|
- calSamples = [];
|
|
|
|
|
- pointSampleCount = 0; // reset per-point counter (first 3 are discarded)
|
|
|
|
|
- startSampleCollection();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function startSampleCollection() {
|
|
|
|
|
- if (calCollectTimer) clearInterval(calCollectTimer);
|
|
|
|
|
- calCollectTimer = setInterval(() => {
|
|
|
|
|
- if (!calActive) return;
|
|
|
|
|
- // Discard the first 3 samples so the user has time to fixate on the dot.
|
|
|
|
|
- if (pointSampleCount < 3) { pointSampleCount++; return; }
|
|
|
|
|
- const gaze = lastGaze; // most recent raw iris ratio (see onResults)
|
|
|
|
|
- if (gaze && gaze.visible) {
|
|
|
|
|
- calSamples.push({ ratioX: gaze.ratioX, ratioY: gaze.ratioY });
|
|
|
|
|
- }
|
|
|
|
|
- if (calSamples.length >= CAL_SAMPLES_PER_POINT) {
|
|
|
|
|
- clearInterval(calCollectTimer);
|
|
|
|
|
- calCollectTimer = null;
|
|
|
|
|
- commitCurrentPoint();
|
|
|
|
|
- }
|
|
|
|
|
- }, CAL_SAMPLE_INTERVAL_MS);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function commitCurrentPoint() {
|
|
|
|
|
- const p = CAL_GRID[CAL_ORDER[calCurrentIdx]];
|
|
|
|
|
- calAllPoints.push({
|
|
|
|
|
- screenX: p.x,
|
|
|
|
|
- screenY: p.y,
|
|
|
|
|
- samples: calSamples.slice()
|
|
|
|
|
- });
|
|
|
|
|
- calCurrentIdx++;
|
|
|
|
|
- if (calCurrentIdx >= CAL_ORDER.length) {
|
|
|
|
|
- finishCalibration();
|
|
|
|
|
- } else {
|
|
|
|
|
- // Brief pause between points so the user can re-target.
|
|
|
|
|
- setTimeout(gotoNextCalPoint, 150);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function finishCalibration() {
|
|
|
|
|
- if (calCollectTimer) { clearInterval(calCollectTimer); calCollectTimer = null; }
|
|
|
|
|
- const fit = fitCalibration(calAllPoints);
|
|
|
|
|
- if (!fit) {
|
|
|
|
|
- console.warn('[cal] OLS fit failed');
|
|
|
|
|
- calActive = false;
|
|
|
|
|
- calPaused = false;
|
|
|
|
|
- showCalibrationUI(false);
|
|
|
|
|
- updateCalStatus(calibrationData ? 'calibrated' : 'uncalibrated');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- calibrationData = { coefX: fit.coefX, coefY: fit.coefY, timestamp: Date.now() };
|
|
|
|
|
- saveCalibration(calibrationData);
|
|
|
|
|
- console.log('[cal] saved', calibrationData);
|
|
|
|
|
- // Reset Kalman so stale state doesn't bleed into the new calibration.
|
|
|
|
|
- kalInit();
|
|
|
|
|
- calActive = false;
|
|
|
|
|
- calPaused = false;
|
|
|
|
|
- showCalibrationUI(false);
|
|
|
|
|
- updateCalStatus('calibrated');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function cancelCalibration() {
|
|
|
|
|
- if (calCollectTimer) { clearInterval(calCollectTimer); calCollectTimer = null; }
|
|
|
|
|
- calActive = false;
|
|
|
|
|
- calPaused = false;
|
|
|
|
|
- showCalibrationUI(false);
|
|
|
|
|
- updateCalStatus(calibrationData ? 'calibrated' : 'uncalibrated');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (calCancel) {
|
|
|
|
|
- calCancel.addEventListener('click', cancelCalibration);
|
|
|
|
|
- }
|
|
|
|
|
- if (recalibrateBtn) {
|
|
|
|
|
- recalibrateBtn.addEventListener('click', () => {
|
|
|
|
|
- if (!faceMesh) return; // need the model to be running
|
|
|
|
|
- clearCalibration();
|
|
|
|
|
- startCalibration();
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Try to restore a previously-saved calibration as soon as the script runs.
|
|
|
|
|
- (function tryRestoreCalibration() {
|
|
|
|
|
- const saved = loadCalibration();
|
|
|
|
|
- if (saved) {
|
|
|
|
|
- calibrationData = saved;
|
|
|
|
|
- updateCalStatus('calibrated');
|
|
|
|
|
- console.log('[cal] restored from localStorage');
|
|
|
|
|
- } else {
|
|
|
|
|
- updateCalStatus('uncalibrated');
|
|
|
|
|
- }
|
|
|
|
|
- })();
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Kalman filter (replaces EMA + outlier threshold)
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // The state is [x, y, vx, vy] in normalized screen coords; the
|
|
|
|
|
- // measurement is [x, y] (from applyRegression). The process model
|
|
|
|
|
- // assumes constant velocity with small random accelerations (Q),
|
|
|
|
|
- // and the measurement model assumes the regression output is
|
|
|
|
|
- // noisy but unbiased (R). This naturally handles gaze noise,
|
|
|
|
|
- // outliers (large innovations get down-weighted), and saccades
|
|
|
|
|
- // (velocity prediction reduces lag).
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function kalUpdate(zx, zy) {
|
|
|
|
|
- // Prediction: x = F*x, P = F*P*F' + Q
|
|
|
|
|
- // F = [[1,0,dt,0],[0,1,0,dt],[0,0,1,0],[0,0,0,1]], dt=1
|
|
|
|
|
- var dt = 1;
|
|
|
|
|
- // Predict state
|
|
|
|
|
- var px = kalX[0] + kalX[2] * dt;
|
|
|
|
|
- var py = kalX[1] + kalX[3] * dt;
|
|
|
|
|
- // Predict P: F*P
|
|
|
|
|
- // F*P where F adds velocity*dt to position rows
|
|
|
|
|
- var pP = new Float64Array(16);
|
|
|
|
|
- for (var i = 0; i < 4; i++) {
|
|
|
|
|
- for (var j = 0; j < 4; j++) {
|
|
|
|
|
- var sum = 0;
|
|
|
|
|
- for (var k = 0; k < 4; k++) {
|
|
|
|
|
- var fik = (i < 2 && k === i + 2) ? dt : (i === k ? 1 : 0); // F[i][k]
|
|
|
|
|
- sum += fik * kalP[k * 4 + j];
|
|
|
|
|
- }
|
|
|
|
|
- pP[i * 4 + j] = sum;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // pP * F' + Q
|
|
|
|
|
- for (var i = 0; i < 4; i++) {
|
|
|
|
|
- for (var j = 0; j < 4; j++) {
|
|
|
|
|
- var sum = 0;
|
|
|
|
|
- for (var k = 0; k < 4; k++) {
|
|
|
|
|
- var fkj = (k < 2 && j === k + 2) ? dt : (k === j ? 1 : 0); // F'[k][j] = F[j][k]
|
|
|
|
|
- sum += pP[i * 4 + k] * fkj;
|
|
|
|
|
- }
|
|
|
|
|
- kalP[i * 4 + j] = sum + kalQ[i * 4 + j];
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // Innovation: y = z - H*x
|
|
|
|
|
- var yx = zx - px;
|
|
|
|
|
- var yy = zy - py;
|
|
|
|
|
- // S = H*P*H' + R (2x2)
|
|
|
|
|
- // H = [[1,0,0,0],[0,1,0,0]], so H*P*H' extracts the 2x2 top-left block of P:
|
|
|
|
|
- // [P00 P01] plus R (flat: [R00,R01,R10,R11])
|
|
|
|
|
- // [P10 P11]
|
|
|
|
|
- var s00 = kalP[0] + kalR[0];
|
|
|
|
|
- var s01 = kalP[1] + kalR[1];
|
|
|
|
|
- var s10 = kalP[4] + kalR[2];
|
|
|
|
|
- var s11 = kalP[5] + kalR[3];
|
|
|
|
|
- var sDet = s00 * s11 - s01 * s10;
|
|
|
|
|
- if (Math.abs(sDet) < 1e-12) { kalX = [zx, zy, 0, 0]; return {x: zx, y: zy}; }
|
|
|
|
|
- var sInv00 = s11 / sDet;
|
|
|
|
|
- var sInv01 = -s01 / sDet;
|
|
|
|
|
- var sInv10 = -s10 / sDet;
|
|
|
|
|
- var sInv11 = s00 / sDet;
|
|
|
|
|
- // K = P * H' * S^-1 (4x2 matrix, stored flat as K[i*2+row])
|
|
|
|
|
- // With H = [[1,0,0,0],[0,1,0,0]], H' has 1s at (0,0) and (1,1) only,
|
|
|
|
|
- // so P*H' picks the first two columns of P, then multiplied by S^-1 (2x2).
|
|
|
|
|
- var K = new Float64Array(8); // 4x2
|
|
|
|
|
- for (var i = 0; i < 4; i++) {
|
|
|
|
|
- K[i*2+0] = kalP[i*4+0] * sInv00 + kalP[i*4+1] * sInv10;
|
|
|
|
|
- K[i*2+1] = kalP[i*4+0] * sInv01 + kalP[i*4+1] * sInv11;
|
|
|
|
|
- }
|
|
|
|
|
- // x = px + K*y
|
|
|
|
|
- kalX[0] = px + K[0] * yx + K[1] * yy;
|
|
|
|
|
- kalX[1] = py + K[2] * yx + K[3] * yy;
|
|
|
|
|
- kalX[2] = kalX[2] + K[4] * yx + K[5] * yy;
|
|
|
|
|
- kalX[3] = kalX[3] + K[6] * yx + K[7] * yy;
|
|
|
|
|
- // P = (I - K*H) * P (Joseph form: numerical stability)
|
|
|
|
|
- // With H = [[1,0,0,0],[0,1,0,0]], only columns 0,1 of K matter:
|
|
|
|
|
- // (I-KH)[i][j] = δ(i,j) - K[i*2+0]*(j==0) - K[i*2+1]*(j==1)
|
|
|
|
|
- // So (I-KH)*P simplifies to:
|
|
|
|
|
- // newP[i][k] = P[i][k] - K[i*2+0]*P[0][k] - K[i*2+1]*P[1][k]
|
|
|
|
|
- var newP = new Float64Array(16);
|
|
|
|
|
- for (var i = 0; i < 4; i++) {
|
|
|
|
|
- for (var k = 0; k < 4; k++) {
|
|
|
|
|
- newP[i*4+k] = kalP[i*4+k] - K[i*2+0]*kalP[k] - K[i*2+1]*kalP[4+k];
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // Symmetrize P to prevent numerical drift
|
|
|
|
|
- for (var i = 0; i < 4; i++) {
|
|
|
|
|
- for (var j = i + 1; j < 4; j++) {
|
|
|
|
|
- var avg = (newP[i*4+j] + newP[j*4+i]) * 0.5;
|
|
|
|
|
- newP[i*4+j] = avg;
|
|
|
|
|
- newP[j*4+i] = avg;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- for (var i = 0; i < 16; i++) kalP[i] = newP[i];
|
|
|
|
|
-
|
|
|
|
|
- return {x: kalX[0], y: kalX[1]};
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function kalInit() {
|
|
|
|
|
- kalX = [0.5, 0.5, 0, 0];
|
|
|
|
|
- for (var i = 0; i < 16; i++) kalP[i] = (i % 5 === 0) ? 1 : 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Canvas sizing
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function resize() {
|
|
|
|
|
- const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
|
|
|
- W = window.innerWidth;
|
|
|
|
|
- H = window.innerHeight;
|
|
|
|
|
- stage.width = W * dpr;
|
|
|
|
|
- stage.height = H * dpr;
|
|
|
|
|
- stage.style.width = W + 'px';
|
|
|
|
|
- stage.style.height = H + 'px';
|
|
|
|
|
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
|
|
|
- }
|
|
|
|
|
- window.addEventListener('resize', () => {
|
|
|
|
|
- resize();
|
|
|
|
|
- // Re-position the calibration dot if one is currently active
|
|
|
|
|
- if (calActive && calCurrentIdx < CAL_ORDER.length) {
|
|
|
|
|
- positionCalDot(calCurrentIdx);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- resize();
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Gaze math: iris-ratio only
|
|
|
|
|
- // Pure eye-in-socket direction. The regression model (fit during
|
|
|
|
|
- // calibration) maps this raw ratio onto screen coordinates.
|
|
|
|
|
- //
|
|
|
|
|
- // Both eyes use the SAME effective sign convention for ratioX (both INCREASE
|
|
|
|
|
- // when the user looks to the right):
|
|
|
|
|
- // • Left eye: ratioX = (iris - inner) / (outer - inner) → increases naturally
|
|
|
|
|
- // • Right eye: raw ratio = (iris - outer) / (inner - outer) → DECREASES when looking right,
|
|
|
|
|
- // so we apply `1 - raw_ratio` to flip it back to the same sign.
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function computeIrisGaze(landmarks) {
|
|
|
|
|
- // Left eye: normal (outer=33, inner=133)
|
|
|
|
|
- const l_outer = landmarks[L_OUTER];
|
|
|
|
|
- const l_inner = landmarks[L_INNER];
|
|
|
|
|
- const l_top = landmarks[L_TOP];
|
|
|
|
|
- const l_bot = landmarks[L_BOT];
|
|
|
|
|
- const _l_c = landmarks[L_IRIS];
|
|
|
|
|
- // Average the 5 left iris contour points for a more stable reading
|
|
|
|
|
- const l_iris = {
|
|
|
|
|
- x: [468,469,470,471,472].reduce((s,i) => s + (landmarks[i]?.x || 0), 0) / 5,
|
|
|
|
|
- y: [468,469,470,471,472].reduce((s,i) => s + (landmarks[i]?.y || 0), 0) / 5
|
|
|
|
|
- };
|
|
|
|
|
- const left = (l_outer && l_inner && _l_c &&
|
|
|
|
|
- !(_l_c.x === 0 && _l_c.y === 0) &&
|
|
|
|
|
- !Number.isNaN(_l_c.x) && !Number.isNaN(_l_c.y) &&
|
|
|
|
|
- Math.abs(l_outer.x - l_inner.x) > 1e-6)
|
|
|
|
|
- ? (() => {
|
|
|
|
|
- const eyeW = l_outer.x - l_inner.x;
|
|
|
|
|
- const ratioX = (l_iris.x - l_inner.x) / eyeW;
|
|
|
|
|
- const midY = (l_top.y + l_bot.y) * 0.5;
|
|
|
|
|
- const ratioY = (l_iris.y - midY) / (Math.abs(eyeW) || 1e-6);
|
|
|
|
|
- return { ratioX, ratioY };
|
|
|
|
|
- })()
|
|
|
|
|
- : null;
|
|
|
|
|
-
|
|
|
|
|
- // Right eye: same formula structure as left eye (no flip needed because
|
|
|
|
|
- // MediaPipe landmarks in the raw frame already give same-sign ratios for
|
|
|
|
|
- // both eyes — both ratioX values DECREASE when the user looks RIGHT).
|
|
|
|
|
- // Using R_OUTER (which is to the RIGHT in the un-mirrored raw frame) as
|
|
|
|
|
- // the denominator reference yields a ratio in the same [0,1]-ish range
|
|
|
|
|
- // and, crucially, the same sign as the left eye so that the average of
|
|
|
|
|
- // both eyes does NOT collapse to ~0.5.
|
|
|
|
|
- const r_outer = landmarks[R_OUTER];
|
|
|
|
|
- const r_inner = landmarks[R_INNER];
|
|
|
|
|
- const r_top = landmarks[R_TOP];
|
|
|
|
|
- const r_bot = landmarks[R_BOT];
|
|
|
|
|
- const _r_c = landmarks[R_IRIS];
|
|
|
|
|
- // Average the 5 right iris contour points for a more stable reading
|
|
|
|
|
- const r_iris = {
|
|
|
|
|
- x: [473,474,475,476,477].reduce((s,i) => s + (landmarks[i]?.x || 0), 0) / 5,
|
|
|
|
|
- y: [473,474,475,476,477].reduce((s,i) => s + (landmarks[i]?.y || 0), 0) / 5
|
|
|
|
|
- };
|
|
|
|
|
- const right = (r_outer && r_inner && _r_c &&
|
|
|
|
|
- !(_r_c.x === 0 && _r_c.y === 0) &&
|
|
|
|
|
- !Number.isNaN(_r_c.x) && !Number.isNaN(_r_c.y) &&
|
|
|
|
|
- Math.abs(r_inner.x - r_outer.x) > 1e-6)
|
|
|
|
|
- ? (() => {
|
|
|
|
|
- // Same formula shape as the left eye, but using R_OUTER (right side
|
|
|
|
|
- // in raw frame) as the "outer" anchor. eyeW = inner - outer is
|
|
|
|
|
- // NEGATIVE here, and (iris - outer)/eyeW is therefore also
|
|
|
|
|
- // negative of the naive (iris - inner)/eyeW — but that's the
|
|
|
|
|
- // correct sign for the right eye, so both eyes agree when the
|
|
|
|
|
- // user looks right. No `1 -` flip is applied.
|
|
|
|
|
- const r_eyeW = r_inner.x - r_outer.x;
|
|
|
|
|
- const r_ratioX = 1 - (r_iris.x - r_outer.x) / r_eyeW;
|
|
|
|
|
- const r_midY = (r_top.y + r_bot.y) * 0.5;
|
|
|
|
|
- const r_ratioY = (r_iris.y - r_midY) / (Math.abs(r_eyeW) || 1e-6);
|
|
|
|
|
- return { ratioX: r_ratioX, ratioY: r_ratioY };
|
|
|
|
|
- })()
|
|
|
|
|
- : null;
|
|
|
|
|
-
|
|
|
|
|
- irisVisible.l = !!left;
|
|
|
|
|
- irisVisible.r = !!right;
|
|
|
|
|
-
|
|
|
|
|
- if (!left && !right) return { ratioX: 0.5, ratioY: 0.5, visible: false };
|
|
|
|
|
-
|
|
|
|
|
- let rx, ry;
|
|
|
|
|
- if (left && right) {
|
|
|
|
|
- rx = (left.ratioX + right.ratioX) * 0.5;
|
|
|
|
|
- ry = (left.ratioY + right.ratioY) * 0.5;
|
|
|
|
|
- } else {
|
|
|
|
|
- const s = left || right;
|
|
|
|
|
- rx = s.ratioX; ry = s.ratioY;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return { ratioX: rx, ratioY: ry, visible: true };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Render: full-screen orb + trail
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function drawOrb() {
|
|
|
|
|
- // Clear with very slight motion blur for the trail
|
|
|
|
|
- ctx.fillStyle = 'rgba(10, 10, 26, 0.35)';
|
|
|
|
|
- ctx.fillRect(0, 0, W, H);
|
|
|
|
|
-
|
|
|
|
|
- // Map smoothed gaze to screen coordinates (with margin so orb stays on screen)
|
|
|
|
|
- const margin = 60;
|
|
|
|
|
- const sx = margin + smoothedX * (W - margin * 2);
|
|
|
|
|
- const sy = margin + smoothedY * (H - margin * 2);
|
|
|
|
|
-
|
|
|
|
|
- // Push trail
|
|
|
|
|
- trail.push({ x: sx, y: sy });
|
|
|
|
|
- if (trail.length > TRAIL_LENGTH) trail.shift();
|
|
|
|
|
-
|
|
|
|
|
- // Draw trail (oldest -> newest, fading in opacity & shrinking radius)
|
|
|
|
|
- // Cheap: simple filled circles, no radialGradient per point.
|
|
|
|
|
- for (let i = 0; i < trail.length; i++) {
|
|
|
|
|
- const t = trail[i];
|
|
|
|
|
- const tNorm = (i + 1) / trail.length; // 0..1
|
|
|
|
|
- const r = ORB_RADIUS * 0.12 * tNorm;
|
|
|
|
|
- const a = 0.03 + 0.25 * tNorm;
|
|
|
|
|
- ctx.fillStyle = 'rgba(0, 229, 255, ' + a + ')';
|
|
|
|
|
- ctx.beginPath();
|
|
|
|
|
- ctx.arc(t.x, t.y, r, 0, Math.PI * 2);
|
|
|
|
|
- ctx.fill();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Main orb - layered radial gradients for glow
|
|
|
|
|
- const cx = sx, cy = sy;
|
|
|
|
|
-
|
|
|
|
|
- // Outer halo
|
|
|
|
|
- let g = ctx.createRadialGradient(cx, cy, 0, cx, cy, ORB_RADIUS * 4);
|
|
|
|
|
- g.addColorStop(0, 'rgba(0, 229, 255, 0.35)');
|
|
|
|
|
- g.addColorStop(0.25, 'rgba(0, 229, 255, 0.18)');
|
|
|
|
|
- g.addColorStop(0.6, 'rgba(0, 184, 212, 0.05)');
|
|
|
|
|
- g.addColorStop(1, 'rgba(0, 229, 255, 0)');
|
|
|
|
|
- ctx.fillStyle = g;
|
|
|
|
|
- ctx.beginPath();
|
|
|
|
|
- ctx.arc(cx, cy, ORB_RADIUS * 4, 0, Math.PI * 2);
|
|
|
|
|
- ctx.fill();
|
|
|
|
|
-
|
|
|
|
|
- // Mid glow
|
|
|
|
|
- g = ctx.createRadialGradient(cx, cy, 0, cx, cy, ORB_RADIUS * 1.8);
|
|
|
|
|
- g.addColorStop(0, 'rgba(180, 255, 255, 0.95)');
|
|
|
|
|
- g.addColorStop(0.25, 'rgba(0, 229, 255, 0.85)');
|
|
|
|
|
- g.addColorStop(0.65, 'rgba(0, 184, 212, 0.5)');
|
|
|
|
|
- g.addColorStop(1, 'rgba(0, 229, 255, 0)');
|
|
|
|
|
- ctx.fillStyle = g;
|
|
|
|
|
- ctx.beginPath();
|
|
|
|
|
- ctx.arc(cx, cy, ORB_RADIUS * 1.8, 0, Math.PI * 2);
|
|
|
|
|
- ctx.fill();
|
|
|
|
|
-
|
|
|
|
|
- // Core
|
|
|
|
|
- g = ctx.createRadialGradient(cx, cy, 0, cx, cy, ORB_RADIUS);
|
|
|
|
|
- g.addColorStop(0, 'rgba(255, 255, 255, 1)');
|
|
|
|
|
- g.addColorStop(0.3, 'rgba(180, 255, 255, 1)');
|
|
|
|
|
- g.addColorStop(0.7, 'rgba(0, 229, 255, 0.95)');
|
|
|
|
|
- g.addColorStop(1, 'rgba(0, 184, 212, 0.7)');
|
|
|
|
|
- ctx.fillStyle = g;
|
|
|
|
|
- ctx.beginPath();
|
|
|
|
|
- ctx.arc(cx, cy, ORB_RADIUS, 0, Math.PI * 2);
|
|
|
|
|
- ctx.fill();
|
|
|
|
|
-
|
|
|
|
|
- // Inner sparkle
|
|
|
|
|
- ctx.fillStyle = 'rgba(255, 255, 255, 0.85)';
|
|
|
|
|
- ctx.beginPath();
|
|
|
|
|
- ctx.arc(cx - ORB_RADIUS * 0.25, cy - ORB_RADIUS * 0.25, ORB_RADIUS * 0.18, 0, Math.PI * 2);
|
|
|
|
|
- ctx.fill();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Render: face mesh overlay on webcam preview
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function drawOverlay(results) {
|
|
|
|
|
- const w = overlay.width;
|
|
|
|
|
- const h = overlay.height;
|
|
|
|
|
- octx.clearRect(0, 0, w, h);
|
|
|
|
|
-
|
|
|
|
|
- if (!results || !results.multiFaceLandmarks || results.multiFaceLandmarks.length === 0) {
|
|
|
|
|
- // Subtle "no face" indicator
|
|
|
|
|
- octx.strokeStyle = 'rgba(0, 229, 255, 0.25)';
|
|
|
|
|
- octx.setLineDash([4, 4]);
|
|
|
|
|
- octx.lineWidth = 1;
|
|
|
|
|
- octx.strokeRect(8, 8, w - 16, h - 16);
|
|
|
|
|
- octx.setLineDash([]);
|
|
|
|
|
- octx.fillStyle = 'rgba(0, 229, 255, 0.7)';
|
|
|
|
|
- octx.font = '10px monospace';
|
|
|
|
|
- octx.fillText('NO FACE DETECTED', 12, h - 12);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const lm = results.multiFaceLandmarks[0];
|
|
|
|
|
- // Note: the video is mirrored visually via CSS, so we mirror X here
|
|
|
|
|
- // to keep the mesh aligned with what the user sees.
|
|
|
|
|
- const pt = (i) => ({ x: (1 - lm[i].x) * w, y: lm[i].y * h });
|
|
|
|
|
-
|
|
|
|
|
- // Draw mesh lines
|
|
|
|
|
- octx.lineWidth = 0.6;
|
|
|
|
|
- octx.strokeStyle = 'rgba(0, 229, 255, 0.45)';
|
|
|
|
|
- octx.beginPath();
|
|
|
|
|
- [FACE_OVAL, LIPS, L_EYE, R_EYE, L_BROW, R_BROW].forEach(connections => {
|
|
|
|
|
- for (let i = 0; i < connections.length; i++) {
|
|
|
|
|
- const a = pt(connections[i]);
|
|
|
|
|
- const b = pt(connections[(i + 1) % connections.length]);
|
|
|
|
|
- octx.moveTo(a.x, a.y);
|
|
|
|
|
- octx.lineTo(b.x, b.y);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- octx.stroke();
|
|
|
|
|
-
|
|
|
|
|
- // Subtle face dots (subsample every 6th point for performance)
|
|
|
|
|
- octx.fillStyle = 'rgba(0, 229, 255, 0.4)';
|
|
|
|
|
- for (let i = 0; i < lm.length; i++) {
|
|
|
|
|
- if (i % 6 !== 0) continue; // was % 3 — doubled the stride to halve the work
|
|
|
|
|
- const p = pt(i);
|
|
|
|
|
- octx.fillRect(p.x, p.y, 1, 1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Iris highlight (big + bright)
|
|
|
|
|
- octx.fillStyle = '#ffeb3b';
|
|
|
|
|
- IRIS_LANDMARKS.forEach(idx => {
|
|
|
|
|
- const p = pt(idx);
|
|
|
|
|
- octx.beginPath();
|
|
|
|
|
- octx.arc(p.x, p.y, 1.6, 0, Math.PI * 2);
|
|
|
|
|
- octx.fill();
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // Iris center (468 / 473) - extra large
|
|
|
|
|
- [L_IRIS, R_IRIS].forEach(idx => {
|
|
|
|
|
- const p = pt(idx);
|
|
|
|
|
- octx.fillStyle = '#ffffff';
|
|
|
|
|
- octx.beginPath();
|
|
|
|
|
- octx.arc(p.x, p.y, 2.6, 0, Math.PI * 2);
|
|
|
|
|
- octx.fill();
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // Animation loop
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function loop(now) {
|
|
|
|
|
- // FPS
|
|
|
|
|
- const dt = now - lastTime;
|
|
|
|
|
- lastTime = now;
|
|
|
|
|
- const instFps = 1000 / Math.max(dt, 1);
|
|
|
|
|
- fpsSmooth = fpsSmooth === 0 ? instFps : (fpsSmooth * 0.9 + instFps * 0.1);
|
|
|
|
|
-
|
|
|
|
|
- // Kalman filter: feed raw gaze through the Kalman filter.
|
|
|
|
|
- // rawX/rawY are already screen-coords (applyRegression when calibrated,
|
|
|
|
|
- // or raw iris ratio clamped to [0,1] before calibration is ready).
|
|
|
|
|
- // No update when no iris is visible, so the orb holds its position
|
|
|
|
|
- // instead of drifting back to center.
|
|
|
|
|
- if (rawXValid && !calPaused) {
|
|
|
|
|
- var kf = kalUpdate(rawX, rawY);
|
|
|
|
|
- smoothedX = kf.x;
|
|
|
|
|
- smoothedY = kf.y;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- drawOrb();
|
|
|
|
|
- if (lastResults) drawOverlay(lastResults);
|
|
|
|
|
-
|
|
|
|
|
- // Status text — overall pipeline state
|
|
|
|
|
- const visible = irisVisible.l || irisVisible.r;
|
|
|
|
|
- if (visible) {
|
|
|
|
|
- statusDot.className = 'dot ok';
|
|
|
|
|
- statusText.className = 'value ok';
|
|
|
|
|
- statusText.textContent = 'TRACKING';
|
|
|
|
|
- dbgSet(dbgStage, 'tracking', 'ok');
|
|
|
|
|
- } else if (faceMesh) {
|
|
|
|
|
- statusDot.className = 'dot warn';
|
|
|
|
|
- statusText.className = 'value warn';
|
|
|
|
|
- statusText.textContent = 'NO IRIS';
|
|
|
|
|
- dbgSet(dbgStage, 'no face', 'warn');
|
|
|
|
|
- } else {
|
|
|
|
|
- statusDot.className = 'dot';
|
|
|
|
|
- statusText.className = 'value';
|
|
|
|
|
- statusText.textContent = 'INIT';
|
|
|
|
|
- dbgSet(dbgStage, 'init', 'run');
|
|
|
|
|
- }
|
|
|
|
|
- fpsEl.textContent = Math.round(fpsSmooth);
|
|
|
|
|
- gxEl.textContent = smoothedX.toFixed(3);
|
|
|
|
|
- gyEl.textContent = smoothedY.toFixed(3);
|
|
|
|
|
- ilEl.textContent = irisVisible.l ? '●' : '○';
|
|
|
|
|
- irEl.textContent = irisVisible.r ? '●' : '○';
|
|
|
|
|
- ilEl.className = 'value ' + (irisVisible.l ? 'ok' : 'warn');
|
|
|
|
|
- irEl.className = 'value ' + (irisVisible.r ? 'ok' : 'warn');
|
|
|
|
|
- dbgSet(dbgFrame, lastFrameMs > 0 ? (lastFrameMs.toFixed(1) + ' ms') : '--');
|
|
|
|
|
-
|
|
|
|
|
- requestAnimationFrame(loop);
|
|
|
|
|
- }
|
|
|
|
|
- requestAnimationFrame(loop);
|
|
|
|
|
-
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- // MediaPipe setup
|
|
|
|
|
- // ============================================================
|
|
|
|
|
- function showError(title, msg) {
|
|
|
|
|
- console.error('[error]', title, '—', msg);
|
|
|
|
|
- errTitle.textContent = title;
|
|
|
|
|
- errMsg.textContent = msg;
|
|
|
|
|
- errOverlay.classList.add('show');
|
|
|
|
|
- loader.classList.add('hidden');
|
|
|
|
|
- statusDot.className = 'dot err';
|
|
|
|
|
- statusText.className = 'value err';
|
|
|
|
|
- statusText.textContent = 'ERROR';
|
|
|
|
|
- dbgSetError((title ? title + ': ' : '') + (msg || ''));
|
|
|
|
|
- dbgSet(dbgStage, 'error', 'err');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- errRetry.addEventListener('click', () => {
|
|
|
|
|
- errOverlay.classList.remove('show');
|
|
|
|
|
- location.reload();
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // Format bytes for the debug panel / loader text
|
|
|
|
|
- function fmtBytes(n) {
|
|
|
|
|
- if (n < 1024) return n + ' B';
|
|
|
|
|
- if (n < 1024 * 1024) return (n / 1024).toFixed(1) + ' KB';
|
|
|
|
|
- return (n / (1024 * 1024)).toFixed(2) + ' MB';
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Pre-fetch a list of MediaPipe WASM / data files with byte-level progress.
|
|
|
|
|
- // Returns a map { filename -> blob: URL } suitable for locateFile().
|
|
|
|
|
- // If any file fails, the function still resolves with whatever succeeded
|
|
|
|
|
- // (MediaPipe will retry from the network as a fallback).
|
|
|
|
|
- async function prefetchAssets(files) {
|
|
|
|
|
- console.log('[model] pre-fetching', files.length, 'asset(s)…');
|
|
|
|
|
- dbgSet(dbgModel, 'loading…', 'run');
|
|
|
|
|
- const out = {};
|
|
|
|
|
- let totalBytes = 0;
|
|
|
|
|
- for (let i = 0; i < files.length; i++) {
|
|
|
|
|
- const file = files[i];
|
|
|
|
|
- const url = 'https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/' + file;
|
|
|
|
|
- setLoaderText(
|
|
|
|
|
- 'Downloading model (' + (i + 1) + '/' + files.length + ')',
|
|
|
|
|
- file + ' (0 MB)'
|
|
|
|
|
- );
|
|
|
|
|
- console.log('[model] GET', file);
|
|
|
|
|
- try {
|
|
|
|
|
- const resp = await fetch(url, { mode: 'cors', cache: 'force-cache' });
|
|
|
|
|
- if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
|
|
|
|
- const len = parseInt(resp.headers.get('content-length') || '0', 10);
|
|
|
|
|
- // Stream the body so we can show incremental progress.
|
|
|
|
|
- const reader = resp.body && resp.body.getReader ? resp.body.getReader() : null;
|
|
|
|
|
- const chunks = [];
|
|
|
|
|
- let received = 0;
|
|
|
|
|
- if (reader) {
|
|
|
|
|
- // eslint-disable-next-line no-constant-condition
|
|
|
|
|
- while (true) {
|
|
|
|
|
- const { done, value } = await reader.read();
|
|
|
|
|
- if (done) break;
|
|
|
|
|
- chunks.push(value);
|
|
|
|
|
- received += value.byteLength;
|
|
|
|
|
- totalBytes += value.byteLength;
|
|
|
|
|
- const pct = len ? Math.round((received / len) * 100) : 0;
|
|
|
|
|
- setLoaderText(
|
|
|
|
|
- 'Downloading model (' + (i + 1) + '/' + files.length + ' · ' + pct + '%)',
|
|
|
|
|
- file + ' — ' + fmtBytes(received) + (len ? ' / ' + fmtBytes(len) : '')
|
|
|
|
|
- );
|
|
|
|
|
- dbgSet(dbgBytes, fmtBytes(totalBytes));
|
|
|
|
|
- }
|
|
|
|
|
- const blob = new Blob(chunks, { type: resp.headers.get('content-type') || 'application/octet-stream' });
|
|
|
|
|
- out[file] = URL.createObjectURL(blob);
|
|
|
|
|
- } else {
|
|
|
|
|
- const blob = await resp.blob();
|
|
|
|
|
- out[file] = URL.createObjectURL(blob);
|
|
|
|
|
- totalBytes += blob.size;
|
|
|
|
|
- dbgSet(dbgBytes, fmtBytes(totalBytes));
|
|
|
|
|
- }
|
|
|
|
|
- console.log('[model] OK', file, fmtBytes(received || (out[file] ? 0 : 0)));
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.warn('[model] pre-fetch failed for', file, e && e.message);
|
|
|
|
|
- // leave out[file] undefined → MediaPipe will fall back to the URL
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- dbgSet(dbgModel, 'ready (' + fmtBytes(totalBytes) + ')', 'ok');
|
|
|
|
|
- console.log('[model] pre-fetch done, total', fmtBytes(totalBytes));
|
|
|
|
|
- return out;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async function initFaceMesh() {
|
|
|
|
|
- console.log('[init] initFaceMesh start');
|
|
|
|
|
- setLoaderText(
|
|
|
|
|
- 'Initializing face mesh…',
|
|
|
|
|
- 'Preparing the iris-tracking model. This may take a moment.'
|
|
|
|
|
- );
|
|
|
|
|
- dbgSet(dbgModel, 'init…', 'run');
|
|
|
|
|
- try {
|
|
|
|
|
- // 1) Pre-fetch the big WASM / data files so the user can see progress
|
|
|
|
|
- // instead of a silent stall. The list mirrors the files MediaPipe
|
|
|
|
|
- // requests at runtime when refineLandmarks is enabled.
|
|
|
|
|
- const assetMap = await prefetchAssets([
|
|
|
|
|
- 'face_mesh_solution_simd_wasm_bin.js',
|
|
|
|
|
- 'face_mesh_solution_simd_wasm_bin.wasm',
|
|
|
|
|
- 'face_mesh_solution_packed_assets.data'
|
|
|
|
|
- ]);
|
|
|
|
|
-
|
|
|
|
|
- setLoaderText(
|
|
|
|
|
- 'Initializing face mesh…',
|
|
|
|
|
- 'WASM runtime ready. Constructing the model.'
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- // 2) Construct FaceMesh. If we pre-fetched a file, serve it from the
|
|
|
|
|
- // blob URL so the browser uses the cached bytes; otherwise let
|
|
|
|
|
- // MediaPipe fall back to the CDN.
|
|
|
|
|
- faceMesh = new FaceMesh({
|
|
|
|
|
- locateFile: (file) => {
|
|
|
|
|
- if (assetMap[file]) {
|
|
|
|
|
- console.log('[model] locateFile (cached)', file);
|
|
|
|
|
- return assetMap[file];
|
|
|
|
|
- }
|
|
|
|
|
- const url = 'https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/' + file;
|
|
|
|
|
- console.log('[model] locateFile (remote)', file);
|
|
|
|
|
- return url;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- faceMesh.setOptions({
|
|
|
|
|
- maxNumFaces: 1,
|
|
|
|
|
- refineLandmarks: true, // gives 468-477 iris
|
|
|
|
|
- minDetectionConfidence: 0.5,
|
|
|
|
|
- minTrackingConfidence: 0.5
|
|
|
|
|
- });
|
|
|
|
|
- faceMesh.onResults(onResults);
|
|
|
|
|
-
|
|
|
|
|
- console.log('[init] FaceMesh constructed, calling .initialize()');
|
|
|
|
|
- setLoaderText(
|
|
|
|
|
- 'Initializing face mesh…',
|
|
|
|
|
- 'Calling MediaPipe .initialize() — finalizing WASM.'
|
|
|
|
|
- );
|
|
|
|
|
- if (typeof faceMesh.initialize === 'function') {
|
|
|
|
|
- await faceMesh.initialize();
|
|
|
|
|
- }
|
|
|
|
|
- console.log('[init] FaceMesh ready');
|
|
|
|
|
- dbgSet(dbgModel, 'ready', 'ok');
|
|
|
|
|
-
|
|
|
|
|
- startCamera();
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.error('FaceMesh init failed:', e);
|
|
|
|
|
- dbgSetError('FaceMesh init failed: ' + (e && e.message ? e.message : e));
|
|
|
|
|
- dbgSet(dbgModel, 'failed', 'err');
|
|
|
|
|
- showError(
|
|
|
|
|
- 'Initialization Failed',
|
|
|
|
|
- 'Could not load MediaPipe Face Mesh (' + ((e && e.message) || 'unknown error') +
|
|
|
|
|
- '). Check your network connection and reload the page.'
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // The most recent iris ratio (used by the calibration collector).
|
|
|
|
|
- let lastGaze = { ratioX: 0.5, ratioY: 0.5, visible: false };
|
|
|
|
|
-
|
|
|
|
|
- function onResults(results) {
|
|
|
|
|
- const t1 = performance.now();
|
|
|
|
|
- lastResults = results;
|
|
|
|
|
-
|
|
|
|
|
- if (!results.multiFaceLandmarks || results.multiFaceLandmarks.length === 0) {
|
|
|
|
|
- // No face - hold last smoothed position, don't drift
|
|
|
|
|
- lastGaze.visible = false;
|
|
|
|
|
- lastFrameMs = performance.now() - t1;
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const lm = results.multiFaceLandmarks[0];
|
|
|
|
|
- const gaze = computeIrisGaze(lm);
|
|
|
|
|
- lastGaze = gaze;
|
|
|
|
|
-
|
|
|
|
|
- if (gaze.visible && calibrationData && !calPaused) {
|
|
|
|
|
- // Map raw iris ratio → screen coordinates via the OLS model
|
|
|
|
|
- const mapped = applyRegression(gaze.ratioX, gaze.ratioY);
|
|
|
|
|
- rawX = mapped.x;
|
|
|
|
|
- rawY = mapped.y;
|
|
|
|
|
- rawXValid = true;
|
|
|
|
|
- } else if (gaze.visible && !calPaused) {
|
|
|
|
|
- // No calibration yet — approximate screen position from iris ratio.
|
|
|
|
|
- // ratioX (eyeW-normalized) lives roughly in [0.2, 0.8] with the center
|
|
|
|
|
- // near 0.5; recenter at 0.5 and stretch x3 to fill the screen without
|
|
|
|
|
- // saturating.
|
|
|
|
|
- // ratioY is eyeW-normalized and signed (look-up → negative, look-down
|
|
|
|
|
- // → positive); recenter at 0.5 and stretch x3. The calibration
|
|
|
|
|
- // regression will fix the exact direction/orientation afterward — this
|
|
|
|
|
- // fallback just needs to produce values that span [0, 1] without
|
|
|
|
|
- // clipping at 1 the moment the user blinks or moves their head.
|
|
|
|
|
- rawX = clamp(0.5 + (gaze.ratioX - 0.5) * 3, 0, 1);
|
|
|
|
|
- rawY = clamp(0.5 + gaze.ratioY * 3, 0, 1);
|
|
|
|
|
- rawXValid = true;
|
|
|
|
|
- } else {
|
|
|
|
|
- // No iris detected, or calibration is paused
|
|
|
|
|
- rawXValid = false;
|
|
|
|
|
- }
|
|
|
|
|
- lastFrameMs = performance.now() - t1;
|
|
|
|
|
-
|
|
|
|
|
- // Debug log: once per second, print the current iris ratios + mapped
|
|
|
|
|
- // screen coordinates. Throttled with lastDebugLogMs so we don't flood
|
|
|
|
|
- // the console at ~30 fps. Helpful for diagnosing "why is the orb
|
|
|
|
|
- // stuck?" — open DevTools and watch the values as you move your eyes.
|
|
|
|
|
- if (rawXValid) {
|
|
|
|
|
- const nowMs = performance.now();
|
|
|
|
|
- if (nowMs - lastDebugLogMs >= 1000) {
|
|
|
|
|
- lastDebugLogMs = nowMs;
|
|
|
|
|
- console.log(
|
|
|
|
|
- '[gaze] ratioX=' + gaze.ratioX.toFixed(3) +
|
|
|
|
|
- ' ratioY=' + gaze.ratioY.toFixed(3) +
|
|
|
|
|
- ' | mappedX=' + rawX.toFixed(3) +
|
|
|
|
|
- ' mappedY=' + rawY.toFixed(3) +
|
|
|
|
|
- ' | calibrated=' + (calibrationData ? 'yes' : 'no') +
|
|
|
|
|
- ' | irisVisible=' + irisVisible.l + '/' + irisVisible.r
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- function startCamera() {
|
|
|
|
|
- console.log('[camera] startCamera');
|
|
|
|
|
- setLoaderText(
|
|
|
|
|
- 'Connecting camera…',
|
|
|
|
|
- 'Requesting webcam access. Please click "Allow" in the browser prompt.'
|
|
|
|
|
- );
|
|
|
|
|
- dbgSet(dbgCamera, 'requesting…', 'run');
|
|
|
|
|
- if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
|
|
|
|
- dbgSet(dbgCamera, 'unsupported', 'err');
|
|
|
|
|
- showError(
|
|
|
|
|
- 'Browser Not Supported',
|
|
|
|
|
- 'Your browser does not support getUserMedia. Please use a modern browser (Chrome, Firefox, Edge, Safari).'
|
|
|
|
|
- );
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- camera = new Camera(video, {
|
|
|
|
|
- onFrame: async () => {
|
|
|
|
|
- // Frame-skip: only send every 2nd frame to MediaPipe. This roughly
|
|
|
|
|
- // halves CPU usage on the face-mesh pass and noticeably improves
|
|
|
|
|
- // responsiveness on mid-range laptops.
|
|
|
|
|
- frameCount++;
|
|
|
|
|
- if (frameCount % 2 !== 0) return;
|
|
|
|
|
- if (faceMesh) {
|
|
|
|
|
- const t0 = performance.now();
|
|
|
|
|
- try {
|
|
|
|
|
- await faceMesh.send({ image: video });
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- // swallow individual frame errors
|
|
|
|
|
- console.warn('[frame] send error', e && e.message);
|
|
|
|
|
- } finally {
|
|
|
|
|
- lastMeshSendAt = performance.now() - t0;
|
|
|
|
|
- if (lastMeshSendAt > 5) {
|
|
|
|
|
- // Only log slow frames to keep the console quiet
|
|
|
|
|
- // console.log('[frame] send', lastMeshSendAt.toFixed(1), 'ms');
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- width: 640,
|
|
|
|
|
- height: 480
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- camera.start()
|
|
|
|
|
- .then(() => {
|
|
|
|
|
- // wait for metadata
|
|
|
|
|
- return new Promise(resolve => {
|
|
|
|
|
- if (video.readyState >= 1) return resolve();
|
|
|
|
|
- video.addEventListener('loadedmetadata', resolve, { once: true });
|
|
|
|
|
- });
|
|
|
|
|
- })
|
|
|
|
|
- .then(() => {
|
|
|
|
|
- console.log('[camera] stream ready, hiding loader');
|
|
|
|
|
- loader.classList.add('hidden');
|
|
|
|
|
- statusText.textContent = 'READY';
|
|
|
|
|
- dbgSet(dbgCamera, 'streaming', 'ok');
|
|
|
|
|
- dbgSet(dbgStage, 'live', 'ok');
|
|
|
|
|
-
|
|
|
|
|
- // First-time calibration: no localStorage → kick off the 9-point flow
|
|
|
|
|
- if (!calibrationData && !calActive) {
|
|
|
|
|
- // small delay so the user can settle in front of the camera
|
|
|
|
|
- setTimeout(() => { if (!calibrationData && !calActive) startCalibration(); }, 800);
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- .catch(err => {
|
|
|
|
|
- console.error('Camera error:', err);
|
|
|
|
|
- dbgSet(dbgCamera, 'error', 'err');
|
|
|
|
|
- const name = err && err.name;
|
|
|
|
|
- if (name === 'NotAllowedError' || name === 'PermissionDeniedError' || name === 'SecurityError') {
|
|
|
|
|
- showError(
|
|
|
|
|
- 'Camera Access Denied',
|
|
|
|
|
- 'We need your camera to track your gaze. Please click the camera icon in your browser\'s address bar, grant permission, then click Retry.'
|
|
|
|
|
- );
|
|
|
|
|
- } else if (name === 'NotFoundError' || name === 'OverconstrainedError') {
|
|
|
|
|
- showError(
|
|
|
|
|
- 'No Camera Found',
|
|
|
|
|
- 'No webcam was detected on this device. Please connect a camera and reload the page.'
|
|
|
|
|
- );
|
|
|
|
|
- } else if (name === 'NotReadableError' || name === 'TrackStartError') {
|
|
|
|
|
- showError(
|
|
|
|
|
- 'Camera Busy',
|
|
|
|
|
- 'Your camera appears to be in use by another application. Please close other apps (Zoom, Skype, etc.) and try again.'
|
|
|
|
|
- );
|
|
|
|
|
- } else {
|
|
|
|
|
- showError(
|
|
|
|
|
- 'Camera Error',
|
|
|
|
|
- (err && err.message) || 'An unknown error occurred while accessing the camera.'
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Wait for the global FaceMesh symbol to be available
|
|
|
|
|
- function waitFor(predicate, timeout = 20000) {
|
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
|
- const start = performance.now();
|
|
|
|
|
- (function check() {
|
|
|
|
|
- if (predicate()) return resolve();
|
|
|
|
|
- if (performance.now() - start > timeout) return reject(new Error('Timeout waiting for MediaPipe'));
|
|
|
|
|
- requestAnimationFrame(check);
|
|
|
|
|
- })();
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Kick off: stage 1 = wait for the CDN script to register window.FaceMesh
|
|
|
|
|
- setLoaderText(
|
|
|
|
|
- 'Loading MediaPipe JS…',
|
|
|
|
|
- 'Downloading the MediaPipe scripts from the CDN.'
|
|
|
|
|
- );
|
|
|
|
|
- console.log('[boot] waiting for window.FaceMesh…');
|
|
|
|
|
- waitFor(() => typeof window.FaceMesh === 'function', 20000)
|
|
|
|
|
- .then(() => {
|
|
|
|
|
- console.log('[boot] window.FaceMesh available');
|
|
|
|
|
- initFaceMesh();
|
|
|
|
|
- })
|
|
|
|
|
- .catch((e) => {
|
|
|
|
|
- console.error('[boot] FaceMesh never appeared:', e && e.message);
|
|
|
|
|
- dbgSet(dbgCdn3, 'TIMEOUT', 'err');
|
|
|
|
|
- dbgSetError('Timed out waiting for MediaPipe JS. The CDN may be blocked.');
|
|
|
|
|
- showError(
|
|
|
|
|
- 'Failed to Load MediaPipe',
|
|
|
|
|
- 'The MediaPipe library could not be loaded (timeout). The CDN may be blocked in your region — try a VPN or a different network, then reload.'
|
|
|
|
|
- );
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // Safety: if a CDN script failed before waitFor() could resolve, bail out
|
|
|
|
|
- // promptly with a clear message instead of waiting for the timeout.
|
|
|
|
|
- window.addEventListener('cdnError', () => {
|
|
|
|
|
- if (typeof window.FaceMesh !== 'function') {
|
|
|
|
|
- console.error('[boot] CDN error before FaceMesh became available');
|
|
|
|
|
- showError(
|
|
|
|
|
- 'CDN Load Failed',
|
|
|
|
|
- 'Could not download the MediaPipe library from the CDN. The CDN may be blocked in your region — try a VPN, a different network, or self-host the files, then reload.'
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
-})();
|
|
|
|
|
-</script>
|
|
|
|
|
|
|
+<!-- Application entry point (ES module) -->
|
|
|
|
|
+<script type="module" src="js/main.js"></script>
|
|
|
</body>
|
|
</body>
|
|
|
-</html>
|
|
|
|
|
|
|
+</html>
|