live_signs.js
source frontend javascript websocket skeleton visualization
File Path: static/live-signs.js
Purpose: Manages the client-side logic for frame capture, WebSocket communication, UI updates, skeleton visualization, and session management.
Frame Processing Loop Diagram
graph TD A[RequestAnimationFrame] --> B{Elapsed > Interval?} B -->|No| A B -->|Yes| C{Socket Open & Ready?} C -->|No| A C -->|Yes| D[Draw Video to Canvas] D --> E[Canvas.toBlob] E --> F["Socket.send(header + blob)"] F --> A
State Management
Maintains the application state in a global object:
let state = {
sessionId: new Date().toISOString(), // Unique session tracking
isSocketOpen: false, // Connection status
isSending: false, // Backpressure control
lastFrameTime: 0, // FPS throttling
lastFrameWord: "", // Stability filtering
sentenceBuffer: [], // Accumulated sentence
stabilityCounter: 0 // Consecutive frame counter
};Configuration (CONFIG)
wsUrl: WebSocket Endpoint (ws://host/live-signs).fps: Target frame rate (30).processWidth: Canvas width for resizing frames before sending (320px).jpgQuality: JPEG compression level (0.7).viz: Visualization settings forface,pose,hands,lines, andpoints.
Core Logic
Skeleton Visualization
loadSkeletonConfig(): Fetches mapping and connection data from/static/simplified_kps_connections.json.drawSkeleton(landmarks): Renders normalized landmarks onto theoverlay-canvas. Scales points based on canvas dimensions and applies region-specific colors.toggleViz(region): Toggles visibility of specific body regions (Pose/Face/Hands) and persists tolocalStorage.
Frame Processing
processFrame(): Prepends a 1-byte header to the JPEG blob indicating if landmarks are needed (based on active visualization settings) and sends it via WebSocket.
socket.onmessage(event)
- Handles inference results from the backend.
- Input: JSON
{detected_sign, confidence, landmarks, status}. - Logic:
- Triggers
drawSkeleton(data.landmarks). - Updates Confidence Meter.
- Stability Check: Increments counter if sign matches
lastFrameWord. - Threshold Reached: Triggers TTS, updates display, and appends to buffer.
- Triggers
UI & Session Management
saveCurrentSession(): Persists history tolocalStorage(last 20 sessions).- Theme Support: Includes
setTheme('light'|'dark')with persistence. - Text-to-Speech: Uses
window.speechSynthesiswith configurable language.
Related Documentation
Connects To:
- websocket.py - The backend endpoint receiving the blobs.
- index.html - The DOM elements manipulated.