-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgesture-control.html
More file actions
396 lines (330 loc) · 12.6 KB
/
gesture-control.html
File metadata and controls
396 lines (330 loc) · 12.6 KB
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
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
<!DOCTYPE html>
<html>
<head>
<title>Gesture Mouse Control</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
min-height: 100vh;
}
.container {
max-width: 800px;
margin: 0 auto;
text-align: center;
}
h1 {
font-size: 2.5em;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.controls {
margin: 30px 0;
}
button {
padding: 15px 30px;
font-size: 18px;
margin: 10px;
border: none;
border-radius: 10px;
cursor: pointer;
font-weight: bold;
transition: all 0.3s ease;
}
.start-btn {
background: #4CAF50;
color: white;
}
.start-btn:hover { background: #45a049; }
.start-btn:disabled { background: #cccccc; cursor: not-allowed; }
.stop-btn {
background: #f44336;
color: white;
}
.stop-btn:hover { background: #da190b; }
.stop-btn:disabled { background: #cccccc; cursor: not-allowed; }
.video-container {
position: relative;
margin: 20px auto;
width: 400px;
height: 300px;
border: 3px solid #fff;
border-radius: 10px;
overflow: hidden;
background: #000;
}
#video {
width: 100%;
height: 100%;
object-fit: cover;
}
.status {
padding: 15px;
margin: 20px 0;
border-radius: 10px;
font-weight: bold;
font-size: 16px;
background: rgba(255,255,255,0.1);
backdrop-filter: blur(10px);
}
.status.active {
background: rgba(76, 175, 80, 0.3);
border: 2px solid #4CAF50;
}
.status.error {
background: rgba(244, 67, 54, 0.3);
border: 2px solid #f44336;
}
.instructions {
background: rgba(255,255,255,0.1);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
backdrop-filter: blur(10px);
}
.instructions h3 {
margin-top: 0;
color: #ffd700;
}
.instructions ul {
text-align: left;
max-width: 400px;
margin: 0 auto;
}
.instructions li {
margin: 10px 0;
line-height: 1.5;
}
.sensitivity-control {
margin: 20px 0;
}
.sensitivity-control label {
display: block;
margin-bottom: 10px;
font-weight: bold;
}
input[type="range"] {
width: 200px;
margin: 10px;
}
.sensitivity-value {
font-weight: bold;
color: #ffd700;
}
</style>
</head>
<body>
<div class="container">
<h1>🎯 Gesture Mouse Control</h1>
<div class="controls">
<button id="startBtn" class="start-btn">Start Gesture Control</button>
<button id="stopBtn" class="stop-btn" disabled>Stop Gesture Control</button>
</div>
<div class="sensitivity-control">
<label for="sensitivity">Sensitivity:</label>
<input type="range" id="sensitivity" min="1" max="10" value="5">
<span class="sensitivity-value" id="sensitivityValue">5</span>
</div>
<div id="status" class="status">Ready to start</div>
<div class="video-container">
<video id="video" autoplay muted playsinline></video>
</div>
<div class="instructions">
<h3>How to use:</h3>
<ul>
<li>🖐️ <strong>Move your hand</strong> to control the mouse cursor</li>
<li>👌 <strong>Pinch your thumb and index finger</strong> to click</li>
<li>🎯 <strong>Point with your index finger</strong> for precise control</li>
<li>⚙️ <strong>Adjust sensitivity</strong> if movement is too fast or slow</li>
</ul>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/control_utils/control_utils.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js"></script>
<script>
let camera = null;
let hands = null;
let isActive = false;
let sensitivity = 5;
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const video = document.getElementById('video');
const status = document.getElementById('status');
const sensitivitySlider = document.getElementById('sensitivity');
const sensitivityValue = document.getElementById('sensitivityValue');
// Update sensitivity display
sensitivitySlider.addEventListener('input', function() {
sensitivity = parseInt(this.value);
sensitivityValue.textContent = sensitivity;
});
// Start gesture control
startBtn.addEventListener('click', async () => {
try {
startBtn.disabled = true;
startBtn.textContent = 'Starting...';
updateStatus('Requesting camera access...', '');
// Request camera
const stream = await navigator.mediaDevices.getUserMedia({
video: {
width: { ideal: 640 },
height: { ideal: 480 }
}
});
video.srcObject = stream;
updateStatus('Camera access granted! Setting up gesture detection...', '');
// Setup MediaPipe
await setupMediaPipe();
// Start processing
camera = new Camera(video, {
onFrame: async () => {
if (hands) {
await hands.send({ image: video });
}
},
width: 640,
height: 480
});
await camera.start();
isActive = true;
startBtn.disabled = true;
stopBtn.disabled = false;
updateStatus('Gesture control active! Move your hand to control the mouse.', 'active');
} catch (error) {
console.error('Failed to start:', error);
updateStatus(`Error: ${error.message}`, 'error');
startBtn.disabled = false;
startBtn.textContent = 'Start Gesture Control';
}
});
// Stop gesture control
stopBtn.addEventListener('click', () => {
stopCamera();
updateStatus('Gesture control stopped', '');
startBtn.disabled = false;
startBtn.textContent = 'Start Gesture Control';
stopBtn.disabled = true;
});
async function setupMediaPipe() {
console.log('Setting up MediaPipe...');
hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}
});
hands.setOptions({
maxNumHands: 1,
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
hands.onResults(onResults);
console.log('MediaPipe setup complete');
// Test MediaPipe with a simple image
console.log('Testing MediaPipe with blank image...');
const canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 480;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, 640, 480);
hands.send({ image: canvas }).then(() => {
console.log('✅ MediaPipe test completed successfully');
}).catch(error => {
console.error('❌ MediaPipe test failed:', error);
});
}
function onResults(results) {
console.log('🎯 onResults called!', results);
if (!results.multiHandLandmarks || results.multiHandLandmarks.length === 0) {
console.log('No hand detected');
return;
}
const landmarks = results.multiHandLandmarks[0];
if (!landmarks) {
console.log('No landmarks found');
return;
}
console.log('Hand detected! Processing landmarks...');
// Get hand position (index finger tip)
const indexTip = landmarks[8];
const thumbTip = landmarks[4];
// Convert to screen coordinates with sensitivity
const screenX = indexTip.x * window.innerWidth * (sensitivity / 5);
const screenY = indexTip.y * window.innerHeight * (sensitivity / 5);
console.log(`Hand position: x=${screenX.toFixed(0)}, y=${screenY.toFixed(0)}`);
// Calculate pinch distance
const pinchDistance = Math.sqrt(
Math.pow(thumbTip.x - indexTip.x, 2) +
Math.pow(thumbTip.y - indexTip.y, 2)
);
const isPinching = pinchDistance < 0.1;
console.log(`Pinch distance: ${pinchDistance.toFixed(3)}, Is pinching: ${isPinching}`);
// Move mouse cursor
moveMouse(screenX, screenY);
// Handle click on pinch
if (isPinching) {
console.log('Pinch detected! Triggering click...');
clickMouse();
}
}
function moveMouse(x, y) {
console.log(`Moving mouse to: x=${x.toFixed(0)}, y=${y.toFixed(0)}`);
const event = new MouseEvent('mousemove', {
view: window,
bubbles: true,
cancelable: true,
clientX: x,
clientY: y
});
document.elementFromPoint(x, y)?.dispatchEvent(event);
}
function clickMouse() {
console.log('Clicking mouse...');
const event = new MouseEvent('mousedown', {
view: window,
bubbles: true,
cancelable: true,
button: 0
});
document.activeElement?.dispatchEvent(event);
setTimeout(() => {
const upEvent = new MouseEvent('mouseup', {
view: window,
bubbles: true,
cancelable: true,
button: 0
});
document.activeElement?.dispatchEvent(upEvent);
console.log('Mouse click completed');
}, 100);
}
function stopCamera() {
if (camera) {
camera.stop();
camera = null;
}
if (hands) {
hands.close();
hands = null;
}
if (video.srcObject) {
video.srcObject.getTracks().forEach(track => track.stop());
video.srcObject = null;
}
isActive = false;
}
function updateStatus(message, type) {
status.textContent = message;
status.className = `status ${type}`;
console.log('Status:', message);
}
// Cleanup on page unload
window.addEventListener('beforeunload', stopCamera);
</script>
</body>
</html>