File size: 12,207 Bytes
60fbe12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edfb162
 
 
60fbe12
edfb162
 
60fbe12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ad1693
 
 
 
 
 
 
 
60fbe12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ad1693
60fbe12
 
 
 
 
 
9cd7dfb
 
 
 
 
60fbe12
 
9cd7dfb
 
 
 
 
 
 
60fbe12
9cd7dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60fbe12
 
 
 
 
9cd7dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60fbe12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
 * PostureMonitor — MediaPipe BlazePose in-browser.
 * GPU delegate with automatic CPU fallback.
 * Video stream assigned immediately; pose is optional overlay.
 */
import { useCallback, useEffect, useRef, useState } from "react";
import { api } from "../api/client";

const POSE_LABELS = {
  GOOD:             { color: "#48bb78", text: "✅ Good Posture"      },
  SLOUCHING:        { color: "#ed8936", text: "⚠️ Sit Up Straight"   },
  HEAD_FORWARD:     { color: "#ed8936", text: "⚠️ Head Forward"      },
  LOOKING_AWAY:     { color: "#fc8181", text: "👀 Looking Away"      },
  FACE_NOT_VISIBLE: { color: "#fc8181", text: "👁 Face Not Visible"  },
  NO_POSE:          { color: "#a0a3b1", text: "📷 Loading Camera…"   },
};

export default function PostureMonitor({ sessionId, stream }) {
  const videoRef   = useRef(null);
  const canvasRef  = useRef(null);
  const poseRef    = useRef(null);
  const timerRef   = useRef(null);
  const sendRef    = useRef(null);
  const metricsRef = useRef([]);
  const lastTsRef  = useRef(0);
  const baselineRef = useRef({ count: 0, noseShoulderOffset: 0 });

  const [status,    setStatus]    = useState("NO_POSE");
  const [poseReady, setPoseReady] = useState(false);
  const [poseError, setPoseError] = useState(null);

  // Assign stream to video immediately — even if pose fails, camera still shows
  useEffect(() => {
    if (!stream || !videoRef.current) return;
    videoRef.current.srcObject = stream;
    videoRef.current.play().catch(() => {});
  }, [stream]);

  const analysePosture = useCallback((landmarks) => {
    // Q1: Head-forward check uses NOSE (0), LEFT_SHOULDER (11), RIGHT_SHOULDER (12),
    // and torso/slouching also uses LEFT_HIP (23), RIGHT_HIP (24).
    // Q2: Previous HEAD_FORWARD threshold was nose.z < lS.z - 0.15.
    // Q3: Previous torso_angle was not calculated from landmarks here; metrics used a hardcoded 90.
    if (!landmarks || landmarks.length < 25) {
      return { label: "FACE_NOT_VISIBLE", score: 0.1, torsoAngle: 0 };
    }

    const NOSE = 0;
    const LEFT_SHOULDER = 11;
    const RIGHT_SHOULDER = 12;
    const LEFT_HIP = 23;
    const RIGHT_HIP = 24;

    const nose = landmarks[NOSE];
    const lS = landmarks[LEFT_SHOULDER];
    const rS = landmarks[RIGHT_SHOULDER];
    const lH = landmarks[LEFT_HIP];
    const rH = landmarks[RIGHT_HIP];

    if (!nose || !lS || !rS || !lH || !rH) {
      return { label: "FACE_NOT_VISIBLE", score: 0.1, torsoAngle: 0 };
    }
    if (nose.visibility < 0.5) {
      return { label: "LOOKING_AWAY", score: 0.1, torsoAngle: 0 };
    }

    const shoulderMidX = (lS.x + rS.x) / 2;
    const shoulderMidY = (lS.y + rS.y) / 2;
    const hipMidY = (lH.y + rH.y) / 2;
    const shoulderDiffY = Math.abs(lS.y - rS.y);
    const noseForwardOffset = Math.abs(nose.x - shoulderMidX);
    const torsoHeight = hipMidY - shoulderMidY;
    const torsoAngle = Math.round(Math.abs(shoulderMidY - hipMidY) * 100);

    // First 3 snapshots: calibrate baseline nose-to-shoulder horizontal offset.
    if (baselineRef.current.count < 3) {
      baselineRef.current.noseShoulderOffset += noseForwardOffset;
      baselineRef.current.count += 1;
      return { label: "NO_POSE", score: 0.5, torsoAngle };
    }
    const baselineOffset = baselineRef.current.noseShoulderOffset / baselineRef.current.count;

    // Relaxed thresholds to reduce false positives
    const isHeadForward = (noseForwardOffset - baselineOffset) > 0.12;
    const isSlouching = torsoHeight < 0.18 || shoulderDiffY > 0.12;
    const isHeadUp = nose.y < shoulderMidY;
    const shouldersLevel = shoulderDiffY < 0.10;
    const headAligned = (noseForwardOffset - baselineOffset) <= 0.08;

    if (isHeadForward) {
      return { label: "HEAD_FORWARD", score: 0.4, torsoAngle };
    }
    if (isSlouching) {
      return { label: "SLOUCHING", score: 0.3, torsoAngle };
    }
    if (headAligned && shouldersLevel && isHeadUp) {
      return { label: "GOOD", score: 0.9, torsoAngle };
    }
    return { label: "SLOUCHING", score: 0.3, torsoAngle };
  }, []);

  const drawSkeleton = useCallback((canvas, landmarks) => {
    const ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    if (!landmarks) return;

    const W = canvas.width, H = canvas.height;
    const CONNECTIONS = [
      [11,12],[11,23],[12,24],[23,24],
      [11,13],[13,15],[12,14],[14,16],
      [0,11],[0,12],
    ];

    ctx.strokeStyle = "rgba(102,126,234,0.85)";
    ctx.lineWidth   = 2.5;
    for (const [a, b] of CONNECTIONS) {
      const pa = landmarks[a], pb = landmarks[b];
      if (!pa || !pb || pa.visibility < 0.45 || pb.visibility < 0.45) continue;
      ctx.beginPath();
      ctx.moveTo(pa.x * W, pa.y * H);
      ctx.lineTo(pb.x * W, pb.y * H);
      ctx.stroke();
    }
    for (const i of [0, 11, 12, 23, 24]) {
      const p = landmarks[i];
      if (!p || p.visibility < 0.45) continue;
      ctx.beginPath();
      ctx.arc(p.x * W, p.y * H, 5, 0, Math.PI * 2);
      ctx.fillStyle = "#667eea";
      ctx.fill();
    }
  }, []);

  // MediaPipe init — GPU first, CPU fallback
  useEffect(() => {
    if (!stream) return;
    let cancelled = false;

    (async () => {
      for (const delegate of ["GPU", "CPU"]) {
        if (cancelled) return;
        try {
          const { PoseLandmarker, FilesetResolver } =
            await import("@mediapipe/tasks-vision");

          const vision = await FilesetResolver.forVisionTasks(
            "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.14/wasm"
          );

          const landmarker = await PoseLandmarker.createFromOptions(vision, {
            baseOptions: {
              modelAssetPath:
                "https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_lite/float16/1/pose_landmarker_lite.task",
              delegate,
            },
            runningMode: "VIDEO",
            numPoses:    1,
          });

          poseRef.current = landmarker;
          setPoseReady(true);
          setPoseError(null);
          console.log(`✅ MediaPipe pose ready (delegate=${delegate})`);
          return;
        } catch (e) {
          console.warn(`MediaPipe init failed (delegate=${delegate}):`, e);
          if (delegate === "CPU") {
            setPoseError("Pose detection unavailable — camera shown without skeleton.");
          }
        }
      }
    })();

    return () => {
      cancelled = true;
      // Close PoseLandmarker to free resources
      if (poseRef.current && poseRef.current.close) {
        poseRef.current.close();
        poseRef.current = null;
      }
    };
  }, [stream]);

  // Analysis loop
  useEffect(() => {
    if (!poseReady || !poseRef.current) return;
    baselineRef.current = { count: 0, noseShoulderOffset: 0 };

    timerRef.current = setInterval(() => {
      const video  = videoRef.current;
      const canvas = canvasRef.current;
      if (!video || !canvas || video.readyState < 2 || video.paused) return;

      canvas.width  = video.videoWidth  || 320;
      canvas.height = video.videoHeight || 240;

      const now = performance.now();
      if (now <= lastTsRef.current) return;
      lastTsRef.current = now;

      try {
        const result = poseRef.current.detectForVideo(video, now);
        const lm     = result.landmarks?.[0];
        drawSkeleton(canvas, lm);
        const posture = analysePosture(lm);
        setStatus(posture.label);
        metricsRef.current.push({
          posture_score: posture.score,
          posture_label: posture.label,
          spine_height:  posture.torsoAngle,
          hands_visible: true,
          timestamp:     Date.now(),
        });
      } catch (_) {}
    }, 500);

    // Metrics buffer with retry logic
    const metricsBuffer = [];
    let consecutiveFailures = 0;
    const MAX_BUFFER_SIZE = 100; // Prevent unbounded growth

    sendRef.current = setInterval(async () => {
      if (!sessionId || metricsRef.current.length === 0) return;

      // Add new metrics to buffer
      metricsBuffer.push(...metricsRef.current);
      if (metricsBuffer.length > MAX_BUFFER_SIZE) {
        // Keep only the most recent metrics if buffer overflows
        metricsBuffer.splice(0, metricsBuffer.length - MAX_BUFFER_SIZE);
      }
      metricsRef.current = [];

      // Send latest metric from buffer
      const latest = metricsBuffer[metricsBuffer.length - 1];
      if (!latest) return;

      try {
        await api.sendPosture({ session_id: sessionId, metrics: latest });
        // Success - clear buffer and reset failure count
        metricsBuffer.length = 0;
        consecutiveFailures = 0;
      } catch (err) {
        consecutiveFailures++;
        console.warn(`Posture metrics send failed (${consecutiveFailures}x):`, err);

        // After 3 consecutive failures, send a batch of aggregated data
        if (consecutiveFailures >= 3 && metricsBuffer.length > 1) {
          try {
            // Calculate average metrics for batch
            const avgScore = metricsBuffer.reduce((sum, m) => sum + (m.posture_score || 0), 0) / metricsBuffer.length;
            const modeLabel = metricsBuffer
              .map(m => m.posture_label)
              .sort((a, b) =>
                metricsBuffer.filter(m => m.posture_label === a).length -
                metricsBuffer.filter(m => m.posture_label === b).length
              ).pop();

            const batchMetric = {
              posture_score: avgScore,
              posture_label: modeLabel,
              spine_height: metricsBuffer[metricsBuffer.length - 1].spine_height,
              hands_visible: true,
              timestamp: Date.now(),
              batch_size: metricsBuffer.length,
              batch_aggregated: true
            };

            await api.sendPosture({ session_id: sessionId, metrics: batchMetric });
            metricsBuffer.length = 0;
            consecutiveFailures = 0;
          } catch (batchErr) {
            console.warn("Batch posture send also failed:", batchErr);
          }
        }
      }
    }, 30000);

    return () => {
      clearInterval(timerRef.current);
      clearInterval(sendRef.current);
      
      // Attempt to flush remaining metrics on unmount
      if (sessionId && metricsBuffer.length > 0) {
        const latest = metricsBuffer[metricsBuffer.length - 1];
        const payload = JSON.stringify({
          session_id: sessionId,
          metrics: {
            ...latest,
            flush_on_unmount: true,
            timestamp: Date.now()
          }
        });
        
        // Use sendBeacon for best-effort delivery on unmount
        if (navigator.sendBeacon) {
          navigator.sendBeacon('/api/posture/report', new Blob([payload], { type: 'application/json' }));
        }
        
        metricsBuffer.length = 0;
      }
    };
  }, [poseReady, sessionId, analysePosture, drawSkeleton]);

  const badge = POSE_LABELS[status] || POSE_LABELS.GOOD;

  return (
    <div style={S.wrap}>
      <div style={S.videoWrap}>
        <video ref={videoRef} muted playsInline style={S.video} />
        <canvas ref={canvasRef} style={S.canvas} />
        <div style={{ ...S.badge, background: badge.color + "22", border: `1px solid ${badge.color}55`, color: badge.color }}>
          {badge.text}
        </div>
        {poseError && <div style={S.errorNote}>⚠️ {poseError}</div>}
      </div>
    </div>
  );
}

const S = {
  wrap:      { width: "100%" },
  videoWrap: { position: "relative", background: "#0f1117", borderRadius: "12px", overflow: "hidden", aspectRatio: "4/3" },
  video:     { width: "100%", height: "100%", objectFit: "cover", transform: "scaleX(-1)" },
  canvas:    { position: "absolute", inset: 0, width: "100%", height: "100%", transform: "scaleX(-1)", pointerEvents: "none" },
  badge:     { position: "absolute", bottom: "8px", left: "8px", right: "8px", borderRadius: "6px", padding: "5px 10px", fontSize: "12px", fontWeight: 600, textAlign: "center" },
  errorNote: { position: "absolute", top: "8px", left: "8px", right: "8px", background: "rgba(0,0,0,0.75)", color: "#a0a3b1", fontSize: "11px", textAlign: "center", borderRadius: "4px", padding: "4px 8px" },
};