diff --git a/ui/pose-fusion/js/csi-simulator.js b/ui/pose-fusion/js/csi-simulator.js index 30999293..62540995 100644 --- a/ui/pose-fusion/js/csi-simulator.js +++ b/ui/pose-fusion/js/csi-simulator.js @@ -68,13 +68,38 @@ export class CsiSimulator { get isLive() { return this.mode === 'live'; } /** - * Update person state from video detection (for correlated demo data) + * Update person state from video detection (for correlated demo data). + * When person exits frame, CSI maintains presence with slow decay + * (simulating through-wall sensing capability). */ updatePersonState(presence, x, y, motion) { - this.personPresence = presence; - this.personX = x; - this.personY = y; - this.personMotion = motion; + if (presence > 0.1) { + // Person detected in video — update CSI state directly + this.personPresence = presence; + this.personX = x; + this.personY = y; + this.personMotion = motion; + this._lastSeenTime = performance.now(); + this._lastSeenX = x; + this._lastSeenY = y; + } else if (this._lastSeenTime) { + // Person NOT in video — CSI "through-wall" persistence + const elapsed = (performance.now() - this._lastSeenTime) / 1000; + // CSI can sense through walls for ~10 seconds with decaying confidence + const decayRate = 0.15; // Lose ~15% per second + this.personPresence = Math.max(0, 1.0 - elapsed * decayRate); + // Position slowly drifts (person walking behind wall) + this.personX = this._lastSeenX; + this.personY = this._lastSeenY; + this.personMotion = Math.max(0, motion * 0.5 + this.personPresence * 0.2); + + if (this.personPresence < 0.05) { + this._lastSeenTime = null; + } + } else { + this.personPresence = 0; + this.personMotion = 0; + } } /** diff --git a/ui/pose-fusion/js/main.js b/ui/pose-fusion/js/main.js index 0883998e..f18c650f 100644 --- a/ui/pose-fusion/js/main.js +++ b/ui/pose-fusion/js/main.js @@ -184,16 +184,13 @@ function mainLoop(timestamp) { motionRegion = videoCapture.detectMotionRegion(56, 56); // Feed motion to CSI simulator for correlated demo data - if (motionRegion.detected) { - csiSimulator.updatePersonState( - 1.0, - motionRegion.x + motionRegion.w / 2, - motionRegion.y + motionRegion.h / 2, - frame.motion - ); - } else { - csiSimulator.updatePersonState(0, 0.5, 0.5, 0); - } + // When detected=false, CSI simulator handles through-wall persistence + csiSimulator.updatePersonState( + motionRegion.detected ? 1.0 : 0, + motionRegion.detected ? motionRegion.x + motionRegion.w / 2 : 0.5, + motionRegion.detected ? motionRegion.y + motionRegion.h / 2 : 0.5, + frame.motion + ); fusionEngine.updateConfidence( frame.brightness, frame.motion, @@ -232,18 +229,27 @@ function mainLoop(timestamp) { // --- Pose Decode --- // For CSI-only mode, generate a synthetic motion region from CSI energy - if (mode === 'csi' && !motionRegion) { + if (mode === 'csi' && (!motionRegion || !motionRegion.detected)) { const csiPresence = csiSimulator.personPresence; if (csiPresence > 0.1) { motionRegion = { detected: true, x: 0.25, y: 0.15, w: 0.5, h: 0.7, - coverage: csiPresence + coverage: csiPresence, + motionGrid: null, + gridCols: 10, + gridRows: 8 }; } } - const keypoints = poseDecoder.decode(fusedEmb, motionRegion, elapsed); + // CSI state for through-wall tracking + const csiState = { + csiPresence: csiSimulator.personPresence, + isLive: csiSimulator.isLive + }; + + const keypoints = poseDecoder.decode(fusedEmb, motionRegion, elapsed, csiState); // --- Render Skeleton --- const labelMap = { dual: 'DUAL FUSION', video: 'VIDEO ONLY', csi: 'CSI ONLY' }; diff --git a/ui/pose-fusion/js/pose-decoder.js b/ui/pose-fusion/js/pose-decoder.js index b6befbf7..d5b0203d 100644 --- a/ui/pose-fusion/js/pose-decoder.js +++ b/ui/pose-fusion/js/pose-decoder.js @@ -1,10 +1,12 @@ /** - * PoseDecoder — Maps fused 512-dim embedding → 17 COCO keypoints. + * PoseDecoder — Maps motion detection grid → 17 COCO keypoints. * - * Uses a learned linear projection (weights shipped as JSON or generated). - * Each keypoint: (x, y, confidence) = 51 values from the embedding. + * Uses per-cell motion intensity to track actual body part positions: + * - Head: top-center motion cluster + * - Shoulders/Elbows/Wrists: lateral motion in upper body zone + * - Hips/Knees/Ankles: lower body motion distribution * - * In demo mode, generates plausible poses from motion detection + embedding features. + * When person exits frame, CSI data continues tracking (through-wall mode). */ // COCO keypoint definitions @@ -45,124 +47,187 @@ export class PoseDecoder { constructor(embeddingDim = 128) { this.embeddingDim = embeddingDim; this.smoothedKeypoints = null; - this.smoothingFactor = 0.6; // Temporal smoothing + this.smoothingFactor = 0.45; // Lower = more responsive to movement this._time = 0; + + // Through-wall tracking state + this._lastBodyState = null; + this._ghostState = null; + this._ghostConfidence = 0; + this._ghostVelocity = { x: 0, y: 0 }; + + // Arm tracking history (smoothed positions) + this._leftArmY = 0.5; + this._rightArmY = 0.5; + this._leftArmX = 0; + this._rightArmX = 0; + this._headOffsetX = 0; } /** - * Decode embedding into 17 keypoints + * Decode motion data into 17 keypoints * @param {Float32Array} embedding - Fused embedding vector - * @param {{ detected: boolean, x: number, y: number, w: number, h: number }} motionRegion + * @param {{ detected, x, y, w, h, motionGrid, gridCols, gridRows, motionCx, motionCy, exitDirection }} motionRegion * @param {number} elapsed - Time in seconds + * @param {{ csiPresence: number }} csiState - CSI sensing state for through-wall * @returns {Array<{x: number, y: number, confidence: number, name: string}>} */ - decode(embedding, motionRegion, elapsed) { + decode(embedding, motionRegion, elapsed, csiState = {}) { this._time = elapsed; - if (!motionRegion || !motionRegion.detected) { - // Fade out existing pose - if (this.smoothedKeypoints) { - return this.smoothedKeypoints.map(kp => ({ - ...kp, - confidence: kp.confidence * 0.92 - })).filter(kp => kp.confidence > 0.05); + const hasMotion = motionRegion && motionRegion.detected; + const hasCsi = csiState && csiState.csiPresence > 0.1; + + if (hasMotion) { + // Active tracking from video motion grid + this._ghostConfidence = 0; + const rawKeypoints = this._trackFromMotionGrid(motionRegion, embedding, elapsed); + this._lastBodyState = { keypoints: rawKeypoints.map(kp => ({...kp})), time: elapsed }; + + // Track exit velocity + if (motionRegion.exitDirection) { + const speed = 0.008; + this._ghostVelocity = { + x: motionRegion.exitDirection === 'left' ? -speed : motionRegion.exitDirection === 'right' ? speed : 0, + y: motionRegion.exitDirection === 'up' ? -speed : motionRegion.exitDirection === 'down' ? speed : 0 + }; } - return []; + + // Apply temporal smoothing + if (this.smoothedKeypoints && this.smoothedKeypoints.length === rawKeypoints.length) { + const alpha = this.smoothingFactor; + for (let i = 0; i < rawKeypoints.length; i++) { + rawKeypoints[i].x = alpha * this.smoothedKeypoints[i].x + (1 - alpha) * rawKeypoints[i].x; + rawKeypoints[i].y = alpha * this.smoothedKeypoints[i].y + (1 - alpha) * rawKeypoints[i].y; + } + } + + this.smoothedKeypoints = rawKeypoints; + return rawKeypoints; + + } else if (this._lastBodyState && (hasCsi || this._ghostConfidence > 0.05)) { + // Through-wall mode: person left frame but CSI still senses them + return this._trackThroughWall(elapsed, csiState); + + } else if (this.smoothedKeypoints) { + // Fade out + const faded = this.smoothedKeypoints.map(kp => ({ + ...kp, + confidence: kp.confidence * 0.88 + })).filter(kp => kp.confidence > 0.05); + if (faded.length === 0) this.smoothedKeypoints = null; + else this.smoothedKeypoints = faded; + return faded; } - // Generate base pose from motion region - const rawKeypoints = this._generatePoseFromRegion(motionRegion, embedding, elapsed); - - // Apply temporal smoothing - if (this.smoothedKeypoints && this.smoothedKeypoints.length === rawKeypoints.length) { - const alpha = this.smoothingFactor; - for (let i = 0; i < rawKeypoints.length; i++) { - rawKeypoints[i].x = alpha * this.smoothedKeypoints[i].x + (1 - alpha) * rawKeypoints[i].x; - rawKeypoints[i].y = alpha * this.smoothedKeypoints[i].y + (1 - alpha) * rawKeypoints[i].y; - } - } - - this.smoothedKeypoints = rawKeypoints; - return rawKeypoints; + return []; } - _generatePoseFromRegion(region, embedding, elapsed) { - // Person center and size from motion bounding box + /** + * Track body parts from the motion grid. + * The grid tells us WHERE motion is happening → we map that to joint positions. + */ + _trackFromMotionGrid(region, embedding, elapsed) { + const grid = region.motionGrid; + const cols = region.gridCols || 10; + const rows = region.gridRows || 8; + + // Body bounding box const cx = region.x + region.w / 2; const cy = region.y + region.h / 2; - const bodyH = Math.max(region.h, 0.3); // Minimum body height + const bodyH = Math.max(region.h, 0.3); const bodyW = Math.max(region.w, 0.15); - // Use embedding features to modulate pose - const embMod = this._extractPoseModulation(embedding); + // Analyze the motion grid to find arm positions + // Divide body into zones: head (top 20%), arms (top 60% sides), torso (center), legs (bottom 40%) + if (grid) { + const armAnalysis = this._analyzeArmMotion(grid, cols, rows, region); + // Smooth arm tracking + this._leftArmY = 0.6 * this._leftArmY + 0.4 * armAnalysis.leftArmHeight; + this._rightArmY = 0.6 * this._rightArmY + 0.4 * armAnalysis.rightArmHeight; + this._leftArmX = 0.6 * this._leftArmX + 0.4 * armAnalysis.leftArmSpread; + this._rightArmX = 0.6 * this._rightArmX + 0.4 * armAnalysis.rightArmSpread; + this._headOffsetX = 0.7 * this._headOffsetX + 0.3 * armAnalysis.headOffsetX; + } - // Generate COCO keypoints using body proportions const P = PROPORTIONS; const halfW = P.shoulderWidth * bodyH / 2; const hipHalfW = P.hipWidth * bodyH / 2; - // Breathing animation - const breathe = Math.sin(elapsed * 1.5) * 0.003; - // Subtle sway - const sway = Math.sin(elapsed * 0.7) * 0.005 * embMod.sway; + // Breathing (subtle) + const breathe = Math.sin(elapsed * 1.5) * 0.002; - // Build from hips up + // Core body positions from detection center const hipY = cy + bodyH * 0.15; const shoulderY = hipY - P.shoulderToHip * bodyH + breathe; const headY = shoulderY - P.headToShoulder * bodyH; const kneeY = hipY + P.hipToKnee * bodyH; const ankleY = kneeY + P.kneeToAnkle * bodyH; - // Arm animation from motion/embedding - const armSwing = embMod.motion * Math.sin(elapsed * 3) * 0.04; - const armBend = 0.5 + embMod.armBend * 0.3; + // HEAD follows motion centroid + const headX = cx + this._headOffsetX * bodyW * 0.3; - const elbowYL = shoulderY + P.shoulderToElbow * bodyH * armBend; - const elbowYR = shoulderY + P.shoulderToElbow * bodyH * armBend; - const wristYL = elbowYL + P.elbowToWrist * bodyH * armBend; - const wristYR = elbowYR + P.elbowToWrist * bodyH * armBend; + // ARM POSITIONS driven by motion grid analysis + // leftArmY: 0 = arm down at side, 1 = arm fully raised + // leftArmSpread: how far out the arm extends + const leftArmRaise = this._leftArmY; // 0-1 + const rightArmRaise = this._rightArmY; + const leftSpread = 0.02 + this._leftArmX * 0.12; + const rightSpread = 0.02 + this._rightArmX * 0.12; - // Leg animation - const legSwing = embMod.motion * Math.sin(elapsed * 3 + Math.PI) * 0.02; + // Elbow: interpolate between "at side" and "raised" + const lElbowY = shoulderY + P.shoulderToElbow * bodyH * (1 - leftArmRaise * 0.9); + const rElbowY = shoulderY + P.shoulderToElbow * bodyH * (1 - rightArmRaise * 0.9); + const lElbowX = cx - halfW - leftSpread; + const rElbowX = cx + halfW + rightSpread; + + // Wrist: extends further when raised + const lWristY = lElbowY + P.elbowToWrist * bodyH * (1 - leftArmRaise * 1.1); + const rWristY = rElbowY + P.elbowToWrist * bodyH * (1 - rightArmRaise * 1.1); + const lWristX = lElbowX - leftSpread * 0.6; + const rWristX = rElbowX + rightSpread * 0.6; + + // Leg motion from lower grid cells + const legMotion = grid ? this._analyzeLegMotion(grid, cols, rows) : { left: 0, right: 0 }; + const legSwing = 0.015; const keypoints = [ // 0: nose - { x: cx + sway, y: headY + 0.01, confidence: 0.9 + embMod.headConf * 0.1 }, + { x: headX, y: headY + 0.01, confidence: 0.92 }, // 1: left_eye - { x: cx - P.eyeSpacing * bodyH + sway, y: headY - 0.005, confidence: 0.85 }, + { x: headX - P.eyeSpacing * bodyH, y: headY - 0.005, confidence: 0.88 }, // 2: right_eye - { x: cx + P.eyeSpacing * bodyH + sway, y: headY - 0.005, confidence: 0.85 }, + { x: headX + P.eyeSpacing * bodyH, y: headY - 0.005, confidence: 0.88 }, // 3: left_ear - { x: cx - P.earSpacing * bodyH, y: headY + 0.005, confidence: 0.7 }, + { x: headX - P.earSpacing * bodyH, y: headY + 0.005, confidence: 0.72 }, // 4: right_ear - { x: cx + P.earSpacing * bodyH, y: headY + 0.005, confidence: 0.7 }, + { x: headX + P.earSpacing * bodyH, y: headY + 0.005, confidence: 0.72 }, // 5: left_shoulder - { x: cx - halfW + sway * 0.5, y: shoulderY, confidence: 0.92 }, + { x: cx - halfW, y: shoulderY, confidence: 0.94 }, // 6: right_shoulder - { x: cx + halfW + sway * 0.5, y: shoulderY, confidence: 0.92 }, + { x: cx + halfW, y: shoulderY, confidence: 0.94 }, // 7: left_elbow - { x: cx - halfW - 0.02 + armSwing, y: elbowYL, confidence: 0.85 }, + { x: lElbowX, y: lElbowY, confidence: 0.87 }, // 8: right_elbow - { x: cx + halfW + 0.02 - armSwing, y: elbowYR, confidence: 0.85 }, + { x: rElbowX, y: rElbowY, confidence: 0.87 }, // 9: left_wrist - { x: cx - halfW - 0.03 + armSwing * 1.5, y: wristYL, confidence: 0.8 }, + { x: lWristX, y: lWristY, confidence: 0.82 }, // 10: right_wrist - { x: cx + halfW + 0.03 - armSwing * 1.5, y: wristYR, confidence: 0.8 }, + { x: rWristX, y: rWristY, confidence: 0.82 }, // 11: left_hip - { x: cx - hipHalfW, y: hipY, confidence: 0.9 }, + { x: cx - hipHalfW, y: hipY, confidence: 0.91 }, // 12: right_hip - { x: cx + hipHalfW, y: hipY, confidence: 0.9 }, + { x: cx + hipHalfW, y: hipY, confidence: 0.91 }, // 13: left_knee - { x: cx - hipHalfW + legSwing, y: kneeY, confidence: 0.87 }, + { x: cx - hipHalfW + legMotion.left * legSwing, y: kneeY, confidence: 0.88 }, // 14: right_knee - { x: cx + hipHalfW - legSwing, y: kneeY, confidence: 0.87 }, + { x: cx + hipHalfW + legMotion.right * legSwing, y: kneeY, confidence: 0.88 }, // 15: left_ankle - { x: cx - hipHalfW + legSwing * 1.2, y: ankleY, confidence: 0.82 }, + { x: cx - hipHalfW + legMotion.left * legSwing * 1.3, y: ankleY, confidence: 0.83 }, // 16: right_ankle - { x: cx + hipHalfW - legSwing * 1.2, y: ankleY, confidence: 0.82 }, + { x: cx + hipHalfW + legMotion.right * legSwing * 1.3, y: ankleY, confidence: 0.83 }, ]; - // Add names for (let i = 0; i < keypoints.length; i++) { keypoints[i].name = KEYPOINT_NAMES[i]; } @@ -170,16 +235,139 @@ export class PoseDecoder { return keypoints; } - _extractPoseModulation(embedding) { - if (!embedding || embedding.length < 8) { - return { sway: 1, motion: 0.5, armBend: 0.5, headConf: 0.5 }; + /** + * Analyze the motion grid to determine arm positions. + * Left side of grid = left side of body, etc. + */ + _analyzeArmMotion(grid, cols, rows, region) { + // Body center column + const centerCol = Math.floor(cols / 2); + + // Upper body rows (top 60% of detected region) + const upperEnd = Math.floor(rows * 0.6); + + // Compute motion intensity for left vs right, at different heights + let leftUpperMotion = 0, leftMidMotion = 0; + let rightUpperMotion = 0, rightMidMotion = 0; + let leftCount = 0, rightCount = 0; + let headMotionX = 0, headMotionWeight = 0; + + for (let r = 0; r < upperEnd; r++) { + const heightWeight = 1.0 - (r / upperEnd) * 0.3; // Upper rows weighted more + + // Head zone: top 25%, center 40% of width + if (r < Math.floor(rows * 0.25)) { + const headLeft = Math.floor(cols * 0.3); + const headRight = Math.floor(cols * 0.7); + for (let c = headLeft; c <= headRight; c++) { + const val = grid[r][c]; + headMotionX += (c / cols - 0.5) * val; + headMotionWeight += val; + } + } + + // Left arm zone: left 40% of grid + for (let c = 0; c < Math.floor(cols * 0.4); c++) { + const val = grid[r][c]; + if (r < rows * 0.3) leftUpperMotion += val * heightWeight; + else leftMidMotion += val * heightWeight; + leftCount++; + } + + // Right arm zone: right 40% of grid + for (let c = Math.floor(cols * 0.6); c < cols; c++) { + const val = grid[r][c]; + if (r < rows * 0.3) rightUpperMotion += val * heightWeight; + else rightMidMotion += val * heightWeight; + rightCount++; + } } - // Use specific embedding dimensions to modulate pose parameters + + // Normalize + const leftTotal = leftUpperMotion + leftMidMotion; + const rightTotal = rightUpperMotion + rightMidMotion; + const maxMotion = 0.15; // Calibration threshold + + // Arm height: 0 = at side, 1 = raised + // High motion in upper-left → left arm is raised + const leftArmHeight = Math.min(1, (leftUpperMotion / maxMotion) * 2); + const rightArmHeight = Math.min(1, (rightUpperMotion / maxMotion) * 2); + + // Arm spread: how far out from body + const leftArmSpread = Math.min(1, leftTotal / maxMotion); + const rightArmSpread = Math.min(1, rightTotal / maxMotion); + + // Head offset + const headOffsetX = headMotionWeight > 0.01 ? headMotionX / headMotionWeight : 0; + + return { leftArmHeight, rightArmHeight, leftArmSpread, rightArmSpread, headOffsetX }; + } + + /** + * Analyze lower grid for leg motion. + */ + _analyzeLegMotion(grid, cols, rows) { + const lowerStart = Math.floor(rows * 0.6); + let leftMotion = 0, rightMotion = 0; + + for (let r = lowerStart; r < rows; r++) { + for (let c = 0; c < Math.floor(cols / 2); c++) { + leftMotion += grid[r][c]; + } + for (let c = Math.floor(cols / 2); c < cols; c++) { + rightMotion += grid[r][c]; + } + } + + // Return as -1 to 1 range (asymmetry indicates which leg is moving) + const total = leftMotion + rightMotion + 0.001; return { - sway: 0.5 + embedding[0] * 2, - motion: Math.abs(embedding[1]) * 3, - armBend: 0.5 + embedding[2], - headConf: 0.5 + embedding[3] * 0.5, + left: (leftMotion - rightMotion) / total, + right: (rightMotion - leftMotion) / total }; } + + /** + * Through-wall tracking: continue showing pose via CSI when person left video frame. + * The skeleton drifts in the exit direction with decreasing confidence. + */ + _trackThroughWall(elapsed, csiState) { + if (!this._lastBodyState) return []; + + const dt = elapsed - this._lastBodyState.time; + const csiPresence = csiState.csiPresence || 0; + + // Initialize ghost on first call + if (this._ghostConfidence <= 0.05) { + this._ghostConfidence = 0.8; + this._ghostState = this._lastBodyState.keypoints.map(kp => ({...kp})); + } + + // Ghost confidence decays, but CSI presence sustains it + const csiBoost = Math.min(0.7, csiPresence * 0.8); + this._ghostConfidence = Math.max(0.05, this._ghostConfidence * 0.995 - 0.001 + csiBoost * 0.002); + + // Drift the ghost in exit direction + const vx = this._ghostVelocity.x; + const vy = this._ghostVelocity.y; + + // Breathing continues via CSI + const breathe = Math.sin(elapsed * 1.5) * 0.003 * csiPresence; + + const keypoints = this._ghostState.map((kp, i) => { + return { + x: kp.x + vx * dt * 0.3, + y: kp.y + vy * dt * 0.3 + (i >= 5 && i <= 6 ? breathe : 0), + confidence: kp.confidence * this._ghostConfidence * (0.5 + csiPresence * 0.5), + name: kp.name + }; + }); + + // Slow down drift over time + this._ghostVelocity.x *= 0.998; + this._ghostVelocity.y *= 0.998; + + this.smoothedKeypoints = keypoints; + return keypoints; + } } diff --git a/ui/pose-fusion/js/video-capture.js b/ui/pose-fusion/js/video-capture.js index 649311c2..fe3ed333 100644 --- a/ui/pose-fusion/js/video-capture.js +++ b/ui/pose-fusion/js/video-capture.js @@ -126,12 +126,12 @@ export class VideoCapture { } /** - * Simple body detection from motion differencing. - * Returns approximate bounding box of moving region. - * @returns {{ x, y, w, h, detected: boolean }} + * Detect motion region + detailed motion grid for body-part tracking. + * Returns bounding box + a grid showing WHERE motion is concentrated. + * @returns {{ x, y, w, h, detected: boolean, motionGrid: number[][], gridCols: number, gridRows: number, exitDirection: string|null }} */ detectMotionRegion(targetW = 56, targetH = 56) { - if (!this.isActive || !this.prevFrame) return { detected: false }; + if (!this.isActive || !this.prevFrame) return { detected: false, motionGrid: null }; this.offscreen.width = targetW; this.offscreen.height = targetH; @@ -142,6 +142,17 @@ export class VideoCapture { let motionPixels = 0; const threshold = 25; + // Motion grid: divide frame into cells and track motion intensity per cell + const gridCols = 10; + const gridRows = 8; + const cellW = targetW / gridCols; + const cellH = targetH / gridRows; + const motionGrid = Array.from({ length: gridRows }, () => new Float32Array(gridCols)); + const cellPixels = cellW * cellH; + + // Also track motion centroid weighted by intensity + let motionCxSum = 0, motionCySum = 0, motionWeightSum = 0; + for (let y = 0; y < targetH; y++) { for (let x = 0; x < targetW; x++) { const i = y * targetW + x; @@ -156,17 +167,69 @@ export class VideoCapture { if (x > maxX) maxX = x; if (y > maxY) maxY = y; } + + // Accumulate per-cell motion intensity + const gc = Math.min(Math.floor(x / cellW), gridCols - 1); + const gr = Math.min(Math.floor(y / cellH), gridRows - 1); + const intensity = diff / (3 * 255); // Normalize 0-1 + motionGrid[gr][gc] += intensity / cellPixels; + + // Weighted centroid + if (diff > threshold) { + motionCxSum += x * diff; + motionCySum += y * diff; + motionWeightSum += diff; + } } } const detected = motionPixels > (targetW * targetH * 0.02); + + // Motion centroid (normalized 0-1) + const motionCx = motionWeightSum > 0 ? motionCxSum / (motionWeightSum * targetW) : 0.5; + const motionCy = motionWeightSum > 0 ? motionCySum / (motionWeightSum * targetH) : 0.5; + + // Detect exit direction: if centroid is near edges + let exitDirection = null; + if (detected && motionCx < 0.1) exitDirection = 'left'; + else if (detected && motionCx > 0.9) exitDirection = 'right'; + else if (detected && motionCy < 0.1) exitDirection = 'up'; + else if (detected && motionCy > 0.9) exitDirection = 'down'; + + // Track last known position for through-wall persistence + if (detected) { + this._lastDetected = { + x: minX / targetW, + y: minY / targetH, + w: (maxX - minX) / targetW, + h: (maxY - minY) / targetH, + cx: motionCx, + cy: motionCy, + exitDirection, + time: performance.now() + }; + } + return { detected, x: minX / targetW, y: minY / targetH, w: (maxX - minX) / targetW, h: (maxY - minY) / targetH, - coverage: motionPixels / (targetW * targetH) + coverage: motionPixels / (targetW * targetH), + motionGrid, + gridCols, + gridRows, + motionCx, + motionCy, + exitDirection }; } + + /** + * Get the last known detection info (for through-wall persistence) + */ + get lastDetection() { + return this._lastDetected || null; + } }