File size: 9,819 Bytes
2b66289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', async () => {
    // Initialize elements
    const videoElem = document.getElementById('video');
    const overlay = document.getElementById('overlay');
    const ctx = overlay.getContext('2d');
    const status = document.getElementById('status');
    const predictionSpan = document.getElementById('prediction');
    const confidenceBar = document.getElementById('confidenceBar');
    const datasetJSON = document.getElementById('datasetJSON');
    
    let labels = [];
    let recording = false;
    let recordingLabel = null;
    let predicting = false;
    let knnK = 5;
    let lastSpoken = null;

    // Set initial status
    setStatus('Initializing camera...');

    // === Utility Functions ===
    function setStatus(s) { 
        status.textContent = `Status: ${s}`; 
    }

    function speak(text) {
        if ('speechSynthesis' in window && text !== lastSpoken) {
            lastSpoken = text;
            const msg = new SpeechSynthesisUtterance(text);
            msg.rate = 1; 
            msg.pitch = 1;
            speechSynthesis.cancel();
            speechSynthesis.speak(msg);
        }
    }

    // === Landmark Processing ===
    function landmarksToVector(landmarks) {
        if (!landmarks || landmarks.length === 0) return null;
        
        const wrist = landmarks[0];
        const ref = landmarks[9] || landmarks[8];
        const dx = ref.x - wrist.x;
        const dy = ref.y - wrist.y;
        const scale = Math.hypot(dx, dy) || 1;
        
        const vec = [];
        for (let p of landmarks) {
            vec.push(
                (p.x - wrist.x) / scale,
                (p.y - wrist.y) / scale,
                (p.z - wrist.z) / scale
            );
        }
        return vec;
    }

    function euclid(a, b) {
        let s = 0;
        for (let i = 0; i < a.length; i++) { 
            const d = a[i] - b[i]; 
            s += d * d; 
        }
        return Math.sqrt(s);
    }

    function knnPredict(vec) {
        const all = [];
        for (const lab of labels) {
            for (const ex of lab.examples) {
                all.push({ label: lab.name, dist: euclid(vec, ex) });
            }
        }
        
        if (!all.length) return { label: null, confidence: 0 };
        
        all.sort((a, b) => a.dist - b.dist);
        const k = Math.min(knnK, all.length);
        const top = all.slice(0, k);
        
        const counts = {};
        for (const t of top) counts[t.label] = (counts[t.label] || 0) + 1;
        
        let best = null, bestCount = 0;
        for (const l in counts) {
            if (counts[l] > bestCount) {
                best = l;
                bestCount = counts[l];
            }
        }
        
        return { 
            label: best, 
            confidence: bestCount / k 
        };
    }

    // === UI: Labels Management ===
    document.getElementById('addLabel').addEventListener('click', () => {
        const name = document.getElementById('labelInput').value.trim();
        if (!name) return alert('Please enter a label name');
        if (labels.find(l => l.name === name)) return alert('Label already exists');
        
        labels.push({ name, examples: [] });
        renderLabels();
        document.getElementById('labelInput').value = '';
    });

    function renderLabels() {
        const labelsArea = document.getElementById('labelsArea');
        labelsArea.innerHTML = '';
        
        labels.forEach((lab, idx) => {
            const div = document.createElement('div');
            div.className = 'flex items-center justify-between bg-gray-700 p-2 rounded-lg';
            
            const labelBtn = document.createElement('button');
            labelBtn.className = `flex items-center gap-2 px-3 py-1 rounded-md transition ${
                recordingLabel === lab.name ? 'bg-teal-600 text-white' : 'bg-gray-600 hover:bg-gray-500'
            }`;
            labelBtn.innerHTML = `
                <i data-feather="${lab.name.length > 1 ? 'type' : 'hash'}" width="16"></i>
                ${lab.name} <span class="text-xs opacity-75">(${lab.examples.length})</span>
            `;
            labelBtn.addEventListener('click', () => {
                recordingLabel = lab.name;
                renderLabels();
            });
            
            const deleteBtn = document.createElement('button');
            deleteBtn.className = 'p-1 text-red-400 hover:text-red-300 rounded-full';
            deleteBtn.innerHTML = '<i data-feather="trash-2" width="16"></i>';
            deleteBtn.addEventListener('click', () => {
                if (confirm(`Delete label "${lab.name}"?`)) {
                    labels.splice(idx, 1);
                    if (recordingLabel === lab.name) recordingLabel = null;
                    renderLabels();
                }
            });
            
            div.appendChild(labelBtn);
            div.appendChild(deleteBtn);
            labelsArea.appendChild(div);
        });
        
        datasetJSON.value = JSON.stringify(labels, null, 2);
        feather.replace();
    }

    // === Data Management ===
    document.getElementById('saveBtn').addEventListener('click', () => {
        if (labels.length === 0) return alert('No data to save');
        
        const blob = new Blob([JSON.stringify(labels)], { type: 'application/json' });
        const url = URL.createObjectURL(blob);
        
        const a = document.createElement('a');
        a.href = url;
        a.download = 'handspeak_dataset.json';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
        
        setStatus('Dataset saved');
    });

    document.getElementById('loadBtn').addEventListener('click', () => {
        document.getElementById('fileInput').click();
    });

    document.getElementById('fileInput').addEventListener('change', (e) => {
        const file = e.target.files[0];
        if (!file) return;
        
        const reader = new FileReader();
        reader.onload = () => {
            try {
                const data = JSON.parse(reader.result);
                if (!Array.isArray(data)) throw new Error('Invalid format');
                
                labels = data.map(l => ({ 
                    name: l.name, 
                    examples: l.examples || [] 
                }));
                
                renderLabels();
                setStatus('Dataset loaded successfully');
            } catch (err) {
                alert('Error loading file: ' + err.message);
            }
        };
        reader.readAsText(file);
    });

    // === Recording & Prediction ===
    document.getElementById('toggleRec').addEventListener('click', () => {
        recording = !recording;
        
        if (recording && !recordingLabel) {
            alert('Please select a label first');
            recording = false;
            return;
        }
        
        setStatus(recording ? `Recording examples for "${recordingLabel}"` : 'Ready');
        document.getElementById('toggleRec').textContent = recording ? 
            'Stop Recording' : 'Start Recording Examples';
            
        document.getElementById('toggleRec').className = recording ? 
            'w-full bg-red-600 hover:bg-red-500 text-white px-4 py-3 rounded-lg font-medium transition' :
            'w-full bg-blue-600 hover:bg-blue-500 text-white px-4 py-3 rounded-lg font-medium transition';
    });

    document.getElementById('predictToggle').addEventListener('click', () => {
        predicting = !predicting;
        
        if (predicting && labels.length === 0) {
            alert('Please add some labels and examples first');
            predicting = false;
            return;
        }
        
        setStatus(predicting ? 'Predicting...' : 'Ready');
        document.getElementById('predictToggle').textContent = predicting ? 
            'Stop Predicting' : 'Start Predicting';
            
        document.getElementById('predictToggle').className = predicting ? 
            'w-full bg-purple-700 hover:bg-purple-600 text-white px-4 py-3 rounded-lg font-medium transition' :
            'w-full bg-purple-600 hover:bg-purple-500 text-white px-4 py-3 rounded-lg font-medium transition';
    });

    // === MediaPipe Hands Setup ===
    const hands = new Hands({ 
        locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}` 
    });
    
    hands.setOptions({
        maxNumHands: 1,
        modelComplexity: 1,
        minDetectionConfidence: 0.7,
        minTrackingConfidence: 0.6
    });
    
    hands.onResults(onResults);
    
    const camera = new Camera(videoElem, {
        onFrame: async () => {
            await hands.send({ image: videoElem });
        },
        width: 640,
        height: 480
    });
    
    camera.start();
    
    // Results Handler
    function onResults(results) {
        ctx.save();
        ctx.clearRect(0, 0, overlay.width, overlay.height);
        ctx.drawImage(results.image, 0, 0, overlay.width, overlay.height);
        
        if (results.multiHandLandmarks) {
            for (const landmarks of results.multiHandLandmarks) {
                drawConnectors(ctx, landmarks, HAND_CONNECTIONS, {
                    color: '#10B981',
                    lineWidth: 4
                });
                
                drawLandmarks(ctx, landmarks, {
                    color: '#F59E0B',
                    lineWidth: 2,
                    radius: (data) => {
                        return lerp(data.from.z, -0.15, 0.1, 5, 1);
                    }
                });
            }
        }
        
        ctx.restore();
        
        // Process landmarks for recording/prediction