-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcv_ui_integration.py
More file actions
1518 lines (1249 loc) · 52.2 KB
/
cv_ui_integration.py
File metadata and controls
1518 lines (1249 loc) · 52.2 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import logging
import os
import time
import json
import base64
from dataclasses import dataclass
from typing import List, Optional, Tuple, Dict, Any
from PIL import Image
import pyautogui
import numpy as np
import ai_engine
import desktop_state
import local_vlm
try:
import torch
from transformers import AutoModelForCausalLM, AutoProcessor
TRANSFORMERS_AVAILABLE = True
except ImportError:
TRANSFORMERS_AVAILABLE = False
class ScreenHelper:
_instance = None
_dpi_scale = None
_monitor_info = None
@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
def __init__(self):
self._init_dpi()
self._init_monitors()
def _init_dpi(self):
try:
import ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
self._dpi_scale = user32.GetDpiForSystem() / 96.0
logging.info(f"DPI scale detected: {self._dpi_scale:.2f}")
except Exception as e:
logging.warning(f"Could not detect DPI scale: {e}")
self._dpi_scale = 1.0
def _init_monitors(self):
self._monitor_info = {}
try:
import ctypes
from ctypes import wintypes
user32 = ctypes.windll.user32
def callback(hMonitor, hdcMonitor, lParam, dwData):
r = ctypes.Structure
class RECT(ctypes.Structure):
_fields_ = [("left", wintypes.LONG), ("top", wintypes.LONG),
("right", wintypes.LONG), ("bottom", wintypes.LONG)]
class MONITORINFO(ctypes.Structure):
_fields_ = [("cbSize", wintypes.DWORD), ("rcMonitor", RECT),
("rcWork", RECT), ("dwFlags", wintypes.DWORD)]
mi = MONITORINFO()
mi.cbSize = ctypes.sizeof(MONITORINFO)
if user32.GetMonitorInfoW(hMonitor, ctypes.byref(mi)):
is_primary = bool(mi.dwFlags & 1)
self._monitor_info[hMonitor] = {
"rect": (mi.rcMonitor.left, mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top),
"work": (mi.rcWork.left, mi.rcWork.top,
mi.rcWork.right - mi.rcWork.left,
mi.rcWork.bottom - mi.rcWork.top),
"primary": is_primary
}
return 1
MONITORENUMPROC = ctypes.WINFUNCTYPE(ctypes.c_int, wintypes.HMONITOR,
wintypes.HDC, ctypes.POINTER(wintypes.RECT), wintypes.LPARAM)
user32.EnumDisplayMonitors(None, None, MONITORENUMPROC(callback), 0)
except Exception as e:
logging.warning(f"Could not enumerate monitors: {e}")
def get_dpi_scale(self) -> float:
return self._dpi_scale or 1.0
def get_primary_monitor_offset(self) -> Tuple[int, int]:
for hmon, info in self._monitor_info.items():
if info.get("primary"):
return (info["rect"][0], info["rect"][1])
return (0, 0)
def adjust_coords_for_monitor(self, x: int, y: int, window_rect: Tuple[int, int, int, int]) -> Tuple[int, int]:
win_left, win_top, win_width, win_height = window_rect
primary_offset_x, primary_offset_y = self.get_primary_monitor_offset()
if win_left < 0 or win_top < 0:
x = x + win_left
y = y + win_top
if primary_offset_x != 0 or primary_offset_y != 0:
x = x - primary_offset_x
y = y - primary_offset_y
return (int(x), int(y))
def scale_screenshot_for_dpi(self, screenshot_path: str) -> str:
scale = self.get_dpi_scale()
if scale == 1.0:
return screenshot_path
try:
img = Image.open(screenshot_path)
if img.width != int(img.width * scale) or img.height != int(img.height * scale):
new_size = (int(img.width * scale), int(img.height * scale))
img_scaled = img.resize(new_size, Image.LANCZOS)
img_scaled.save(screenshot_path)
logging.info(f"Screenshot scaled from {img.width}x{img.height} to {new_size[0]}x{new_size[1]}")
except Exception as e:
logging.warning(f"Could not scale screenshot for DPI: {e}")
return screenshot_path
def get_monitor_containing_point(self, x: int, y: int) -> Optional[Dict]:
for hmon, info in self._monitor_info.items():
rect = info["rect"]
if rect[0] <= x < rect[0] + rect[2] and rect[1] <= y < rect[1] + rect[3]:
return info
return None
def get_system_locale(self) -> str:
try:
import locale
return locale.getdefaultlocale()[0] or "en_US"
except:
return "en_US"
_screen_helper = ScreenHelper.get_instance()
def get_screen_helper():
return _screen_helper
_LOCALIZED_UI_TERMS = {
"en_US": {},
"de_DE": {
"save": ["Speichern", "Speichern"],
"open": ["Öffnen", "Datei öffnen"],
"close": ["Schließen"],
"cancel": ["Abbrechen"],
"ok": ["OK", "Bestätigen"],
"yes": ["Ja"],
"no": ["Nein"],
"delete": ["Löschen", "Entfernen"],
"edit": ["Bearbeiten"],
"copy": ["Kopieren"],
"paste": ["Einfügen"],
"cut": ["Ausschneiden"],
"new": ["Neu", "Neue"],
"open": ["Öffnen"],
"settings": ["Einstellungen", "Optionen"],
"search": ["Suchen", "Suche"],
"help": ["Hilfe"],
"back": ["Zurück"],
"next": ["Weiter", "Weiter"],
"finish": ["Fertig", "Beenden"],
"apply": ["Übernehmen", "Anwenden"],
"reset": ["Zurücksetzen"],
"refresh": ["Aktualisieren", "Neu laden"],
"logout": ["Abmelden", "Ausloggen"],
"login": ["Anmelden", "Einloggen"],
},
"fr_FR": {
"save": ["Enregistrer", "Sauvegarder"],
"open": ["Ouvrir"],
"close": ["Fermer"],
"cancel": ["Annuler"],
"ok": ["OK", "Confirmer"],
"yes": ["Oui"],
"no": ["Non"],
"delete": ["Supprimer"],
"edit": ["Modifier"],
"copy": ["Copier"],
"paste": ["Coller"],
"cut": ["Couper"],
"new": ["Nouveau", "Nouvelle"],
"settings": ["Paramètres", "Options"],
"search": ["Rechercher", "Recherche"],
"help": ["Aide"],
"back": ["Retour"],
"next": ["Suivant", "Suite"],
"finish": ["Terminer", "Finir"],
"apply": ["Appliquer"],
"reset": ["Réinitialiser"],
"refresh": ["Actualiser", "Recharger"],
},
"es_ES": {
"save": ["Guardar"],
"open": ["Abrir"],
"close": ["Cerrar"],
"cancel": ["Cancelar", "Cancelar"],
"ok": ["Aceptar", "OK"],
"yes": ["Sí"],
"no": ["No"],
"delete": ["Eliminar", "Borrar"],
"edit": ["Editar"],
"copy": ["Copiar"],
"paste": ["Pegar"],
"cut": ["Cortar"],
"new": ["Nuevo", "Nueva"],
"settings": ["Configuración", "Opciones"],
"search": ["Buscar", "Búsqueda"],
"help": ["Ayuda"],
"back": ["Atrás", "Volver"],
"next": ["Siguiente"],
"finish": ["Finalizar", "Terminar"],
"apply": ["Aplicar"],
"reset": ["Restablecer", "Reiniciar"],
"refresh": ["Actualizar", "Recargar"],
},
"ja_JP": {
"save": ["保存"],
"open": ["開く"],
"close": ["閉じる"],
"cancel": ["キャンセル"],
"ok": ["OK", "了解"],
"yes": ["はい"],
"no": ["いいえ"],
"delete": ["削除", "消去"],
"edit": ["編集"],
"copy": ["コピー"],
"paste": ["貼り付け"],
"cut": ["切り取り"],
"new": ["新規"],
"settings": ["設定"],
"search": ["検索"],
"help": ["ヘルプ"],
"back": ["戻る", "後退"],
"next": ["次へ"],
"finish": ["完了", "終了"],
"apply": ["適用"],
"reset": ["リセット", "初期化"],
"refresh": ["更新", "再読み込み"],
},
"zh_CN": {
"save": ["保存", "存储"],
"open": ["打开", "开启"],
"close": ["关闭"],
"cancel": ["取消"],
"ok": ["确定", "好"],
"yes": ["是", "是"],
"no": ["否", "不"],
"delete": ["删除"],
"edit": ["编辑"],
"copy": ["复制"],
"paste": ["粘贴"],
"cut": ["剪切"],
"new": ["新建", "新建"],
"settings": ["设置"],
"search": ["搜索"],
"help": ["帮助"],
"back": ["返回", "后退"],
"next": ["下一步", "继续"],
"finish": ["完成", "结束"],
"apply": ["应用", "Apply"],
"reset": ["重置", ["重置"]],
"refresh": ["刷新", "重新加载"],
},
"ko_KR": {
"save": ["저장"],
"open": ["열기"],
"close": ["닫기"],
"cancel": ["취소"],
"ok": ["확인", "OK"],
"yes": ["예"],
"no": ["아니오"],
"delete": ["삭제"],
"edit": ["편집"],
"copy": ["복사"],
"paste": ["붙여넣기"],
"cut": ["잘라내기"],
"new": ["새로 만들기", "새로운"],
"settings": ["설정"],
"search": ["검색"],
"help": ["도움말"],
"back": ["뒤로", "이전"],
"next": ["다음"],
"finish": ["완료", "마침"],
"apply": ["적용"],
"reset": ["초기화", "재설정"],
"refresh": ["새로고침", "새整理"],
},
}
def get_localized_terms(term: str) -> List[str]:
locale = _screen_helper.get_system_locale()
localized = []
for locale_key, translations in _LOCALIZED_UI_TERMS.items():
if term.lower() in translations:
localized.extend(translations[term.lower()])
if term.lower() not in localized:
localized.append(term.lower())
return list(set(localized))
@dataclass
class DetectedElement:
x: int
y: int
width: int
height: int
label: str
confidence: float
element_type: str = "unknown"
@property
def center_x(self) -> int:
return self.x + self.width // 2
@property
def center_y(self) -> int:
return self.y + self.height // 2
@property
def center(self) -> Tuple[int, int]:
return (self.center_x, self.center_y)
@property
def bbox(self) -> Tuple[int, int, int, int]:
return (self.x, self.y, self.width, self.height)
def contains_point(self, px: int, py: int) -> bool:
return self.x <= px <= self.x + self.width and self.y <= py <= self.y + self.height
class CVUIModel:
def __init__(self, model_name: str = "microsoft/torchvision-models"):
self.model = None
self.processor = None
self.model_name = model_name
self.device = "cpu"
self._is_loaded = False
def load(self) -> bool:
if self._is_loaded:
return True
if not TRANSFORMERS_AVAILABLE:
logging.warning("Transformers not available, using fallback CV")
return False
try:
logging.info(f"Loading CV model: {self.model_name}")
self.model = AutoModelForCausalLM.from_pretrained(self.model_name, trust_remote_code=True)
self.model.to(self.device)
self.model.eval()
self._is_loaded = True
logging.info("CV model loaded successfully")
return True
except Exception as e:
logging.error(f"Failed to load CV model: {e}")
return False
def unload(self):
if self.model:
del self.model
self.model = None
self._is_loaded = False
if torch.cuda.is_available():
torch.cuda.empty_cache()
class UIElementDetector:
def __init__(self):
self.cv_model = None
self._desktop_state = desktop_state.get_desktop_state()
self._screenshot_cache = {}
self._cache_ttl = 2.0
self._last_screenshot_time = 0
self._use_local_cv = False
self._model_path = None
def initialize(self, use_local_cv: bool = True, model_path: Optional[str] = None):
self._model_path = model_path
if use_local_cv and TRANSFORMERS_AVAILABLE:
self.cv_model = CVUIModel(model_path or "microsoft/torchvision-models")
self._use_local_cv = self.cv_model.load()
if not self._use_local_cv:
logging.info("Falling back to hybrid CV+LLM approach")
async def detect_elements(self, screenshot_path: str, prompt: Optional[str] = None) -> List[DetectedElement]:
self._last_screenshot_time = time.time()
if self._use_local_cv and self.cv_model and self.cv_model._is_loaded:
return await self._detect_with_model(screenshot_path, prompt)
return await self._detect_with_hybrid(screenshot_path, prompt)
async def _detect_with_model(self, screenshot_path: str, prompt: Optional[str]) -> List[DetectedElement]:
try:
image = Image.open(screenshot_path).convert("RGB")
if prompt:
inputs = self.cv_model.processor(text=prompt, images=image, return_tensors="pt")
inputs = {k: v.to(self.cv_model.device) for k, v in inputs.items()}
with torch.no_grad():
outputs = self.cv_model.model(**inputs)
elements = self._parse_model_outputs(outputs, image.size)
return elements
return []
except Exception as e:
logging.error(f"Model detection failed: {e}")
return await self._detect_with_hybrid(screenshot_path, prompt)
async def _detect_with_hybrid(self, screenshot_path: str, prompt: Optional[str]) -> List[DetectedElement]:
elements = []
elements.extend(self._detect_buttons_with_opencv(screenshot_path))
elements.extend(self._detect_inputs_with_opencv(screenshot_path))
llm_elements = await self._detect_with_llm_vision(screenshot_path, prompt)
elements.extend(llm_elements)
elements = self._deduplicate_elements(elements)
return elements
def _detect_buttons_with_opencv(self, screenshot_path: str) -> List[DetectedElement]:
elements = []
try:
import cv2
img = cv2.imread(screenshot_path)
if img is None:
return elements
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_bound = np.array([0, 0, 180])
upper_bound = np.array([180, 50, 255])
mask = cv2.inRange(hsv, lower_bound, upper_bound)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if 20 < w < 400 and 10 < h < 100:
elements.append(DetectedElement(
x=x, y=y, width=w, height=h,
label="Button",
confidence=0.6,
element_type="button"
))
except Exception as e:
logging.debug(f"OpenCV button detection error: {e}")
return elements
def _detect_inputs_with_opencv(self, screenshot_path: str) -> List[DetectedElement]:
elements = []
try:
import cv2
img = cv2.imread(screenshot_path)
if img is None:
return elements
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rectangles = []
edges = cv2.Canny(gray, 50, 150)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
aspect_ratio = w / h if h > 0 else 0
if 50 < w < 800 and 15 < h < 60 and 1.5 < aspect_ratio < 20:
rectangles.append((x, y, w, h))
rectangles = self._merge_close_rectangles(rectangles)
for x, y, w, h in rectangles:
elements.append(DetectedElement(
x=x, y=y, width=w, height=h,
label="Input",
confidence=0.5,
element_type="input"
))
except Exception as e:
logging.debug(f"OpenCV input detection error: {e}")
return elements
def _merge_close_rectangles(self, rectangles: List, threshold: int = 10) -> List:
if not rectangles:
return []
merged = True
while merged:
merged = False
new_rects = []
for i, (x1, y1, w1, h1) in enumerate(rectangles):
for j, (x2, y2, w2, h2) in enumerate(rectangles):
if i != j:
if abs(x1 - x2) < threshold and abs(y1 - y2) < threshold:
nx = min(x1, x2)
ny = min(y1, y2)
nw = max(x1 + w1, x2 + w2) - nx
nh = max(y1 + h1, y2 + h2) - ny
rectangles[i] = (nx, ny, nw, nh)
merged = True
break
if (x1, y1, w1, h1) in new_rects:
continue
new_rects.append((x1, y1, w1, h1))
rectangles = new_rects
return rectangles
async def _detect_with_llm_vision(self, screenshot_path: str, prompt: Optional[str]) -> List[DetectedElement]:
elements = []
local_vlm_instance = local_vlm.get_local_vlm()
is_local_available = await local_vlm_instance.check_availability()
if is_local_available:
try:
result = await local_vlm_instance.analyze_image(screenshot_path, prompt)
if result.elements:
for elem_data in result.elements:
elem = DetectedElement(
x=int(elem_data.get("x", 0)),
y=int(elem_data.get("y", 0)),
width=int(elem_data.get("width", 50)),
height=int(elem_data.get("height", 30)),
label=elem_data.get("label", "Unknown"),
confidence=float(elem_data.get("confidence", 0.6)),
element_type=elem_data.get("type", "unknown")
)
elements.append(elem)
logging.info(f"Local VLM detected {len(elements)} elements in {result.latency_ms:.0f}ms")
return elements
except Exception as e:
logging.warning(f"Local VLM failed, falling back to cloud: {e}")
if not prompt:
prompt = """Identify all interactive UI elements in this screenshot.
For each element provide:
- type: button, input, menu, link, checkbox, or other
- label: the visible text or description
- x, y: top-left corner coordinates
- width, height: dimensions in pixels
Return ONLY a JSON array like:
[{"type": "button", "label": "Save", "x": 100, "y": 200, "width": 80, "height": 30}]
Do not add any explanation, just the JSON array."""
try:
result = await ai_engine.analyze_image(screenshot_path, prompt)
elements = self._parse_llm_response(result)
except Exception as e:
logging.error(f"LLM vision detection failed: {e}")
return elements
if elements:
elements = self._verify_llm_coordinates(elements, screenshot_path)
logging.info(f"LLM vision returned {len(elements)} verified elements")
return elements
def _parse_llm_response(self, response: str) -> List[DetectedElement]:
import re
elements = []
try:
json_str = response.strip()
match = re.search(r'\[.*\]', json_str, re.DOTALL)
if match:
json_str = match.group(0)
data = json.loads(json_str)
for item in data:
elem = DetectedElement(
x=int(item.get("x", 0)),
y=int(item.get("y", 0)),
width=int(item.get("width", 50)),
height=int(item.get("height", 30)),
label=item.get("label", "Unknown"),
confidence=float(item.get("confidence", 0.5)),
element_type=item.get("type", "unknown")
)
elements.append(elem)
except (json.JSONDecodeError, ValueError) as e:
logging.debug(f"Failed to parse LLM response: {e}")
return elements
def _verify_llm_coordinates(self, elements: List[DetectedElement], screenshot_path: str) -> List[DetectedElement]:
verified = []
for elem in elements:
if self._is_coord_in_any_element_bounds(elem):
elem.confidence = min(elem.confidence + 0.3, 1.0)
verified.append(elem)
else:
logging.warning(f"LLM hallucinated coords for '{elem.label}': ({elem.x}, {elem.y}) - verifying against actual elements")
elem.confidence = max(elem.confidence - 0.4, 0.0)
verified.append(elem)
return [e for e in verified if e.confidence > 0.3]
def _is_coord_in_any_element_bounds(self, llm_elem: DetectedElement) -> bool:
try:
import desktop_state
ds = desktop_state.get_desktop_state()
ds.update(force=True)
if not ds.active_window:
return False
all_elems = ds._flatten_elements(ds.active_window.elements)
for ui_elem in all_elems:
if (ui_elem.x <= llm_elem.x <= ui_elem.x + ui_elem.width and
ui_elem.y <= llm_elem.y <= ui_elem.y + ui_elem.height):
return True
return False
except Exception:
return False
def _deduplicate_elements(self, elements: List[DetectedElement], iou_threshold: float = 0.5) -> List[DetectedElement]:
if not elements:
return []
filtered = []
for elem in elements:
is_duplicate = False
for existing in filtered:
if self._compute_iou(elem, existing) > iou_threshold:
if elem.confidence > existing.confidence:
filtered.remove(existing)
is_duplicate = False
break
else:
is_duplicate = True
break
if not is_duplicate:
filtered.append(elem)
return filtered
def _compute_iou(self, elem1: DetectedElement, elem2: DetectedElement) -> float:
x1 = max(elem1.x, elem2.x)
y1 = max(elem1.y, elem2.y)
x2 = min(elem1.x + elem1.width, elem2.x + elem2.width)
y2 = min(elem1.y + elem1.height, elem2.y + elem2.height)
if x2 <= x1 or y2 <= y1:
return 0.0
intersection = (x2 - x1) * (y2 - y1)
area1 = elem1.width * elem1.height
area2 = elem2.width * elem2.height
union = area1 + area2 - intersection
return intersection / union if union > 0 else 0.0
class KeyboardShortcuts:
COMMON_SHORTCUTS = {
"copy": ["ctrl", "c"],
"paste": ["ctrl", "v"],
"cut": ["ctrl", "x"],
"select_all": ["ctrl", "a"],
"undo": ["ctrl", "z"],
"redo": ["ctrl", "y"],
"save": ["ctrl", "s"],
"open": ["ctrl", "o"],
"close": ["alt", "f4"],
"tab_next": ["ctrl", "tab"],
"tab_prev": ["ctrl", "shift", "tab"],
"refresh": ["f5"],
"find": ["ctrl", "f"],
"new_window": ["ctrl", "n"],
"new_tab": ["ctrl", "t"],
"close_tab": ["ctrl", "w"],
"quit": ["ctrl", "q"],
"escape": ["esc"],
"enter": ["enter"],
"delete": ["delete"],
"backspace": ["backspace"],
"home": ["home"],
"end": ["end"],
"page_up": ["pageup"],
"page_down": ["pagedown"],
"up": ["up"],
"down": ["down"],
"left": ["left"],
"right": ["right"],
"switch_app": ["alt", "tab"],
"force_close": ["ctrl", "shift", "escape"],
}
def __init__(self):
self._held_keys = set()
def parse_shortcut(self, shortcut_name: str) -> List[str]:
shortcut_name = shortcut_name.lower().strip()
if shortcut_name in self.COMMON_SHORTCUTS:
return self.COMMON_SHORTCUTS[shortcut_name]
return self._parse_key_combination(shortcut_name)
def _parse_key_combination(self, combo: str) -> List[str]:
keys = []
parts = combo.lower().replace("+", " ").split()
key_map = {
"ctrl": "ctrl", "control": "ctrl",
"alt": "alt", "option": "alt",
"shift": "shift",
"cmd": "cmd", "command": "cmd", "win": "cmd", "windows": "cmd",
"tab": "tab",
"enter": "enter", "return": "enter",
"escape": "esc", "esc": "esc",
"delete": "delete", "del": "delete",
"backspace": "backspace",
"up": "up", "down": "down", "left": "left", "right": "right",
"home": "home", "end": "end",
"pageup": "pageup", "pagedown": "pagedown",
"f1": "f1", "f2": "f2", "f3": "f3", "f4": "f4",
"f5": "f5", "f6": "f6", "f7": "f7", "f8": "f8",
"f9": "f9", "f10": "f10", "f11": "f11", "f12": "f12",
}
for part in parts:
if part in key_map:
keys.append(key_map[part])
elif len(part) == 1:
keys.append(part)
return keys
def press(self, shortcut_name: str) -> bool:
try:
keys = self.parse_shortcut(shortcut_name)
if not keys:
return False
for key in keys[:-1]:
pyautogui.keyDown(key)
self._held_keys.add(key)
pyautogui.press(keys[-1])
for key in reversed(keys[:-1]):
pyautogui.keyUp(key)
self._held_keys.discard(key)
return True
except Exception as e:
logging.error(f"Keyboard shortcut '{shortcut_name}' failed: {e}")
self._release_all()
return False
def hold(self, key: str) -> bool:
try:
parsed = self._parse_key_combination(key)
if parsed:
for k in parsed:
pyautogui.keyDown(k)
self._held_keys.add(k)
return True
except Exception as e:
logging.error(f"Hold key '{key}' failed: {e}")
return False
def release(self, key: str) -> bool:
try:
parsed = self._parse_key_combination(key)
if parsed:
for k in parsed:
pyautogui.keyUp(k)
self._held_keys.discard(k)
return True
except Exception as e:
logging.error(f"Release key '{key}' failed: {e}")
return False
def release_all(self):
for key in list(self._held_keys):
try:
pyautogui.keyUp(key)
except:
pass
self._held_keys.clear()
def type_text(self, text: str, delay: float = 0.05) -> bool:
try:
pyautogui.write(text, interval=delay)
return True
except Exception as e:
logging.error(f"Type text failed: {e}")
return False
async def type_text_async(self, text: str, delay: float = 0.05) -> bool:
return await asyncio.to_thread(self.type_text, text, delay)
def shortcut_to_string(self, shortcut_name: str) -> str:
keys = self.parse_shortcut(shortcut_name)
return " + ".join(k.upper() for k in keys)
class RobustClicker:
def __init__(self):
self.detector = UIElementDetector()
self._desktop_state = desktop_state.get_desktop_state()
self._max_retries = 3
self._retry_delay = 0.5
self._keyboard = KeyboardShortcuts()
self._circuit_breaker_max_iterations = 10
self._circuit_breaker_iterations = 0
self._total_time_budget = 30.0
self._start_time = None
def initialize(self, use_local_cv: bool = True):
self.detector.initialize(use_local_cv=use_local_cv)
def _check_circuit_breaker(self) -> bool:
if self._circuit_breaker_iterations >= self._circuit_breaker_max_iterations:
logging.warning(f"Circuit breaker triggered: exceeded {self._circuit_breaker_max_iterations} iterations")
return False
if self._start_time and (time.time() - self._start_time) > self._total_time_budget:
logging.warning(f"Circuit breaker triggered: exceeded {self._total_time_budget}s time budget")
return False
return True
def _get_retry_delay(self, attempt: int) -> float:
base_delay = self._retry_delay
exponential_delay = base_delay * (2 ** attempt)
max_delay = 5.0
jitter = 0.1 * base_delay * (hash(str(attempt)) % 10)
return min(exponential_delay + jitter, max_delay)
async def find_and_click(self, description: str, screenshot_path: Optional[str] = None) -> Dict[str, Any]:
if self._start_time is None:
self._start_time = time.time()
if not screenshot_path:
screenshot_path = self._take_screenshot()
result = {
"success": False,
"description": description,
"attempts": 0,
"elements_found": [],
"final_position": None,
"error": None
}
for attempt in range(self._max_retries):
if not self._check_circuit_breaker():
result["error"] = "Circuit breaker triggered - too many attempts"
return result
result["attempts"] = attempt + 1
self._circuit_breaker_iterations += 1
screenshot = screenshot_path if attempt == 0 else self._take_screenshot()
elements = await self.detector.detect_elements(
screenshot,
f"Find '{description}' in this UI"
)
result["elements_found"] = [e.label for e in elements]
matched = self._match_element(elements, description)
if matched:
result["final_position"] = matched.center
click_success = await self._execute_click_with_verification(matched)
result["success"] = click_success
if click_success:
self._reset_circuit_breaker()
return result
retry_delay = self._get_retry_delay(attempt)
logging.info(f"Attempt {attempt + 1} failed for '{description}', waiting {retry_delay:.2f}s before retry")
await asyncio.sleep(retry_delay)
result["error"] = f"Failed after {self._max_retries} attempts - circuit breaker iterations: {self._circuit_breaker_iterations}"
return result
def _reset_circuit_breaker(self):
self._circuit_breaker_iterations = 0
self._start_time = None
async def find_and_drag(
self,
source_description: str,
target_description: str,
screenshot_path: Optional[str] = None
) -> Dict[str, Any]:
if not screenshot_path:
screenshot_path = self._take_screenshot()
result = {
"success": False,
"source": source_description,
"target": target_description,
"attempts": 0,
"source_pos": None,
"target_pos": None,
"error": None
}
for attempt in range(self._max_retries):
if not self._check_circuit_breaker():
result["error"] = "Circuit breaker triggered - too many attempts"
return result
result["attempts"] = attempt + 1
self._circuit_breaker_iterations += 1
screenshot = screenshot_path if attempt == 0 else self._take_screenshot()
elements = await self.detector.detect_elements(screenshot)
source = self._match_element(elements, source_description)
target = self._match_element(elements, target_description)
if source and target:
result["source_pos"] = source.center
result["target_pos"] = target.center
drag_success = await self._execute_drag_with_verification(source, target)
result["success"] = drag_success
if drag_success:
self._reset_circuit_breaker()
return result
retry_delay = self._get_retry_delay(attempt)
await asyncio.sleep(retry_delay)
result["error"] = f"Failed after {self._max_retries} attempts - circuit breaker iterations: {self._circuit_breaker_iterations}"
return result
async def _execute_drag_with_verification(
self,
source: DetectedElement,
target: DetectedElement
) -> bool:
start_x, start_y = source.center
end_x, end_y = target.center
self._desktop_state.update(force=True)
pre_state = self._desktop_state.get_state_summary()
pyautogui.moveTo(start_x, start_y)
await asyncio.sleep(0.1)
pyautogui.drag(
end_x - start_x,
end_y - start_y,
duration=0.5,
button='left'
)
await asyncio.sleep(0.3)
self._desktop_state.update(force=True)
post_state = self._desktop_state.get_state_summary()
return pre_state != post_state
async def wait_for_element(
self,
description: str,
timeout: float = 10.0,
poll_interval: float = 0.5
) -> Dict[str, Any]:
result = {
"found": False,
"description": description,
"element": None,
"wait_time": 0.0,
"error": None
}
start_time = time.time()
while time.time() - start_time < timeout:
screenshot = self._take_screenshot()
elements = await self.detector.detect_elements(screenshot)
matched = self._match_element(elements, description)