-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_detection_bot.py
More file actions
1513 lines (1381 loc) · 75.9 KB
/
image_detection_bot.py
File metadata and controls
1513 lines (1381 loc) · 75.9 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 cv2
import numpy as np
import pyautogui
import time
import os
import json
import sys
import math
from typing import Optional, Tuple, List, Dict, Any, Union
import argparse
import logging
from enum import Enum
from dataclasses import dataclass, asdict
import random
from PIL import ImageGrab
try:
from PyQt6.QtGui import QGuiApplication, QImage
except Exception:
QGuiApplication = None
QImage = None
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('bot_debug.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class ActionType(Enum):
CLICK = "click"
MOVE = "move" # Move to absolute coordinates
MOVE_TO = "move_to" # Move to detected template position
RIGHT_CLICK = "right_click"
DOUBLE_CLICK = "double_click"
TYPE = "type"
KEY_PRESS = "key_press"
WAIT = "wait"
SCROLL = "scroll"
CLICK_AND_HOLD = "click_and_hold"
PLAY_RECORDING = "play_recording"
@dataclass
class Action:
type: ActionType
button: str = "left"
clicks: int = 1
text: Optional[str] = None
key: Optional[str] = None
seconds: float = 1.0
pixels: int = 0
x: Optional[int] = None
y: Optional[int] = None
duration: float = 0.0
region: Optional[Tuple[int, int, int, int]] = None
random: bool = False
random_region: Optional[Tuple[int, int, int, int]] = None
if_template: Optional[str] = None
if_confidence: Optional[float] = None
if_region: Optional[Tuple[int, int, int, int]] = None
if_timeout: float = 0.5
else_actions: Optional[List[Any]] = None
if_not_actions: Optional[List[Any]] = None
recording_path: Optional[str] = None
recording_speed: float = 1.0
start_transition: float = 0.0
jitter_px: int = 0
delay_jitter_ms: int = 0
enabled: bool = True
class ImageDetectionBot:
def __init__(self, confidence: float = 0.8):
"""
Initialize the Image Detection Bot.
Args:
confidence: Confidence threshold for template matching (0.0 to 1.0)
"""
self.confidence = confidence
# Optional debug flag to trace conditional branches (else vs if_not)
self.branch_debug: bool = False
self.templates: Dict[str, np.ndarray] = {}
# Cache for feature-based matching (scale/rotation robust)
self.template_features: Dict[str, Dict[str, Any]] = {}
# ORB detector and BF matcher for fast feature matching
try:
self.orb = cv2.ORB_create(nfeatures=1500)
self.bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
except Exception:
self.orb = None
self.bf = None
# AKAZE detector and matcher for additional scale robustness
try:
self.akaze = cv2.AKAZE_create()
self.bf_akaze = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
except Exception:
self.akaze = None
self.bf_akaze = None
# SIFT detector (requires opencv-contrib) and L2 matcher for strong feature fallback
try:
self.sift = cv2.SIFT_create()
self.bf_sift = cv2.BFMatcher(cv2.NORM_L2, crossCheck=False)
except Exception:
self.sift = None
self.bf_sift = None
self.current_position: Optional[Tuple[int, int]] = None
self.variables: Dict[str, Any] = {}
self.action_metrics: List[Dict[str, Any]] = []
def load_template(self, name: str, image_path: str) -> bool:
"""
Load an image template from file.
Args:
name: Name to reference this template
image_path: Path to the template image file
Returns:
bool: True if template was loaded successfully, False otherwise
"""
logger.info(f"Attempting to load template: name='{name}', path='{image_path}'")
# Check if path exists and is a file
if not os.path.isfile(image_path):
logger.error(f"Template file does not exist or is not a file: {image_path}")
return False
# Check file size to ensure it's not empty
file_size = os.path.getsize(image_path)
if file_size == 0:
logger.error(f"Template file is empty: {image_path}")
return False
logger.info(f"Template file exists, size: {file_size} bytes")
try:
# Try to read the image
template = cv2.imread(image_path, cv2.IMREAD_COLOR)
if template is None:
error_msg = f"OpenCV failed to load the image. The file might be corrupted or in an unsupported format: {image_path}"
logger.error(error_msg)
return False
# Check if image was loaded correctly
if template.size == 0:
error_msg = f"Loaded image is empty: {image_path}"
logger.error(error_msg)
return False
# Store the template
self.templates[name] = template
# Precompute feature descriptors for scale/rotation robust matching, if ORB available
try:
if self.orb is not None:
gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
kps, des = self.orb.detectAndCompute(gray, None)
self.template_features[name] = {
'keypoints': kps,
'descriptors': des,
'shape': gray.shape[::-1] # (w, h)
}
logger.info(f"Computed ORB features for template '{name}': {len(kps)} keypoints")
# Also compute AKAZE descriptors as a fallback
if self.akaze is not None:
gray_a = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
kps_a, des_a = self.akaze.detectAndCompute(gray_a, None)
# Merge into template_features structure
tf = self.template_features.setdefault(name, {})
tf['akaze_keypoints'] = kps_a
tf['akaze_descriptors'] = des_a
tf['shape'] = tf.get('shape', gray_a.shape[::-1])
logger.info(f"Computed AKAZE features for template '{name}': {len(kps_a)} keypoints")
# Compute SIFT descriptors for robust matching
if self.sift is not None:
gray_s = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
kps_s, des_s = self.sift.detectAndCompute(gray_s, None)
tf = self.template_features.setdefault(name, {})
tf['sift_keypoints'] = kps_s
tf['sift_descriptors'] = des_s
tf['shape'] = tf.get('shape', gray_s.shape[::-1])
logger.info(f"Computed SIFT features for template '{name}': {0 if kps_s is None else len(kps_s)} keypoints")
except Exception as e:
logger.warning(f"Feature extraction failed for template '{name}': {e}")
logger.info(f"Successfully loaded template '{name}' from {image_path}, dimensions: {template.shape[1]}x{template.shape[0]}")
return True
except Exception as e:
error_msg = f"Unexpected error loading template '{name}' from {image_path}: {str(e)}"
logger.error(error_msg, exc_info=True)
return False
pass
def find_on_screen(self, template_name: str, region: Optional[Tuple[int, int, int, int]] = None) -> Optional[Tuple[int, int]]:
"""
Find a template on the screen.
Args:
template_name: Name of the loaded template to find
region: Optional (x, y, width, height) region to search in
Returns:
Optional[Tuple[int, int]]: (x, y) coordinates of the center of the found image, or None if not found
"""
logger.debug(f"Searching for template: '{template_name}' in region: {region}")
if template_name not in self.templates:
# Log available templates for debugging
available_templates = list(self.templates.keys())
logger.error(
f"Template '{template_name}' not found. Available templates: {available_templates}"
)
return None
template = self.templates[template_name]
logger.debug(f"Template dimensions: {template.shape[1]}x{template.shape[0]}")
try:
# Take screenshot of the specified region or full screen
screenshot = pyautogui.screenshot(region=region)
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
# Convert both images to grayscale
gray_screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
# Try multiple template matching methods
methods = [
cv2.TM_CCOEFF_NORMED, # Best for most cases
cv2.TM_CCORR_NORMED, # Good for some cases where CCOEFF fails
]
best_match = None
best_confidence = 0
for method in methods:
# Ensure template does not exceed screenshot dimensions
ih, iw = gray_screenshot.shape[:2]
th, tw = gray_template.shape[:2]
tpl_to_use = gray_template
try:
if ih < th or iw < tw:
scale = min(ih / max(th, 1), iw / max(tw, 1))
if scale <= 0:
continue
new_w = max(1, int(tw * scale))
new_h = max(1, int(th * scale))
tpl_to_use = cv2.resize(gray_template, (new_w, new_h), interpolation=cv2.INTER_AREA)
except Exception:
pass
# Perform template matching
result = cv2.matchTemplate(gray_screenshot, tpl_to_use, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# For TM_SQDIFF and TM_SQDIFF_NORMED, the best matches are lower values
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
confidence = 1 - min_val # Convert to similarity score
loc = min_loc
else:
confidence = max_val
loc = max_loc
if confidence > best_confidence:
best_confidence = confidence
best_loc = loc
if best_confidence >= self.confidence:
# Calculate center coordinates
h, w = gray_template.shape[:2]
x = best_loc[0] + w // 2
y = best_loc[1] + h // 2
if region:
x += region[0]
y += region[1]
logger.debug(f"Found '{template_name}' at ({x}, {y}) with confidence {best_confidence:.4f}")
return (x, y)
else:
logger.debug(f"Could not find '{template_name}' (best match confidence: {best_confidence:.4f})")
# Save the screenshot and template for debugging
try:
debug_dir = os.path.join(os.path.dirname(__file__), 'debug')
os.makedirs(debug_dir, exist_ok=True)
timestamp = time.strftime("%Y%m%d_%H%M%S")
cv2.imwrite(os.path.join(debug_dir, f'screenshot_{timestamp}.png'), screenshot)
cv2.imwrite(os.path.join(debug_dir, f'template_{timestamp}.png'), template)
logger.info(f"Saved debug images to {debug_dir}")
except Exception as e:
logger.error(f"Failed to save debug images: {str(e)}")
return None
except Exception as e:
logger.error(f"Error finding template '{template_name}': {str(e)}", exc_info=True)
return None
def find_image(self, template_name: str, region: Optional[Tuple[int, int, int, int]] = None,
timeout: float = 10.0, check_interval: float = 0.5,
confidence: Optional[float] = None,
strategy: Optional[str] = None,
min_inliers: int = 12,
ratio_thresh: float = 0.75,
ransac_thresh: float = 4.0) -> Optional[Tuple[int, int]]:
"""
Find a template image on the screen within a specified region.
Args:
template_name: Name of the template to find
region: Optional (x, y, width, height) region to search in
timeout: Maximum time in seconds to search for the image
check_interval: Time in seconds between search attempts
confidence: Confidence threshold (0.0 to 1.0). If None, uses the bot's default confidence.
Returns:
Tuple of (x, y) coordinates of the center of the found image, or None if not found
"""
if template_name not in self.templates:
logger.error(f"Template '{template_name}' not found")
return None
# Use provided confidence or fall back to instance confidence
conf_threshold = confidence if confidence is not None else self.confidence
start_time = time.time()
template = self.templates[template_name]
use_feature = (strategy == 'feature') and (self.orb is not None) and (template_name in self.template_features)
while (time.time() - start_time) < timeout:
try:
# Take a screenshot of the specified region or full screen, handling multi-monitor coords
def _capture_region_bgr(x: int, y: int, w: int, h: int) -> Optional[np.ndarray]:
# Prefer QScreen if available for robust multi-monitor capture
if QGuiApplication is not None:
try:
screens = QGuiApplication.screens()
# Select screen with largest intersection
best = None; best_area = -1
for sc in screens:
g = sc.geometry()
gx1, gy1, gx2, gy2 = g.x(), g.y(), g.x() + g.width(), g.y() + g.height()
ix1, iy1 = max(x, gx1), max(y, gy1)
ix2, iy2 = min(x + w, gx2), min(y + h, gy2)
if ix2 > ix1 and iy2 > iy1:
area = (ix2 - ix1) * (iy2 - iy1)
if area > best_area:
best_area = area; best = sc
target = best if best is not None else (screens[0] if screens else None)
if target is not None:
g = target.geometry()
local_x = max(0, x - g.x()); local_y = max(0, y - g.y())
grab_w = max(1, min(w, g.width() - local_x)); grab_h = max(1, min(h, g.height() - local_y))
dpr = target.devicePixelRatio()
pm = target.grabWindow(0, int(local_x * dpr), int(local_y * dpr), int(grab_w * dpr), int(grab_h * dpr))
if pm.isNull():
return None
qi = pm.toImage().convertToFormat(QImage.Format.Format_BGR888)
height = qi.height(); width = qi.width(); bpl = qi.bytesPerLine()
size_bytes = qi.sizeInBytes() if hasattr(qi, 'sizeInBytes') else (bpl * height)
bits = qi.bits(); bits.setsize(size_bytes)
buf = np.frombuffer(bits, dtype=np.uint8)
img_row = buf.reshape((height, bpl))
img_row = img_row[:, :width * 3]
arr = img_row.reshape((height, width, 3))
return arr.copy()
except Exception:
pass
# Fallback: PIL/pyautogui based on coords
try:
img = ImageGrab.grab(bbox=(x, y, x + w, y + h))
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
except Exception:
return cv2.cvtColor(np.array(pyautogui.screenshot(region=(x, y, w, h))), cv2.COLOR_RGB2BGR)
def _capture_full_desktop_bgr() -> np.ndarray:
if QGuiApplication is not None:
try:
screens = QGuiApplication.screens()
if screens:
x0 = min(sc.geometry().x() for sc in screens)
y0 = min(sc.geometry().y() for sc in screens)
x1 = max(sc.geometry().x() + sc.geometry().width() for sc in screens)
y1 = max(sc.geometry().y() + sc.geometry().height() for sc in screens)
total_w, total_h = x1 - x0, y1 - y0
canvas = np.zeros((total_h, total_w, 3), dtype=np.uint8)
for sc in screens:
g = sc.geometry()
pm = sc.grabWindow(0)
if pm.isNull():
continue
qi = pm.toImage().convertToFormat(QImage.Format.Format_BGR888)
h = qi.height(); w = qi.width(); bpl = qi.bytesPerLine()
size_bytes = qi.sizeInBytes() if hasattr(qi, 'sizeInBytes') else (bpl * h)
bits = qi.bits(); bits.setsize(size_bytes)
buf = np.frombuffer(bits, dtype=np.uint8)
img_row = buf.reshape((h, bpl))
img_row = img_row[:, :w * 3]
arr = img_row.reshape((h, w, 3))
off_x = g.x() - x0; off_y = g.y() - y0
y_end = min(off_y + h, total_h); x_end = min(off_x + w, total_w)
canvas[off_y:y_end, off_x:x_end] = arr[:y_end - off_y, :x_end - off_x]
return canvas
except Exception:
pass
# Fallback to pyautogui
return cv2.cvtColor(np.array(pyautogui.screenshot()), cv2.COLOR_RGB2BGR)
if region:
rx, ry, rw, rh = region
screenshot_bgr = _capture_region_bgr(rx, ry, rw, rh)
else:
screenshot_bgr = _capture_full_desktop_bgr()
if screenshot_bgr is None or screenshot_bgr.size == 0:
time.sleep(check_interval)
continue
if use_feature:
# Feature-based matching for scale/rotation robustness
try:
gray_frame = cv2.cvtColor(screenshot_bgr, cv2.COLOR_BGR2GRAY)
tpl_feats = self.template_features.get(template_name, {})
# ORB attempt
orb_ok = False
try:
if self.orb is not None:
kps2, des2 = self.orb.detectAndCompute(gray_frame, None)
des1 = tpl_feats.get('descriptors')
kps1 = tpl_feats.get('keypoints')
logger.debug(f"ORB scene kps={0 if kps2 is None else len(kps2)}, tpl kps={0 if kps1 is None else len(kps1)}")
good = []
if des1 is not None and des2 is not None and len(des2) > 0:
matches = self.bf.knnMatch(des1, des2, k=2)
for m, n in matches:
if m.distance < ratio_thresh * n.distance:
good.append(m)
logger.debug(f"ORB good matches={len(good)} (min_inliers={min_inliers})")
if len(good) >= min_inliers:
src_pts = np.float32([kps1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kps2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, ransac_thresh)
matchesMask = mask.ravel().tolist() if mask is not None else []
inliers = int(np.sum(matchesMask)) if matchesMask else 0
if H is not None and inliers >= min_inliers:
w_tpl, h_tpl = tpl_feats.get('shape', (template.shape[1], template.shape[0]))
pts = np.float32([[0, 0], [w_tpl, 0], [w_tpl, h_tpl], [0, h_tpl]]).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, H)
area = cv2.contourArea(dst.astype(np.float32))
tpl_area = float(w_tpl * h_tpl)
area_ratio = area / tpl_area if tpl_area > 0 else 0.0
logger.debug(f"ORB homography area_ratio={area_ratio:.2f}, inliers={inliers}")
if 0.2 <= area_ratio <= 5.0:
center = np.mean(dst.reshape(-1, 2), axis=0)
x, y = int(center[0]), int(center[1])
if region:
x += region[0]
y += region[1]
logger.info(f"Feature match (ORB) '{template_name}' at ({x}, {y}) with {inliers} inliers / {len(good)} good matches")
return (x, y)
else:
logger.debug("ORB homography rejected due to implausible scale")
orb_ok = True
except Exception as e:
logger.debug(f"ORB feature matching error: {e}")
# AKAZE fallback if ORB failed or insufficient
try:
if not orb_ok and self.akaze is not None:
kps2_a, des2_a = self.akaze.detectAndCompute(gray_frame, None)
des1_a = tpl_feats.get('akaze_descriptors')
kps1_a = tpl_feats.get('akaze_keypoints')
logger.debug(f"AKAZE scene kps={0 if kps2_a is None else len(kps2_a)}, tpl kps={0 if kps1_a is None else len(kps1_a)}")
good_a = []
if des1_a is not None and des2_a is not None and len(des2_a) > 0:
matches_a = self.bf_akaze.knnMatch(des1_a, des2_a, k=2)
for m, n in matches_a:
if m.distance < ratio_thresh * n.distance:
good_a.append(m)
logger.debug(f"AKAZE good matches={len(good_a)} (min_inliers={min_inliers})")
if len(good_a) >= min_inliers:
src_pts_a = np.float32([kps1_a[m.queryIdx].pt for m in good_a]).reshape(-1, 1, 2)
dst_pts_a = np.float32([kps2_a[m.trainIdx].pt for m in good_a]).reshape(-1, 1, 2)
H_a, mask_a = cv2.findHomography(src_pts_a, dst_pts_a, cv2.RANSAC, ransac_thresh)
matchesMask_a = mask_a.ravel().tolist() if mask_a is not None else []
inliers_a = int(np.sum(matchesMask_a)) if matchesMask_a else 0
if H_a is not None and inliers_a >= min_inliers:
w_tpl, h_tpl = tpl_feats.get('shape', (template.shape[1], template.shape[0]))
pts = np.float32([[0, 0], [w_tpl, 0], [w_tpl, h_tpl], [0, h_tpl]]).reshape(-1, 1, 2)
dst_a = cv2.perspectiveTransform(pts, H_a)
area_a = cv2.contourArea(dst_a.astype(np.float32))
tpl_area = float(w_tpl * h_tpl)
area_ratio_a = area_a / tpl_area if tpl_area > 0 else 0.0
logger.debug(f"AKAZE homography area_ratio={area_ratio_a:.2f}, inliers={inliers_a}")
if 0.2 <= area_ratio_a <= 5.0:
center_a = np.mean(dst_a.reshape(-1, 2), axis=0)
x, y = int(center_a[0]), int(center_a[1])
if region:
x += region[0]
y += region[1]
logger.info(f"Feature match (AKAZE) '{template_name}' at ({x}, {y}) with {inliers_a} inliers / {len(good_a)} good matches")
return (x, y)
else:
logger.debug("AKAZE homography rejected due to implausible scale")
except Exception as e:
logger.debug(f"AKAZE feature matching error: {e}")
# SIFT fallback
try:
if self.sift is not None:
kps2_s, des2_s = self.sift.detectAndCompute(gray_frame, None)
des1_s = tpl_feats.get('sift_descriptors')
kps1_s = tpl_feats.get('sift_keypoints')
logger.debug(f"SIFT scene kps={0 if kps2_s is None else len(kps2_s)}, tpl kps={0 if kps1_s is None else len(kps1_s)}")
good_s = []
if des1_s is not None and des2_s is not None and len(des2_s) > 0:
matches_s = self.bf_sift.knnMatch(des1_s, des2_s, k=2)
for m, n in matches_s:
if m.distance < ratio_thresh * n.distance:
good_s.append(m)
logger.debug(f"SIFT good matches={len(good_s)} (min_inliers={min_inliers})")
if len(good_s) >= min_inliers:
src_pts_s = np.float32([kps1_s[m.queryIdx].pt for m in good_s]).reshape(-1, 1, 2)
dst_pts_s = np.float32([kps2_s[m.trainIdx].pt for m in good_s]).reshape(-1, 1, 2)
H_s, mask_s = cv2.findHomography(src_pts_s, dst_pts_s, cv2.RANSAC, ransac_thresh)
matchesMask_s = mask_s.ravel().tolist() if mask_s is not None else []
inliers_s = int(np.sum(matchesMask_s)) if matchesMask_s else 0
if H_s is not None and inliers_s >= min_inliers:
w_tpl, h_tpl = tpl_feats.get('shape', (template.shape[1], template.shape[0]))
pts = np.float32([[0, 0], [w_tpl, 0], [w_tpl, h_tpl], [0, h_tpl]]).reshape(-1, 1, 2)
dst_s = cv2.perspectiveTransform(pts, H_s)
area_s = cv2.contourArea(dst_s.astype(np.float32))
tpl_area = float(w_tpl * h_tpl)
area_ratio_s = area_s / tpl_area if tpl_area > 0 else 0.0
logger.debug(f"SIFT homography area_ratio={area_ratio_s:.2f}, inliers={inliers_s}")
if 0.2 <= area_ratio_s <= 5.0:
center_s = np.mean(dst_s.reshape(-1, 2), axis=0)
x, y = int(center_s[0]), int(center_s[1])
if region:
x += region[0]
y += region[1]
logger.info(f"Feature match (SIFT) '{template_name}' at ({x}, {y}) with {inliers_s} inliers / {len(good_s)} good matches")
return (x, y)
else:
logger.debug("SIFT homography rejected due to implausible scale")
except Exception as e:
logger.debug(f"SIFT feature matching error: {e}")
# If both fail, fall back to template match below
except Exception as e:
logger.debug(f"Feature matching error: {e}")
# Multi-scale fallback: try resized template matching when feature matching fails
try:
scales = [0.6, 0.75, 0.9, 1.0, 1.1, 1.25, 1.5]
best_ms_conf = 0.0
best_ms_loc = None
best_ms_size = None
h_tpl, w_tpl = template.shape[:2]
for s in scales:
tw = max(5, int(w_tpl * s))
th = max(5, int(h_tpl * s))
interp = cv2.INTER_AREA if s < 1.0 else cv2.INTER_CUBIC
scaled_tpl = cv2.resize(template, (tw, th), interpolation=interp)
if scaled_tpl is None or scaled_tpl.size == 0:
continue
if screenshot_bgr.shape[0] < th or screenshot_bgr.shape[1] < tw:
continue
res = cv2.matchTemplate(screenshot_bgr, scaled_tpl, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val > best_ms_conf:
best_ms_conf = max_val
best_ms_loc = max_loc
best_ms_size = (tw, th)
if best_ms_conf >= conf_threshold and best_ms_loc and best_ms_size:
tw, th = best_ms_size
x = best_ms_loc[0] + tw // 2
y = best_ms_loc[1] + th // 2
if region:
x += region[0]
y += region[1]
logger.info(f"Multi-scale match '{template_name}' at ({x}, {y}) with confidence {best_ms_conf:.2f}")
return (x, y)
else:
logger.debug(f"Multi-scale fallback did not reach threshold (best={best_ms_conf:.2f}, thr={conf_threshold:.2f})")
except Exception as e:
logger.debug(f"Multi-scale fallback error: {e}")
# Fallback to classic template matching (guarded by dimensions)
try:
ih, iw = screenshot_bgr.shape[:2]
th, tw = template.shape[:2]
tpl_to_use = template
if ih < th or iw < tw:
scale = min(ih / max(th, 1), iw / max(tw, 1))
if scale > 0:
new_w = max(1, int(tw * scale))
new_h = max(1, int(th * scale))
tpl_to_use = cv2.resize(template, (new_w, new_h), interpolation=cv2.INTER_AREA)
else:
# Skip classic fallback if scaling invalid
tpl_to_use = None
if tpl_to_use is None or tpl_to_use.size == 0:
raise ValueError("Template larger than search region; skipping classic fallback")
result = cv2.matchTemplate(screenshot_bgr, tpl_to_use, cv2.TM_CCOEFF_NORMED)
except Exception as e:
logger.debug(f"Classic template match skipped: {e}")
result = None
if result is not None:
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val >= conf_threshold:
h, w = (tpl_to_use.shape[0], tpl_to_use.shape[1])
x = max_loc[0] + w // 2
y = max_loc[1] + h // 2
if region:
x += region[0]
y += region[1]
logger.info(f"Found '{template_name}' at ({x}, {y}) with confidence {max_val:.2f}")
return (x, y)
# Wait before next attempt
time.sleep(check_interval)
except Exception as e:
logger.error(f"Error during image search: {e}")
time.sleep(check_interval)
logger.info(f"Template '{template_name}' not found after {timeout} seconds (confidence threshold: {conf_threshold:.2f})")
return None
def move_to(self, x: int, y: int, duration: float = 0.0) -> None:
"""
Move mouse to the specified coordinates.
Args:
x: Target x-coordinate
y: Target y-coordinate
duration: Time in seconds for the movement. If 0, the movement is instant.
"""
try:
# Get current position for logging
start_x, start_y = pyautogui.position()
# Move the mouse
if duration > 0:
pyautogui.moveTo(x, y, duration=duration, tween=pyautogui.easeInOutQuad)
else:
pyautogui.moveTo(x, y, duration=0.1) # Small duration to ensure smooth movement
# Update current position
self.current_position = (x, y)
# Get final position for verification
final_x, final_y = pyautogui.position()
logger.info(f"Moved from ({start_x}, {start_y}) to ({x}, {y}) (final: {final_x}, {final_y}){f' over {duration:.2f}s' if duration > 0 else ''}")
# If we didn't end up where we expected, log a warning
if abs(final_x - x) > 5 or abs(final_y - y) > 5: # Allow 5px tolerance
logger.warning(f"Mouse did not reach target position. Expected: ({x}, {y}), Actual: ({final_x}, {final_y})")
except Exception as e:
logger.error(f"Error moving to ({x}, {y}): {str(e)}")
# Try to update position even if there was an error
try:
final_pos = pyautogui.position()
self.current_position = final_pos
logger.info(f"Updated current position to {final_pos} after move error")
except Exception as e2:
logger.error(f"Failed to update position after error: {str(e2)}")
def click_at(self, x: int, y: int, button: str = 'left', clicks: int = 1, force_move: bool = False) -> None:
"""
Click at the specified coordinates.
Args:
x: X coordinate
y: Y coordinate
button: Mouse button ('left', 'middle', or 'right')
clicks: Number of clicks
force_move: If True, will move to the target position before clicking
"""
try:
current_x, current_y = pyautogui.position()
# Only move if we're not already at the target position and force_move is True
if (current_x, current_y) != (x, y):
if force_move:
# For random region clicks, we want to move to the position
pyautogui.moveTo(x, y, duration=0.1)
time.sleep(0.1) # Small delay to ensure movement is complete
else:
# For regular clicks, just log the mismatch
logger.warning(f"Mouse not at target position. Current: ({current_x}, {current_y}), Target: ({x}, {y})")
logger.warning("Using current mouse position for click to prevent movement")
x, y = current_x, current_y
# Log the click position
logger.info(f"Clicking at position: ({x}, {y})")
# Perform the click at the current position
for _ in range(clicks):
# Use direct mouse events for more control
pyautogui.mouseDown(button=button)
time.sleep(0.05) # Short delay between down and up
pyautogui.mouseUp(button=button)
# Small delay between multiple clicks
if _ < clicks - 1:
time.sleep(0.1)
# Update current position
self.current_position = (x, y)
logger.info(f"Clicked at ({x}, {y}) with {button} button")
except Exception as e:
logger.error(f"Error clicking at ({x}, {y}): {str(e)}")
raise
def right_click_at(self, x: int, y: int) -> None:
"""Right click at the specified coordinates."""
self.click_at(x, y, button='right')
def double_click_at(self, x: int, y: int) -> None:
"""Double click at the specified coordinates."""
self.click_at(x, y, clicks=2)
def type_text(self, text: str) -> None:
"""Type the specified text."""
try:
pyautogui.write(text)
logger.info(f"Typed: {text}")
except Exception as e:
logger.error(f"Error typing text: {str(e)}")
def press_key(self, key: str) -> None:
"""Press the specified key."""
try:
pyautogui.press(key)
logger.info(f"Pressed key: {key}")
except Exception as e:
logger.error(f"Error pressing key {key}: {str(e)}")
def play_recording(self, recording_path: str, speed: float = 1.0, transition: float = 0.0, jitter_px: int = 0, delay_jitter_ms: int = 0) -> bool:
try:
if not recording_path or not os.path.exists(recording_path):
logger.error(f"Recording file not found: {recording_path}")
return False
with open(recording_path, 'r', encoding='utf-8') as f:
data = json.load(f)
events = list(data.get('events', []) or [])
if not events:
logger.warning("Recording has no events")
return False
first_x = None; first_y = None
for ev0 in events:
try:
if str(ev0.get('type','')) == 'move':
first_x = int(ev0.get('x', 0)); first_y = int(ev0.get('y', 0))
break
except Exception:
pass
start = float(events[0].get('t', 0.0))
try:
if first_x is not None and first_y is not None:
cx, cy = pyautogui.position()
dx = float(first_x - cx); dy = float(first_y - cy)
dist = math.hypot(dx, dy)
sp = float(speed) if speed and float(speed) > 0 else 1.0
td = float(transition) if transition is not None else 0.0
if td and td > 0:
pre_dur = float(td)
else:
pre_dur = max(0.05, min(0.75, (dist / 1000.0) / max(sp, 0.001)))
pyautogui.moveTo(first_x, first_y, duration=pre_dur)
self.current_position = (first_x, first_y)
except Exception:
pass
base = time.perf_counter()
try:
logger.info(f"Playing recording '{recording_path}' at speed {float(speed)}x")
except Exception:
pass
prev_pause = None
try:
prev_pause = getattr(pyautogui, 'PAUSE', None)
pyautogui.PAUSE = 0.0
except Exception:
prev_pause = None
# Use per-interval scheduling to ensure speed scaling applies consistently
prev_t = 0.0
last_tick = base
sp = 1.0
try:
sp = float(speed) if speed and float(speed) > 0 else 1.0
except Exception:
sp = 1.0
try:
for idx, ev in enumerate(events):
try:
t_rel = float(ev.get('t', 0.0)) - start
except Exception:
t_rel = 0.0
# Compute the intended interval since previous event and scale by speed
interval = max(0.0, (t_rel - prev_t) / sp)
if interval > 0.0:
deadline = last_tick + interval
# Sleep until reaching the scaled deadline
while time.perf_counter() < deadline:
time.sleep(0.005)
last_tick = deadline
prev_t = t_rel
# Optional small random delay jitter per event
try:
dj = int(delay_jitter_ms or 0)
if dj > 0:
time.sleep(random.uniform(0.0, float(dj)/1000.0))
except Exception:
pass
typ = str(ev.get('type', ''))
if typ == 'move':
x = int(ev.get('x', 0)); y = int(ev.get('y', 0))
try:
jp = int(jitter_px or 0)
if jp > 0:
x += random.randint(-jp, jp)
y += random.randint(-jp, jp)
except Exception:
pass
pyautogui.moveTo(x, y, duration=0.0)
self.current_position = (x, y)
elif typ in ('mouse_down', 'mouse_up'):
btn = ev.get('button', 'left')
# Mouse down/up occurs at current position (already jittered by move)
if typ == 'mouse_down':
pyautogui.mouseDown(button=btn)
else:
pyautogui.mouseUp(button=btn)
elif typ == 'scroll':
dy = int(ev.get('dy', 0))
pyautogui.scroll(dy)
elif typ in ('key_down', 'key_up'):
k = str(ev.get('key', ''))
try:
if typ == 'key_down':
pyautogui.keyDown(k)
else:
pyautogui.keyUp(k)
except Exception:
try:
pyautogui.press(k)
except Exception:
pass
finally:
try:
if prev_pause is not None:
pyautogui.PAUSE = prev_pause
except Exception:
pass
return True
except Exception as e:
logger.error(f"Failed to play recording: {e}")
return False
def _resolve(self, v: Any) -> Any:
try:
if isinstance(v, str):
if v.startswith('${') and v.endswith('}'):
key = v[2:-1]
return self.variables.get(key, v)
return v
except Exception:
return v
def wait(self, seconds: float) -> None:
"""Wait for the specified number of seconds."""
time.sleep(seconds)
logger.info(f"Waited for {seconds} seconds")
def wait_for_image(self, template_name: str, timeout: float = 10.0, interval: float = 0.5,
confidence: Optional[float] = None) -> Optional[Tuple[int, int]]:
"""
Wait for an image to appear on screen.
Args:
template_name: Name of the template to wait for
timeout: Maximum time to wait in seconds
interval: Time between checks in seconds
confidence: Confidence threshold (0.0 to 1.0). If None, uses the bot's default confidence.
Returns:
Optional[Tuple[int, int]]: Coordinates where the image was found, or None if not found
"""
start_time = time.time()
while time.time() - start_time < timeout:
position = self.find_image(template_name, confidence=confidence)
if position is not None:
return position
time.sleep(interval)
logger.warning(f"Timed out waiting for '{template_name}'")
return None
def execute_action_at_position(self, action: Action, position: Optional[Tuple[int, int]] = None) -> bool:
"""
Execute an action at a specific position.
Args:
action: Action to execute
position: Optional position to execute the action at. If None, uses the bot's current position.
Returns:
bool: True if the action was executed successfully, False otherwise
"""
if hasattr(action, 'enabled') and not action.enabled:
return True
if position is None:
if self.current_position is None:
logger.error("No position provided and no current position set")
return False
position = self.current_position
cond_tpl = getattr(action, 'if_template', None)
cond_pos = None
if cond_tpl:
cond_pos = self.find_image(
template_name=cond_tpl,
region=getattr(action, 'if_region', None),
timeout=max(0.0, float(getattr(action, 'if_timeout', 0.5))),
confidence=getattr(action, 'if_confidence', None)
)
if self.branch_debug:
logger.info(f"Condition check for '{cond_tpl}' => {'FOUND' if cond_pos is not None else 'NOT FOUND'}{f' at {cond_pos}' if cond_pos is not None else ''} (execute_at_position)")
x, y = position
# If this is a click action and a region is specified, pick a random point in the region
if action.type == ActionType.CLICK and hasattr(action, 'region') and action.region:
region = action.region
if isinstance(region, (list, tuple)) and len(region) == 4:
x = random.randint(region[0], region[0] + region[2] - 1)
y = random.randint(region[1], region[1] + region[3] - 1)
try:
self.click_at(x, y, button=action.button, clicks=action.clicks)
return True
except Exception as e:
logger.error(f"Error executing random region click: {str(e)}")
return False
# If this is a move_to action and random is set, move to a random point in the region
if action.type == ActionType.MOVE_TO and getattr(action, 'random', False) and hasattr(action, 'random_region') and action.random_region:
region = action.random_region
if isinstance(region, (list, tuple)) and len(region) == 4:
x = random.randint(region[0], region[0] + region[2] - 1)
y = random.randint(region[1], region[1] + region[3] - 1)
try:
self.move_to(x, y, duration=action.duration)
return True
except Exception as e:
logger.error(f"Error executing random region move_to: {str(e)}")
return False
try:
try:
dj = int(getattr(action, 'delay_jitter_ms', 0) or 0)
if dj > 0:
time.sleep(random.uniform(0.0, max(0.0, float(dj))/1000.0))
except Exception:
pass
try:
jp = int(getattr(action, 'jitter_px', 0) or 0)
if jp > 0:
x += random.randint(-jp, jp)
y += random.randint(-jp, jp)
except Exception:
pass
if action.type == ActionType.CLICK:
self.click_at(x, y, button=self._resolve(getattr(action, 'button', 'left')), clicks=int(self._resolve(getattr(action, 'clicks', 1))))
elif action.type == ActionType.MOVE:
self.move_to(x, y, duration=float(self._resolve(getattr(action, 'duration', 0.0))))
elif action.type == ActionType.MOVE_TO:
self.move_to(x, y, duration=float(self._resolve(getattr(action, 'duration', 0.0))))
elif action.type == ActionType.RIGHT_CLICK:
self.right_click_at(x, y)
elif action.type == ActionType.DOUBLE_CLICK:
self.double_click_at(x, y)
elif action.type == ActionType.TYPE and action.text:
self.type_text(str(self._resolve(getattr(action, 'text', ''))))
elif action.type == ActionType.KEY_PRESS and action.key:
self.press_key(str(self._resolve(getattr(action, 'key', ''))))
elif action.type == ActionType.PLAY_RECORDING:
rp = self._resolve(getattr(action, 'recording_path', None))
if not rp:
logger.error("No recording_path set for PLAY_RECORDING")
return False
speed = float(self._resolve(getattr(action, 'recording_speed', 1.0)))
transition = float(self._resolve(getattr(action, 'start_transition', 0.0)))
jp = int(getattr(action, 'jitter_px', 0) or 0)
dj = int(getattr(action, 'delay_jitter_ms', 0) or 0)
return bool(self.play_recording(str(rp), speed=speed, transition=transition, jitter_px=jp, delay_jitter_ms=dj))
elif action.type == ActionType.WAIT:
self.wait(float(self._resolve(getattr(action, 'seconds', 1.0))))
elif action.type == ActionType.SCROLL:
try:
pyautogui.scroll(int(self._resolve(getattr(action, 'pixels', 0))))
except Exception:
return False