-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
572 lines (479 loc) · 23.7 KB
/
main.py
File metadata and controls
572 lines (479 loc) · 23.7 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import sys
import threading
import time
import logging
import cv2
import numpy as np
from pathlib import Path
from typing import Optional, List, Tuple
from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import QTimer, pyqtSlot, QMetaObject, Qt
from src.ui.main_window import MainWindow
from src.ui.overlay import OverlayWidget
from src.core.gesture_tracker import GestureTracker
from src.core.gesture_recognizer import GestureRecognizer, GestureType
from src.input.virtual_mouse import VirtualMouse
from src.input.virtual_keyboard import VirtualKeyboard
from src.ui.virtual_keyboard_widget import VirtualKeyboardWidget
from src.ai.desktop_agent import DesktopAgent
from src.ai.vision_helper import VisionHelper
from src.utils.system_control import SystemController
from src.utils.action_executor import ActionExecutor
from src.core.config import (
OVERLAY_FPS,
CAPTURE_FRAME_WIDTH, CAPTURE_FRAME_HEIGHT, CAPTURE_FPS,
CAPTURE_READ_RETRY_SLEEP, CAPTURE_ERROR_SLEEP, CAPTURE_STOP_SLEEP,
HAND_DUPLICATE_THRESHOLD,
SHORTCUT_COOLDOWN, SHORTCUT_COMBO_WINDOW,
KEYBOARD_TOGGLE_COOLDOWN,
MOUSE_SPEED_MAP, MOUSE_SMOOTHING_MAP,
VOICE_AGENT_SPEAK_DELAY, VOICE_AGENT_SPEAK_MAX_LEN,
logger,
)
VoiceAssistant = None
class GestureOS:
def __init__(self):
self.app = QApplication(sys.argv)
self.main_window = MainWindow()
self.main_window.start_system.connect(self.start)
self.main_window.stop_system.connect(self.stop)
self.main_window.mouse_enabled.stateChanged.connect(self._on_mouse_toggled)
self.main_window.keyboard_enabled.stateChanged.connect(self._on_keyboard_toggled)
self.main_window.voice_enabled.stateChanged.connect(self._on_voice_toggled)
self.main_window.ai_enabled.stateChanged.connect(self._on_ai_toggled)
self.overlay = OverlayWidget()
self.overlay.mouse_mode_changed.connect(self._on_mouse_mode_changed)
self.overlay.keyboard_mode_changed.connect(self._on_keyboard_mode_changed)
self.gesture_tracker = GestureTracker()
self.gesture_recognizer = GestureRecognizer()
self.virtual_mouse = VirtualMouse()
self.virtual_keyboard = VirtualKeyboard()
self.keyboard_widget = VirtualKeyboardWidget()
self.keyboard_widget.closed.connect(self._on_keyboard_widget_closed)
self._voice_assistant_instance = None
self._voice_initialized = False
try:
self.desktop_agent = DesktopAgent()
self.desktop_agent.on_action(self._on_agent_action)
self.desktop_agent.on_state_change(self._on_agent_state_changed)
except Exception as e:
logger.error(f"Desktop agent init error: {e}")
self.desktop_agent = None
self.vision_helper = VisionHelper()
self.system_controller = SystemController()
self.action_executor = ActionExecutor(
log_callback=self.main_window.log_message,
speak_callback=lambda t: self.voice_assistant.speak(t) if self.voice_assistant else None
)
self._is_running = False
self._capture_thread: Optional[threading.Thread] = None
self._keyboard_toggle_cooldown = 0.0
self._agent_mode = False
self._zoom_prev_distance: Optional[float] = None
self._shortcut_last_gesture = ""
self._shortcut_last_time = 0.0
self._shortcut_cooldown = SHORTCUT_COOLDOWN
self._lock = threading.Lock()
self._setup_callbacks()
@property
def voice_assistant(self):
if self._voice_initialized:
return self._voice_assistant_instance
self._voice_initialized = True
try:
from src.input.voice_assistant import VoiceAssistant
self._voice_assistant_instance = VoiceAssistant()
self._voice_assistant_instance.on_command(self._on_voice_command)
self._voice_assistant_instance.on_state_change(self._on_voice_state_changed)
self.main_window.log_message("Asistente de voz cargado")
except Exception as e:
logger.error(f"Voice assistant init failed: {e}")
self._voice_assistant_instance = None
self.main_window.log_message(f"Voz no disponible: {e}")
return self._voice_assistant_instance
def _setup_callbacks(self):
self.virtual_mouse.set_gesture_callback(self._on_mouse_gesture)
self.virtual_keyboard.set_gesture_callback(self._on_keyboard_gesture)
self.main_window.speed_changed.connect(self._on_speed_changed)
def _on_speed_changed(self, level: int):
smoothing = MOUSE_SMOOTHING_MAP.get(level, 0.30)
speed = MOUSE_SPEED_MAP.get(level, 2.2)
self.virtual_mouse._smoothing = smoothing
self.virtual_mouse._speed_mult = speed
bar = "\u2b1b" * level + "\u2b1c" * (5 - level)
self.main_window.log_message(f"Velocidad raton: {bar} ({level}/5)")
def start(self):
if self._is_running:
return
self._is_running = True
self._keyboard_toggle_cooldown = 0.0
self.main_window.log_message("Iniciando GestureOS...")
try:
self.gesture_tracker.release()
except Exception as e:
logger.warning(f"Tracker release warning: {e}")
self.gesture_tracker = GestureTracker()
if self.voice_assistant:
try:
self.voice_assistant.start()
self.main_window.log_message("Asistente de voz iniciado")
except Exception as e:
logger.error(f"Voice start failed: {e}")
self.main_window.log_message(f"Error al iniciar voz: {e}")
if self.desktop_agent:
try:
self.desktop_agent.start()
self.main_window.log_message("Agente IA iniciado")
except Exception as e:
logger.error(f"Agent start failed: {e}")
self.main_window.log_message(f"Error al iniciar IA: {e}")
self._capture_thread = threading.Thread(target=self._capture_loop, daemon=True)
self._capture_thread.start()
self.overlay.show()
self.main_window.show()
self.main_window.log_message("Sistema iniciado correctamente - Usa la palma para mover el mouse!")
def stop(self):
if not self._is_running:
return
self._is_running = False
if self._capture_thread and self._capture_thread.is_alive():
self._capture_thread.join(timeout=2)
if self.voice_assistant:
try:
self.voice_assistant.stop()
except Exception as e:
logger.error(f"Voice stop failed: {e}")
if self.desktop_agent:
try:
self.desktop_agent.stop()
except Exception as e:
logger.error(f"Agent stop failed: {e}")
self.overlay.hide()
self.main_window.log_message("Sistema detenido")
def _capture_loop(self):
cap = cv2.VideoCapture(0)
if not cap.isOpened():
self.main_window.log_message("Error: No se pudo abrir la camara")
return
cap.set(cv2.CAP_PROP_FRAME_WIDTH, CAPTURE_FRAME_WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, CAPTURE_FRAME_HEIGHT)
cap.set(cv2.CAP_PROP_FPS, CAPTURE_FPS)
try:
while self._is_running:
try:
ret, frame = cap.read()
if not ret:
time.sleep(CAPTURE_READ_RETRY_SLEEP)
continue
frame = cv2.flip(frame, 1)
hands = self.gesture_tracker.process_frame(frame)
unique_hands = []
seen_positions = []
for hand in hands:
pos = hand.palm_center
is_duplicate = False
for seen_pos in seen_positions:
if (abs(pos[0] - seen_pos[0]) < HAND_DUPLICATE_THRESHOLD and
abs(pos[1] - seen_pos[1]) < HAND_DUPLICATE_THRESHOLD):
is_duplicate = True
break
if not is_duplicate:
unique_hands.append(hand)
seen_positions.append(pos)
hands = unique_hands
left_hand = None
right_hand = None
for hand in hands:
gesture_state = self.gesture_recognizer.recognize(hand)
gesture_name = gesture_state.gesture.value
handedness = hand.handedness
if handedness == "Left":
left_hand = (hand, gesture_state)
display_hand = "\U0001f590 Mover"
else:
right_hand = (hand, gesture_state)
display_hand = "\U0001f446 Accion"
if left_hand and right_hand:
l_pos = left_hand[0].palm_center
r_pos = right_hand[0].palm_center
self.overlay.update_gesture(f"{display_hand}: {gesture_name}")
self.main_window.update_gesture(f"{display_hand}: {gesture_name}")
if self.virtual_keyboard.is_visible and (left_hand or right_hand):
hand_to_use = left_hand[0] if left_hand else (right_hand[0] if right_hand else None)
gesture_to_use = (left_hand[1].gesture if left_hand else (right_hand[1].gesture if right_hand else None))
if hand_to_use and gesture_to_use:
self.virtual_keyboard.handle_gesture(
gesture_to_use,
hand_to_use.palm_center
)
if self.virtual_mouse.is_enabled and left_hand:
hand, gesture_state = left_hand
self.virtual_mouse.update_move(
hand.palm_center,
gesture_state.gesture,
)
if self.virtual_mouse.is_enabled and right_hand:
hand, gesture_state = right_hand
with self._lock:
old_click_count = self.virtual_mouse._fist_click_count
self.virtual_mouse.update_action(
gesture_state.gesture,
hand.palm_center,
)
with self._lock:
if self.virtual_mouse._fist_click_count != old_click_count:
self.overlay.add_log("Click")
if (self.virtual_mouse.is_enabled and
left_hand and right_hand and len(hands) == 2):
lgs = left_hand[1].gesture.value
rgs = right_hand[1].gesture.value
if lgs == "open_palm" and rgs == "open_palm":
self._zoom_prev_distance = self.virtual_mouse.handle_zoom(
left_hand[0].palm_center,
right_hand[0].palm_center,
self._zoom_prev_distance
)
self.overlay.set_active_mode("zoom")
else:
self._zoom_prev_distance = None
if right_hand:
rgs = right_hand[1].gesture.value
now = time.time()
if now - self._shortcut_last_time > self._shortcut_cooldown:
if (rgs == "fist" and
self._shortcut_last_gesture == "fist" and
now - self._shortcut_last_time < SHORTCUT_COMBO_WINDOW):
import pyautogui as _pag
_pag.hotkey("ctrl", "c")
self.overlay.add_log("Ctrl+C")
self.main_window.log_message("Atajo: Ctrl+C")
self._shortcut_last_time = now
elif (rgs == "thumbs_down" and
self._shortcut_last_gesture == "thumbs_down" and
now - self._shortcut_last_time < SHORTCUT_COMBO_WINDOW):
import pyautogui as _pag
_pag.hotkey("ctrl", "z")
self.overlay.add_log("Ctrl+Z")
self.main_window.log_message("Atajo: Ctrl+Z")
self._shortcut_last_time = now
elif (rgs == "thumbs_up" and
self._shortcut_last_gesture == "thumbs_down" and
now - self._shortcut_last_time < SHORTCUT_COMBO_WINDOW):
import pyautogui as _pag
_pag.hotkey("ctrl", "v")
self.overlay.add_log("Ctrl+V")
self.main_window.log_message("Atajo: Ctrl+V")
self._shortcut_last_time = now
self._shortcut_last_gesture = rgs
dwell_prog = self.virtual_mouse.dwell_progress
self.overlay.set_dwell_progress(dwell_prog)
if dwell_prog > 0.02:
self.overlay.set_active_mode("dwell")
elif self.virtual_mouse.state.value == "dragging":
self.overlay.set_active_mode("drag")
elif self.virtual_mouse.state.value == "scrolling":
self.overlay.set_active_mode("scroll")
elif self.virtual_mouse._dragging:
self.overlay.set_active_mode("select")
else:
if not (left_hand and right_hand and
left_hand[1].gesture.value == "open_palm" and
right_hand[1].gesture.value == "open_palm"):
self.overlay.set_active_mode("")
if len(hands) == 2 and left_hand and right_hand:
left_gesture = left_hand[1].gesture.value
right_gesture = right_hand[1].gesture.value
if (left_gesture == "thumbs_up" and right_gesture == "thumbs_up" and
(time.time() - self._keyboard_toggle_cooldown) > KEYBOARD_TOGGLE_COOLDOWN):
try:
if not self.virtual_keyboard.is_visible:
self.virtual_keyboard.show()
self.overlay.set_keyboard_enabled(True)
QMetaObject.invokeMethod(
self.keyboard_widget,
"show_keyboard",
Qt.ConnectionType.QueuedConnection
)
QMetaObject.invokeMethod(
self.main_window,
"_invoke_keyboard_on",
Qt.ConnectionType.QueuedConnection
)
else:
self.virtual_keyboard.hide()
self.overlay.set_keyboard_enabled(False)
QMetaObject.invokeMethod(
self.keyboard_widget,
"hide_keyboard",
Qt.ConnectionType.QueuedConnection
)
QMetaObject.invokeMethod(
self.main_window,
"_invoke_keyboard_off",
Qt.ConnectionType.QueuedConnection
)
except Exception as e:
logger.error(f"Keyboard toggle failed: {e}")
self._keyboard_toggle_cooldown = time.time()
self.overlay.update_frame(frame)
self.overlay.update_hands(hands)
with self._lock:
self.overlay._fps = self.gesture_tracker.fps
time.sleep(1 / OVERLAY_FPS)
except Exception as e:
logger.error(f"Capture loop error: {e}")
time.sleep(CAPTURE_ERROR_SLEEP)
finally:
cap.release()
def _on_mouse_toggled(self, state):
enabled = bool(state)
self.virtual_mouse.enable() if enabled else self.virtual_mouse.disable()
self.overlay.set_mouse_enabled(enabled)
self.main_window.log_message(f"Mouse {'activado' if enabled else 'desactivado'}")
def _on_keyboard_toggled(self, state):
enabled = bool(state)
if enabled:
self.virtual_keyboard.show()
self.keyboard_widget.show_keyboard()
else:
self.virtual_keyboard.hide()
self.keyboard_widget.hide_keyboard()
self.overlay.set_keyboard_enabled(enabled)
self.main_window.log_message(f"Teclado {'activado' if enabled else 'desactivado'}")
def _on_keyboard_widget_closed(self):
self.virtual_keyboard.hide()
self.overlay.set_keyboard_enabled(False)
self.main_window.set_keyboard_enabled(False)
self.main_window.log_message("Teclado cerrado")
@pyqtSlot()
def _show_keyboard_widget(self):
self.virtual_keyboard.show()
self.keyboard_widget.show_keyboard()
self.overlay.set_keyboard_enabled(True)
self.main_window.set_keyboard_enabled(True)
self.main_window.log_message("Teclado activado (👍👍)")
@pyqtSlot()
def _hide_keyboard_widget(self):
self.virtual_keyboard.hide()
self.keyboard_widget.hide_keyboard()
self.overlay.set_keyboard_enabled(False)
self.main_window.set_keyboard_enabled(False)
self.main_window.log_message("Teclado cerrado (👍👍)")
def _on_voice_toggled(self, state):
enabled = bool(state)
if self.voice_assistant:
if enabled:
try:
self.voice_assistant.start()
self.voice_assistant.set_state(True)
except Exception as e:
logger.error(f"Voice toggle on failed: {e}")
else:
try:
self.voice_assistant.set_state(False)
except Exception as e:
logger.error(f"Voice toggle off failed: {e}")
self.main_window.log_message(f"Voz {'activada' if enabled else 'desactivada'}")
def _on_ai_toggled(self, state):
enabled = bool(state)
if self.desktop_agent:
if enabled:
try:
self.desktop_agent.start()
except Exception as e:
logger.error(f"Agent toggle on failed: {e}")
else:
try:
self.desktop_agent.stop()
except Exception as e:
logger.error(f"Agent toggle off failed: {e}")
self.main_window.log_message(f"IA {'activada' if enabled else 'desactivada'}")
def _on_mouse_mode_changed(self, enabled):
self.main_window.set_mouse_enabled(enabled)
def _on_keyboard_mode_changed(self, enabled):
self.main_window.set_keyboard_enabled(enabled)
def _on_mouse_gesture(self, gesture: str):
self.main_window.log_message(f"Gesto mouse: {gesture}")
def _on_keyboard_gesture(self, key: str):
self.main_window.log_message(f"Tecla presionada: {key}")
def _on_voice_command(self, cmd):
if isinstance(cmd, str):
action, params, text = "ai_agent", {"query": cmd}, cmd
else:
action, params, text = cmd.action, cmd.params, cmd.command
self.main_window.log_message(f"🎤 '{text}' → {action}")
self.overlay.add_log(f"🎤 {text[:22]}")
if self._agent_mode and action not in ("stop_agent_mode",):
if self.desktop_agent and self.main_window.ai_enabled.isChecked():
threading.Thread(
target=self._send_to_agent,
args=(text, None),
daemon=True
).start()
return
if action == "toggle_agent_mode":
self._agent_mode = not self._agent_mode
state = "ACTIVO" if self._agent_mode else "desactivado"
self.main_window.log_message(f"🤖 Modo agente: {state}")
self.overlay.add_log(f"🤖 Modo agente {state}")
if self.voice_assistant:
self.voice_assistant.speak(f"Modo agente {state}")
return
if action == "toggle_keyboard":
is_on = self.virtual_keyboard.is_visible
if is_on:
QTimer.singleShot(0, self._hide_keyboard_widget)
else:
QTimer.singleShot(0, self._show_keyboard_widget)
return
if action in ("ai_agent", "analyze_screen"):
if self.desktop_agent and self.main_window.ai_enabled.isChecked():
images = None
if action == "analyze_screen":
images = [self.action_executor.take_screenshot_b64()]
images = [i for i in images if i]
query = params.get("query", text)
threading.Thread(
target=self._send_to_agent,
args=(query, images or None),
daemon=True
).start()
return
if action == "stop_voice":
if self.voice_assistant:
self.voice_assistant.set_state(False)
return
self.action_executor.execute(action, params)
def _on_voice_state_changed(self, state):
self.main_window.update_status(f"Voz: {state.value}")
def _send_to_agent(self, query: str, images=None):
if not self.desktop_agent:
return
try:
response = self.desktop_agent.ask(query, images=images)
action = response.get("action", "respond")
params = response.get("params", {})
expl = response.get("explanation", "")
text = params.get("text", "")
self.main_window.log_message(f"🤖 IA: {expl}")
self.overlay.add_log(f"🤖 {expl[:22]}")
self.action_executor.execute(action, params)
if text and self.voice_assistant and self.voice_assistant.is_voice_enabled():
short_text = text[:VOICE_AGENT_SPEAK_MAX_LEN] + "..." if len(text) > VOICE_AGENT_SPEAK_MAX_LEN else text
time.sleep(VOICE_AGENT_SPEAK_DELAY)
self.voice_assistant.speak(short_text)
except Exception as e:
logger.error(f"Agent query failed: {e}")
self.main_window.log_message(f"Error agente: {e}")
def _on_agent_action(self, action):
self.action_executor.execute(action.action_type, action.params)
def _on_agent_state_changed(self, state):
self.main_window.update_status(f"IA: {state.value}")
def run(self):
self.main_window.show()
return self.app.exec()
def main():
gestureos = GestureOS()
return gestureos.run()
if __name__ == "__main__":
sys.exit(main())