algo-test.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html><head><meta charset="UTF-8"><title>Core Logic Test</title>
  3. <style>
  4. body{background:#111;color:#0f0;font-family:monospace;padding:20px;white-space:pre-wrap;font-size:13px}
  5. .err{color:#f44}.ok{color:#0f0}.warn{color:#fa0}.info{color:#88f}
  6. </style>
  7. </head>
  8. <body>
  9. <h3 style="color:#0ff">Eye Tracking - Core Algorithm Unit Test</h3>
  10. <div id="out"></div>
  11. <script>
  12. const O = document.getElementById('out');
  13. let pass=0,fail=0;
  14. function log(s,c){const d=document.createElement('div');d.className=c||'';d.textContent=s;O.appendChild(d)}
  15. function assert(cond,label){if(cond){pass++;log(' PASS: '+label,'ok')}else{fail++;log(' FAIL: '+label,'err')}}
  16. // ============ TEST 1: computeIrisGaze with right-eye flip ============
  17. log('=== TEST 1: computeIrisGaze right-eye flip ===','info');
  18. // Simulate: user looking RIGHT in un-mirrored image
  19. // Left eye: iris shifted toward outer (33, x=0.30), inner at x=0.35
  20. // Right eye: iris shifted toward outer (362, x=0.75), inner at x=0.65
  21. // In un-mirrored: looking right means iris goes toward L_OUTER (smaller x) and R_OUTER (larger x)
  22. function computeIrisGaze(landmarks) {
  23. const L_OUTER=33,L_INNER=133,L_TOP=159,L_BOT=145,L_IRIS=468;
  24. const R_OUTER=362,R_INNER=263,R_TOP=386,R_BOT=374,R_IRIS=473;
  25. function eyeData(outerIdx,innerIdx,topIdx,botIdx,irisIdx){
  26. const outer=landmarks[outerIdx],inner=landmarks[innerIdx],top=landmarks[topIdx],bot=landmarks[botIdx],iris=landmarks[irisIdx];
  27. if(!outer||!inner||!iris)return null;
  28. if((iris.x===0&&iris.y===0)||Number.isNaN(iris.x)||Number.isNaN(iris.y))return null;
  29. const eyeW=outer.x-inner.x;
  30. if(Math.abs(eyeW)<1e-6)return null;
  31. const ratioX=(iris.x-inner.x)/eyeW;
  32. const midY=(top.y+bot.y)*0.5;
  33. const ratioY=(iris.y-midY)/Math.abs(eyeW);
  34. return{ratioX,ratioY};
  35. }
  36. const l_outer=landmarks[L_OUTER],l_inner=landmarks[L_INNER],l_top=landmarks[L_TOP],l_bot=landmarks[L_BOT],l_iris=landmarks[L_IRIS];
  37. const left=(l_outer&&l_inner&&l_iris&&!(l_iris.x===0&&l_iris.y===0)&&!Number.isNaN(l_iris.x)&&!Number.isNaN(l_iris.y)&&Math.abs(l_outer.x-l_inner.x)>1e-6)?(()=>{
  38. 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);return{ratioX,ratioY};
  39. })():null;
  40. const r_outer=landmarks[R_OUTER],r_inner=landmarks[R_INNER],r_top=landmarks[R_TOP],r_bot=landmarks[R_BOT],r_iris=landmarks[R_IRIS];
  41. const right=(r_outer&&r_inner&&r_iris&&!(r_iris.x===0&&r_iris.y===0)&&!Number.isNaN(r_iris.x)&&!Number.isNaN(r_iris.y)&&Math.abs(r_inner.x-r_outer.x)>1e-6)?(()=>{
  42. const r_eyeW=r_inner.x-r_outer.x;const r_ratioX=(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);return{ratioX:r_ratioX,ratioY:r_ratioY};
  43. })():null;
  44. if(!left&&!right)return{ratioX:0.5,ratioY:0.5,visible:false};
  45. let rx,ry;
  46. if(left&&right){rx=(left.ratioX+right.ratioX)*0.5;ry=(left.ratioY+right.ratioY)*0.5}
  47. else{const s=left||right;rx=s.ratioX;ry=s.ratioY}
  48. return{ratioX:rx,ratioY:ry,visible:true};
  49. }
  50. function makeLm(obj){const lm=new Array(478);for(let i=0;i<478;i++){lm[i]={x:0,y:0,z:0}};for(const[k,v]of Object.entries(obj)){lm[parseInt(k)]={x:v[0],y:v[1],z:0}};return lm}
  51. // Case 1: Looking CENTER
  52. const lmCenter = makeLm({
  53. 33:[0.30,0.50], 133:[0.35,0.50], 159:[0.31,0.47], 145:[0.31,0.53], 468:[0.325,0.50],
  54. 362:[0.75,0.50], 263:[0.70,0.50], 386:[0.69,0.47], 374:[0.69,0.53], 473:[0.725,0.50]
  55. });
  56. const gCenter = computeIrisGaze(lmCenter);
  57. log('Looking CENTER: ratioX='+gCenter.ratioX.toFixed(4)+' ratioY='+gCenter.ratioY.toFixed(4));
  58. assert(Math.abs(gCenter.ratioX-0.5)<0.05, 'Center X should be ~0.5 (got '+gCenter.ratioX.toFixed(4)+')');
  59. assert(gCenter.visible, 'Center should be visible');
  60. // Case 2: Looking RIGHT (iris shifts toward outer corners in un-mirrored image)
  61. const lmRight = makeLm({
  62. 33:[0.30,0.50], 133:[0.35,0.50], 159:[0.31,0.47], 145:[0.31,0.53], 468:[0.29,0.50], // left iris closer to outer
  63. 362:[0.75,0.50], 263:[0.70,0.50], 386:[0.69,0.47], 374:[0.69,0.53], 473:[0.76,0.50] // right iris closer to outer
  64. });
  65. const gRight = computeIrisGaze(lmRight);
  66. log('Looking RIGHT: ratioX='+gRight.ratioX.toFixed(4)+' ratioY='+gRight.ratioY.toFixed(4));
  67. assert(gRight.ratioX < 0.45, 'Looking RIGHT: X should be < 0.45 (got '+gRight.ratioX.toFixed(4)+')');
  68. // Case 3: Looking LEFT (iris shifts toward inner corners)
  69. const lmLeft = makeLm({
  70. 33:[0.30,0.50], 133:[0.35,0.50], 159:[0.31,0.47], 145:[0.31,0.53], 468:[0.34,0.50],
  71. 362:[0.75,0.50], 263:[0.70,0.50], 386:[0.69,0.47], 374:[0.69,0.53], 473:[0.69,0.50]
  72. });
  73. const gLeft = computeIrisGaze(lmLeft);
  74. log('Looking LEFT: ratioX='+gLeft.ratioX.toFixed(4)+' ratioY='+gLeft.ratioY.toFixed(4));
  75. assert(gLeft.ratioX > 0.55, 'Looking LEFT: X should be > 0.55 (got '+gLeft.ratioX.toFixed(4)+')');
  76. // Case 4: Looking UP
  77. const lmUp = makeLm({
  78. 33:[0.30,0.50], 133:[0.35,0.50], 159:[0.31,0.47], 145:[0.31,0.53], 468:[0.325,0.49],
  79. 362:[0.75,0.50], 263:[0.70,0.50], 386:[0.69,0.47], 374:[0.69,0.53], 473:[0.725,0.49]
  80. });
  81. const gUp = computeIrisGaze(lmUp);
  82. log('Looking UP: ratioX='+gUp.ratioX.toFixed(4)+' ratioY='+gUp.ratioY.toFixed(4));
  83. assert(gUp.ratioY < -0.01, 'Looking UP: Y should be negative (got '+gUp.ratioY.toFixed(4)+')');
  84. // Case 5: Looking DOWN
  85. const lmDown = makeLm({
  86. 33:[0.30,0.50], 133:[0.35,0.50], 159:[0.31,0.47], 145:[0.31,0.53], 468:[0.325,0.51],
  87. 362:[0.75,0.50], 263:[0.70,0.50], 386:[0.69,0.47], 374:[0.69,0.53], 473:[0.725,0.51]
  88. });
  89. const gDown = computeIrisGaze(lmDown);
  90. log('Looking DOWN: ratioX='+gDown.ratioX.toFixed(4)+' ratioY='+gDown.ratioY.toFixed(4));
  91. assert(gDown.ratioY > 0.01, 'Looking DOWN: Y should be positive (got '+gDown.ratioY.toFixed(4)+')');
  92. // ============ TEST 2: OLS Linear Regression ============
  93. log('\n=== TEST 2: Linear Regression (OLS) ===','info');
  94. function solve3x3(A,b){
  95. 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]));
  96. if(Math.abs(det)<1e-9)return null;
  97. const d0=(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]));
  98. const d1=(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]));
  99. const d2=(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]));
  100. return[d0/det,d1/det,d2/det];
  101. }
  102. function olsFit(X,y){
  103. const n=X.length;const A=[[0,0,0],[0,0,0],[0,0,0]];const b=[0,0,0];
  104. for(let i=0;i<n;i++){const xi=X[i];for(let r=0;r<3;r++){for(let c=0;c<3;c++)A[r][c]+=xi[r]*xi[c];b[r]+=xi[r]*y[i]}}
  105. return solve3x3(A,b);
  106. }
  107. function fitCalibration(pts){
  108. const avgs=pts.map(p=>{let sx=0,sy=0;for(let i=0;i<p.samples.length;i++){sx+=p.samples[i].ratioX;sy+=p.samples[i].ratioY}return{ratioX:sx/p.samples.length,ratioY:sy/p.samples.length,screenX:p.screenX,screenY:p.screenY}});
  109. const X=avgs.map(p=>[1,p.ratioX,p.ratioY]);
  110. const cx=olsFit(X,avgs.map(p=>p.screenX));
  111. const cy=olsFit(X,avgs.map(p=>p.screenY));
  112. if(!cx||!cy)return null;
  113. return{coefX:[cx[0],cx[1],cx[2]],coefY:[cy[0],cy[1],cy[2]]};
  114. }
  115. // Simulate 9-point calibration with ideal data
  116. // At each screen position, generate 20 iris ratio samples
  117. const calPts = [
  118. {x:0.15,y:0.15},{x:0.5,y:0.15},{x:0.85,y:0.15},
  119. {x:0.15,y:0.5},{x:0.5,y:0.5},{x:0.85,y:0.5},
  120. {x:0.15,y:0.85},{x:0.5,y:0.85},{x:0.85,y:0.85}
  121. ];
  122. const calData = calPts.map(screenPt => {
  123. // Iris ratio roughly maps: ratioX=0.3→left, 0.7→right; ratioY=-0.03→up, 0.03→down
  124. const rx = 0.3 + screenPt.x * 0.4; // screen 0→ratioX 0.3, screen 1→ratioX 0.7
  125. const ry = -0.03 + screenPt.y * 0.06; // screen 0→ratioY -0.03, screen 1→ratioY 0.03
  126. const samples = [];
  127. for(let i=0;i<20;i++){
  128. samples.push({ratioX: rx + (Math.random()-0.5)*0.01, ratioY: ry + (Math.random()-0.5)*0.002});
  129. }
  130. return {screenX: screenPt.x, screenY: screenPt.y, samples};
  131. });
  132. const fit = fitCalibration(calData);
  133. if(!fit){log('FAIL: OLS fit returned null','err')}else{
  134. log('Calibration coefficients:', 'ok');
  135. log(' coefX = ['+fit.coefX.map(v=>v.toFixed(6)).join(', ')+']');
  136. log(' coefY = ['+fit.coefY.map(v=>v.toFixed(6)).join(', ')+']');
  137. // Test: predict center (ratioX=0.5, ratioY=0)
  138. const predX = fit.coefX[0] + fit.coefX[1]*0.5 + fit.coefX[2]*0;
  139. const predY = fit.coefY[0] + fit.coefY[1]*0.5 + fit.coefY[2]*0;
  140. log('Predict center (0.5,0): screenX='+predX.toFixed(3)+' screenY='+predY.toFixed(3));
  141. assert(Math.abs(predX-0.5)<0.05, 'Center prediction X should be ~0.5');
  142. assert(Math.abs(predY-0.5)<0.05, 'Center prediction Y should be ~0.5');
  143. // Test: predict corners
  144. const tlx = fit.coefX[0] + fit.coefX[1]*0.3 + fit.coefX[2]*(-0.03);
  145. const tly = fit.coefY[0] + fit.coefY[1]*0.3 + fit.coefY[2]*(-0.03);
  146. log('Predict TL (0.3,-0.03): screenX='+tlx.toFixed(3)+' screenY='+tly.toFixed(3));
  147. assert(Math.abs(tlx-0.15)<0.1, 'TL prediction X should be near 0.15');
  148. assert(Math.abs(tly-0.15)<0.1, 'TL prediction Y should be near 0.15');
  149. const brx = fit.coefX[0] + fit.coefX[1]*0.7 + fit.coefX[2]*0.03;
  150. const bry = fit.coefY[0] + fit.coefY[1]*0.7 + fit.coefY[2]*0.03;
  151. log('Predict BR (0.7,0.03): screenX='+brx.toFixed(3)+' screenY='+bry.toFixed(3);
  152. assert(Math.abs(brx-0.85)<0.1, 'BR prediction X should be near 0.85');
  153. assert(Math.abs(bry-0.85)<0.1, 'BR prediction Y should be near 0.85');
  154. }
  155. // ============ TEST 3: EMA + Outlier rejection ============
  156. log('\n=== TEST 3: EMA + Outlier ===','info');
  157. const EMA_ALPHA=0.3, OUTLIER_THRESHOLD=0.15;
  158. let emaX=0.5,emaY=0.5;
  159. function applyEMA(rx,ry){const dx=Math.abs(rx-emaX),dy=Math.abs(ry-emaY);if(dx>OUTLIER_THRESHOLD||dy>OUTLIER_THRESHOLD)return false;emaX=EMA_ALPHA*rx+(1-EMA_ALPHA)*emaX;emaY=EMA_ALPHA*ry+(1-EMA_ALPHA)*emaY;return true}
  160. assert(applyEMA(0.5,0.5)==true, 'EMA accepts small change');
  161. assert(Math.abs(emaX-0.5)<0.01, 'EMA stays near 0.5 for input 0.5');
  162. assert(applyEMA(0.8,0.8)==false, 'EMA rejects large jump (>0.15)');
  163. assert(Math.abs(emaX-0.5)<0.01, 'EMA holds after rejection');
  164. log('\n=== SUMMARY: '+pass+' passed, '+fail+' failed ===', fail>0?'err':'ok');
  165. </script>
  166. </body>
  167. </html>