| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <!DOCTYPE html>
- <html><head><meta charset="UTF-8"><title>Core Logic Test</title>
- <style>
- body{background:#111;color:#0f0;font-family:monospace;padding:20px;white-space:pre-wrap;font-size:13px}
- .err{color:#f44}.ok{color:#0f0}.warn{color:#fa0}.info{color:#88f}
- </style>
- </head>
- <body>
- <h3 style="color:#0ff">Eye Tracking - Core Algorithm Unit Test</h3>
- <div id="out"></div>
- <script>
- const O = document.getElementById('out');
- let pass=0,fail=0;
- function log(s,c){const d=document.createElement('div');d.className=c||'';d.textContent=s;O.appendChild(d)}
- function assert(cond,label){if(cond){pass++;log(' PASS: '+label,'ok')}else{fail++;log(' FAIL: '+label,'err')}}
- // ============ TEST 1: computeIrisGaze with right-eye flip ============
- log('=== TEST 1: computeIrisGaze right-eye flip ===','info');
- // Simulate: user looking RIGHT in un-mirrored image
- // Left eye: iris shifted toward outer (33, x=0.30), inner at x=0.35
- // Right eye: iris shifted toward outer (362, x=0.75), inner at x=0.65
- // In un-mirrored: looking right means iris goes toward L_OUTER (smaller x) and R_OUTER (larger x)
- function computeIrisGaze(landmarks) {
- const L_OUTER=33,L_INNER=133,L_TOP=159,L_BOT=145,L_IRIS=468;
- const R_OUTER=362,R_INNER=263,R_TOP=386,R_BOT=374,R_IRIS=473;
-
- function eyeData(outerIdx,innerIdx,topIdx,botIdx,irisIdx){
- const outer=landmarks[outerIdx],inner=landmarks[innerIdx],top=landmarks[topIdx],bot=landmarks[botIdx],iris=landmarks[irisIdx];
- if(!outer||!inner||!iris)return null;
- if((iris.x===0&&iris.y===0)||Number.isNaN(iris.x)||Number.isNaN(iris.y))return null;
- const eyeW=outer.x-inner.x;
- if(Math.abs(eyeW)<1e-6)return null;
- const ratioX=(iris.x-inner.x)/eyeW;
- const midY=(top.y+bot.y)*0.5;
- const ratioY=(iris.y-midY)/Math.abs(eyeW);
- return{ratioX,ratioY};
- }
- 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];
- 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)?(()=>{
- 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};
- })():null;
- 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];
- 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)?(()=>{
- 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};
- })():null;
- 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};
- }
- 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}
- // Case 1: Looking CENTER
- const lmCenter = makeLm({
- 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],
- 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]
- });
- const gCenter = computeIrisGaze(lmCenter);
- log('Looking CENTER: ratioX='+gCenter.ratioX.toFixed(4)+' ratioY='+gCenter.ratioY.toFixed(4));
- assert(Math.abs(gCenter.ratioX-0.5)<0.05, 'Center X should be ~0.5 (got '+gCenter.ratioX.toFixed(4)+')');
- assert(gCenter.visible, 'Center should be visible');
- // Case 2: Looking RIGHT (iris shifts toward outer corners in un-mirrored image)
- const lmRight = makeLm({
- 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
- 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
- });
- const gRight = computeIrisGaze(lmRight);
- log('Looking RIGHT: ratioX='+gRight.ratioX.toFixed(4)+' ratioY='+gRight.ratioY.toFixed(4));
- assert(gRight.ratioX < 0.45, 'Looking RIGHT: X should be < 0.45 (got '+gRight.ratioX.toFixed(4)+')');
- // Case 3: Looking LEFT (iris shifts toward inner corners)
- const lmLeft = makeLm({
- 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],
- 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]
- });
- const gLeft = computeIrisGaze(lmLeft);
- log('Looking LEFT: ratioX='+gLeft.ratioX.toFixed(4)+' ratioY='+gLeft.ratioY.toFixed(4));
- assert(gLeft.ratioX > 0.55, 'Looking LEFT: X should be > 0.55 (got '+gLeft.ratioX.toFixed(4)+')');
- // Case 4: Looking UP
- const lmUp = makeLm({
- 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],
- 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]
- });
- const gUp = computeIrisGaze(lmUp);
- log('Looking UP: ratioX='+gUp.ratioX.toFixed(4)+' ratioY='+gUp.ratioY.toFixed(4));
- assert(gUp.ratioY < -0.01, 'Looking UP: Y should be negative (got '+gUp.ratioY.toFixed(4)+')');
- // Case 5: Looking DOWN
- const lmDown = makeLm({
- 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],
- 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]
- });
- const gDown = computeIrisGaze(lmDown);
- log('Looking DOWN: ratioX='+gDown.ratioX.toFixed(4)+' ratioY='+gDown.ratioY.toFixed(4));
- assert(gDown.ratioY > 0.01, 'Looking DOWN: Y should be positive (got '+gDown.ratioY.toFixed(4)+')');
- // ============ TEST 2: OLS Linear Regression ============
- log('\n=== TEST 2: Linear Regression (OLS) ===','info');
- 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 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]));
- 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]));
- 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]));
- return[d0/det,d1/det,d2/det];
- }
- function olsFit(X,y){
- const n=X.length;const A=[[0,0,0],[0,0,0],[0,0,0]];const b=[0,0,0];
- 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]}}
- return solve3x3(A,b);
- }
- function fitCalibration(pts){
- 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}});
- const X=avgs.map(p=>[1,p.ratioX,p.ratioY]);
- const cx=olsFit(X,avgs.map(p=>p.screenX));
- const cy=olsFit(X,avgs.map(p=>p.screenY));
- if(!cx||!cy)return null;
- return{coefX:[cx[0],cx[1],cx[2]],coefY:[cy[0],cy[1],cy[2]]};
- }
- // Simulate 9-point calibration with ideal data
- // At each screen position, generate 20 iris ratio samples
- const calPts = [
- {x:0.15,y:0.15},{x:0.5,y:0.15},{x:0.85,y:0.15},
- {x:0.15,y:0.5},{x:0.5,y:0.5},{x:0.85,y:0.5},
- {x:0.15,y:0.85},{x:0.5,y:0.85},{x:0.85,y:0.85}
- ];
- const calData = calPts.map(screenPt => {
- // Iris ratio roughly maps: ratioX=0.3→left, 0.7→right; ratioY=-0.03→up, 0.03→down
- const rx = 0.3 + screenPt.x * 0.4; // screen 0→ratioX 0.3, screen 1→ratioX 0.7
- const ry = -0.03 + screenPt.y * 0.06; // screen 0→ratioY -0.03, screen 1→ratioY 0.03
- const samples = [];
- for(let i=0;i<20;i++){
- samples.push({ratioX: rx + (Math.random()-0.5)*0.01, ratioY: ry + (Math.random()-0.5)*0.002});
- }
- return {screenX: screenPt.x, screenY: screenPt.y, samples};
- });
- const fit = fitCalibration(calData);
- if(!fit){log('FAIL: OLS fit returned null','err')}else{
- log('Calibration coefficients:', 'ok');
- log(' coefX = ['+fit.coefX.map(v=>v.toFixed(6)).join(', ')+']');
- log(' coefY = ['+fit.coefY.map(v=>v.toFixed(6)).join(', ')+']');
-
- // Test: predict center (ratioX=0.5, ratioY=0)
- const predX = fit.coefX[0] + fit.coefX[1]*0.5 + fit.coefX[2]*0;
- const predY = fit.coefY[0] + fit.coefY[1]*0.5 + fit.coefY[2]*0;
- log('Predict center (0.5,0): screenX='+predX.toFixed(3)+' screenY='+predY.toFixed(3));
- assert(Math.abs(predX-0.5)<0.05, 'Center prediction X should be ~0.5');
- assert(Math.abs(predY-0.5)<0.05, 'Center prediction Y should be ~0.5');
-
- // Test: predict corners
- const tlx = fit.coefX[0] + fit.coefX[1]*0.3 + fit.coefX[2]*(-0.03);
- const tly = fit.coefY[0] + fit.coefY[1]*0.3 + fit.coefY[2]*(-0.03);
- log('Predict TL (0.3,-0.03): screenX='+tlx.toFixed(3)+' screenY='+tly.toFixed(3));
- assert(Math.abs(tlx-0.15)<0.1, 'TL prediction X should be near 0.15');
- assert(Math.abs(tly-0.15)<0.1, 'TL prediction Y should be near 0.15');
-
- const brx = fit.coefX[0] + fit.coefX[1]*0.7 + fit.coefX[2]*0.03;
- const bry = fit.coefY[0] + fit.coefY[1]*0.7 + fit.coefY[2]*0.03;
- log('Predict BR (0.7,0.03): screenX='+brx.toFixed(3)+' screenY='+bry.toFixed(3);
- assert(Math.abs(brx-0.85)<0.1, 'BR prediction X should be near 0.85');
- assert(Math.abs(bry-0.85)<0.1, 'BR prediction Y should be near 0.85');
- }
- // ============ TEST 3: EMA + Outlier rejection ============
- log('\n=== TEST 3: EMA + Outlier ===','info');
- const EMA_ALPHA=0.3, OUTLIER_THRESHOLD=0.15;
- let emaX=0.5,emaY=0.5;
- 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}
- assert(applyEMA(0.5,0.5)==true, 'EMA accepts small change');
- assert(Math.abs(emaX-0.5)<0.01, 'EMA stays near 0.5 for input 0.5');
- assert(applyEMA(0.8,0.8)==false, 'EMA rejects large jump (>0.15)');
- assert(Math.abs(emaX-0.5)<0.01, 'EMA holds after rejection');
- log('\n=== SUMMARY: '+pass+' passed, '+fail+' failed ===', fail>0?'err':'ok');
- </script>
- </body>
- </html>
|