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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
 * Visualization Module
 * Handles canvas rendering, skeleton overlay, and visual effects
 */

export class Visualizer {
    constructor() {
        this.canvas = null;
        this.ctx = null;
        this.animationFrame = null;
        this.isPlaying = false;
        
        // Visualization settings
        this.settings = {
            showSkeleton: true,
            showKeypoints: true,
            showTrails: false,
            lineThickness: 2,
            pointRadius: 4,
            trailLength: 10
        };
        
        // Color scheme
        this.colors = {
            highConfidence: '#10b981',  // Green
            medConfidence: '#f59e0b',   // Orange
            lowConfidence: '#ef4444',   // Red
            connection: '#6366f1'       // Blue
        };
        
        // Trail history
        this.trailHistory = [];
        this.maxTrailLength = 30;
        
        // Skeleton connections (MediaPipe Pose landmark indices)
        this.connections = [
            // Face
            [0, 1], [1, 2], [2, 3], [3, 7],
            [0, 4], [4, 5], [5, 6], [6, 8],
            
            // Torso
            [9, 10], [11, 12], [11, 23], [12, 24], [23, 24],
            
            // Arms
            [11, 13], [13, 15], [15, 17], [15, 19], [15, 21],
            [12, 14], [14, 16], [16, 18], [16, 20], [16, 22],
            
            // Legs
            [23, 25], [25, 27], [27, 29], [27, 31],
            [24, 26], [26, 28], [28, 30], [28, 32]
        ];
    }
    
    /**
     * Initialize canvas overlay
     */
    init(videoId, canvasId = null) {
        const video = document.getElementById(videoId);
        if (!video) {
            console.error('Video element not found');
            return false;
        }
        
        // Create or get canvas
        if (canvasId) {
            this.canvas = document.getElementById(canvasId);
        } else {
            this.canvas = this.createOverlayCanvas(video);
        }
        
        if (!this.canvas) {
            console.error('Canvas element not found or could not be created');
            return false;
        }
        
        this.ctx = this.canvas.getContext('2d');
        
        // Match canvas size to video
        this.resizeCanvas(video);
        
        // Handle video resize
        video.addEventListener('loadedmetadata', () => {
            this.resizeCanvas(video);
        });
        
        window.addEventListener('resize', () => {
            this.resizeCanvas(video);
        });
        
        return true;
    }
    
    /**
     * Create overlay canvas above video
     */
    createOverlayCanvas(video) {
        const canvas = document.createElement('canvas');
        canvas.id = 'overlay-canvas';
        canvas.style.position = 'absolute';
        canvas.style.top = '0';
        canvas.style.left = '0';
        canvas.style.pointerEvents = 'none';
        canvas.style.zIndex = '10';
        
        // Insert canvas after video
        video.parentNode.style.position = 'relative';
        video.parentNode.appendChild(canvas);
        
        return canvas;
    }
    
    /**
     * Resize canvas to match video
     */
    resizeCanvas(video) {
        if (!this.canvas) return;
        
        this.canvas.width = video.videoWidth || video.clientWidth;
        this.canvas.height = video.videoHeight || video.clientHeight;
        this.canvas.style.width = video.clientWidth + 'px';
        this.canvas.style.height = video.clientHeight + 'px';
    }
    
    /**
     * Draw skeleton from pose landmarks
     */
    drawSkeleton(landmarks, confidence = 0.5) {
        if (!this.ctx || !landmarks || landmarks.length === 0) return;
        
        this.clear();
        
        const width = this.canvas.width;
        const height = this.canvas.height;
        
        // Draw connections
        if (this.settings.showSkeleton) {
            this.ctx.lineWidth = this.settings.lineThickness;
            
            this.connections.forEach(([startIdx, endIdx]) => {
                if (startIdx < landmarks.length && endIdx < landmarks.length) {
                    const start = landmarks[startIdx];
                    const end = landmarks[endIdx];
                    
                    // Check visibility
                    if (start[2] > confidence && end[2] > confidence) {
                        const x1 = start[0] * width;
                        const y1 = start[1] * height;
                        const x2 = end[0] * width;
                        const y2 = end[1] * height;
                        
                        // Color based on average confidence
                        const avgConf = (start[2] + end[2]) / 2;
                        this.ctx.strokeStyle = this.getConfidenceColor(avgConf);
                        
                        // Draw line
                        this.ctx.beginPath();
                        this.ctx.moveTo(x1, y1);
                        this.ctx.lineTo(x2, y2);
                        this.ctx.stroke();
                    }
                }
            });
        }
        
        // Draw keypoints
        if (this.settings.showKeypoints) {
            landmarks.forEach((landmark, idx) => {
                if (landmark[2] > confidence) {
                    const x = landmark[0] * width;
                    const y = landmark[1] * height;
                    const conf = landmark[2];
                    
                    // Draw point
                    this.ctx.fillStyle = this.getConfidenceColor(conf);
                    this.ctx.beginPath();
                    this.ctx.arc(x, y, this.settings.pointRadius, 0, 2 * Math.PI);
                    this.ctx.fill();
                    
                    // Draw landmark index (for debugging)
                    // this.ctx.fillStyle = 'white';
                    // this.ctx.font = '10px Arial';
                    // this.ctx.fillText(idx, x + 5, y - 5);
                }
            });
        }
        
        // Draw trails if enabled
        if (this.settings.showTrails) {
            this.drawTrails();
        }
    }
    
    /**
     * Get color based on confidence score
     */
    getConfidenceColor(confidence) {
        if (confidence >= 0.8) {
            return this.colors.highConfidence;
        } else if (confidence >= 0.5) {
            return this.colors.medConfidence;
        } else {
            return this.colors.lowConfidence;
        }
    }
    
    /**
     * Add pose to trail history
     */
    addToTrail(landmarks) {
        if (!landmarks) return;
        
        this.trailHistory.push(landmarks);
        
        // Keep trail length limited
        if (this.trailHistory.length > this.maxTrailLength) {
            this.trailHistory.shift();
        }
    }
    
    /**
     * Draw movement trails
     */
    drawTrails() {
        if (this.trailHistory.length < 2) return;
        
        const width = this.canvas.width;
        const height = this.canvas.height;
        
        // Draw trails for key points (e.g., wrists and ankles)
        const trailPoints = [15, 16, 27, 28]; // Left/right wrists and ankles
        
        trailPoints.forEach(pointIdx => {
            this.ctx.strokeStyle = this.colors.connection;
            this.ctx.lineWidth = 1;
            this.ctx.globalAlpha = 0.5;
            
            this.ctx.beginPath();
            let firstPoint = true;
            
            this.trailHistory.forEach((landmarks, idx) => {
                if (pointIdx < landmarks.length) {
                    const point = landmarks[pointIdx];
                    
                    if (point[2] > 0.5) { // Visibility threshold
                        const x = point[0] * width;
                        const y = point[1] * height;
                        
                        if (firstPoint) {
                            this.ctx.moveTo(x, y);
                            firstPoint = false;
                        } else {
                            this.ctx.lineTo(x, y);
                        }
                    }
                }
            });
            
            this.ctx.stroke();
            this.ctx.globalAlpha = 1.0;
        });
    }
    
    /**
     * Clear canvas
     */
    clear() {
        if (!this.ctx) return;
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
    
    /**
     * Draw text overlay
     */
    drawText(text, x, y, options = {}) {
        if (!this.ctx) return;
        
        const {
            font = '16px Arial',
            color = '#ffffff',
            background = 'rgba(0, 0, 0, 0.7)',
            padding = 8
        } = options;
        
        this.ctx.font = font;
        const metrics = this.ctx.measureText(text);
        const textWidth = metrics.width;
        const textHeight = 20; // Approximate height
        
        // Draw background
        if (background) {
            this.ctx.fillStyle = background;
            this.ctx.fillRect(
                x - padding,
                y - textHeight - padding,
                textWidth + padding * 2,
                textHeight + padding * 2
            );
        }
        
        // Draw text
        this.ctx.fillStyle = color;
        this.ctx.fillText(text, x, y);
    }
    
    /**
     * Draw info box with stats
     */
    drawInfoBox(info, position = 'top-left') {
        if (!this.ctx || !info) return;
        
        const padding = 10;
        const lineHeight = 20;
        const lines = Object.entries(info).map(([key, value]) => `${key}: ${value}`);
        
        // Calculate box dimensions
        this.ctx.font = '14px Arial';
        const maxWidth = Math.max(...lines.map(line => this.ctx.measureText(line).width));
        const boxWidth = maxWidth + padding * 2;
        const boxHeight = lines.length * lineHeight + padding * 2;
        
        // Determine position
        let x, y;
        switch (position) {
            case 'top-left':
                x = padding;
                y = padding;
                break;
            case 'top-right':
                x = this.canvas.width - boxWidth - padding;
                y = padding;
                break;
            case 'bottom-left':
                x = padding;
                y = this.canvas.height - boxHeight - padding;
                break;
            case 'bottom-right':
                x = this.canvas.width - boxWidth - padding;
                y = this.canvas.height - boxHeight - padding;
                break;
            default:
                x = padding;
                y = padding;
        }
        
        // Draw background
        this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
        this.ctx.fillRect(x, y, boxWidth, boxHeight);
        
        // Draw border
        this.ctx.strokeStyle = '#6366f1';
        this.ctx.lineWidth = 2;
        this.ctx.strokeRect(x, y, boxWidth, boxHeight);
        
        // Draw text
        this.ctx.fillStyle = '#ffffff';
        this.ctx.font = '14px Arial';
        lines.forEach((line, idx) => {
            this.ctx.fillText(line, x + padding, y + padding + (idx + 1) * lineHeight);
        });
    }
    
    /**
     * Draw FPS counter
     */
    drawFPS(fps) {
        this.drawText(`FPS: ${fps.toFixed(1)}`, 10, 30, {
            font: '16px monospace',
            color: '#10b981'
        });
    }
    
    /**
     * Toggle skeleton visibility
     */
    toggleSkeleton() {
        this.settings.showSkeleton = !this.settings.showSkeleton;
        return this.settings.showSkeleton;
    }
    
    /**
     * Toggle keypoints visibility
     */
    toggleKeypoints() {
        this.settings.showKeypoints = !this.settings.showKeypoints;
        return this.settings.showKeypoints;
    }
    
    /**
     * Toggle trails
     */
    toggleTrails() {
        this.settings.showTrails = !this.settings.showTrails;
        if (!this.settings.showTrails) {
            this.trailHistory = [];
        }
        return this.settings.showTrails;
    }
    
    /**
     * Update settings
     */
    updateSettings(newSettings) {
        this.settings = { ...this.settings, ...newSettings };
    }
    
    /**
     * Destroy visualizer
     */
    destroy() {
        if (this.animationFrame) {
            cancelAnimationFrame(this.animationFrame);
        }
        if (this.canvas && this.canvas.parentNode) {
            this.canvas.parentNode.removeChild(this.canvas);
        }
        this.ctx = null;
        this.canvas = null;
    }
}

// Create global instance
export const visualizer = new Visualizer();