-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
1075 lines (974 loc) · 43.1 KB
/
__init__.py
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 os
import pathlib
import random
import re
import shutil
import webbrowser
from multiprocessing import Pool
from tempfile import TemporaryDirectory
from threading import Lock
from time import sleep, strftime
from typing import Union
import keyboard
import kthread
import numpy as np
import regex
import requests
import ujson
from a_pandas_ex_less_memory_more_speed import pd_add_less_memory_more_speed
from skimage.feature import match_template
import cv2
import pandas as pd
import glob
from flatten_everything import flatten_everything
from a_cv2_imshow_thread import add_imshow_thread_to_cv2
from windows_adb_screen_capture import ScreenShots
from collections import defaultdict
nested_dict = lambda: defaultdict(nested_dict)
from a_pandas_ex_plode_tool import pd_add_explode_tools
from a_pandas_ex_intersection_difference import pd_add_set
add_imshow_thread_to_cv2()
pd_add_set()
pd_add_explode_tools()
pd_add_less_memory_more_speed()
def use_black_and_white_pictures(img):
if img.shape[-1] == 4:
return cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def intersects(box1, box2):
return not (
box1[2] < box2[0] or box1[0] > box2[2] or box1[1] > box2[3] or box1[3] < box2[1]
)
def open_image_in_cv(image, channels_in_output=None):
if isinstance(image, str):
if os.path.exists(image):
if os.path.isfile(image):
image = cv2.imread(image, cv2.IMREAD_UNCHANGED)
elif re.search(r"^.{1,10}://", str(image)) is not None:
x = requests.get(image).content
image = cv2.imdecode(np.frombuffer(x, np.uint8), cv2.IMREAD_COLOR)
else:
image = cv2.imdecode(np.frombuffer(image, np.uint8), cv2.IMREAD_COLOR)
elif "PIL" in str(type(image)):
image = np.array(image)
else:
if image.dtype != np.uint8:
image = image.astype(np.uint8)
if channels_in_output is not None:
if image.shape[-1] == 4 and channels_in_output == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
elif image.shape[-1] == 3 and channels_in_output == 4:
image = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
else:
pass
return image
def apply_matching_template(haystack: np.ndarray, needle: np.ndarray):
reas = match_template(haystack, needle)
return reas
def cropimage(img, coords):
if sum(coords) == 0:
return img
return img[coords[1] : coords[3], coords[0] : coords[2]].copy()
def _findpics(liste):
key, key2, x0, y0, x1, y1, haystack, needle = liste
allresas = []
try:
allresas = apply_matching_template(haystack, needle)
except Exception:
pass
return [key, key2, x0, y0, x1, y1, haystack, needle, allresas]
def find_pics(liste, workers=1):
groupedresults = []
if workers == 1:
for coi in liste:
groupedresults.append(_findpics(coi))
groupedresults = [groupedresults]
else:
with Pool(workers) as p:
groupedresults.append(p.map(_findpics, liste).copy())
return groupedresults[0]
def resize_image(img, scale_percent=100, interpolation=cv2.INTER_AREA):
width = int(img.shape[1] / 100 * scale_percent)
height = int(img.shape[0] / 100 * scale_percent)
dim = (width, height)
resized = cv2.resize(img, dim, interpolation=interpolation)
return resized
regex_compiled = regex.compile(
r"[\\/]+[^\\/]*--(\d+)x(\d+)--(\d+)x(\d+)", flags=regex.IGNORECASE
)
def get_enumerated_file_path(folder, leading_zeros=8, ending=".png"):
counter = 0
if not os.path.exists(folder):
os.makedirs(folder)
newfilepath = os.path.join(folder, str(counter).zfill(leading_zeros) + ending)
while os.path.exists(newfilepath):
counter += 1
newfilepath = os.path.join(folder, str(counter).zfill(leading_zeros) + ending)
return newfilepath
def start_annotation_tool():
dirname = os.path.join(os.path.abspath(os.path.dirname(__file__)))
annotationtoolfiles = [
x
for x in [
"canvas.min.js",
"filesaver.min.js",
"jszip.min.js",
"ybat.css",
"ybat.html",
"ybat.js",
]
if os.path.exists(os.path.join(dirname, x))
]
if len(annotationtoolfiles) == 6:
main_path_stringuri = pathlib.Path(os.path.join(dirname, "ybat.html")).as_uri()
webbrowser.open(main_path_stringuri)
print(
"The annotation tool requires a class list like:\n\n\nclasses.txt\n\nbutton1\nbutton2\ntextfield1\ntextfield2"
)
print(
"\n\nClick on: Save COCO when you are done and copy the file name of the generated file"
)
else:
print(
f"The annotation tool could not be found! Download it here:\nhttps://github.com/drainingsun/ybat\n\nPut it the folder of this module:\n{dirname}"
)
print(dirname)
def create_needle_images_from_annotations(
cocojson,
savefolder,
outputfolder,
expand_x_negative=200,
expand_x_positive=200,
expand_y_negative=200,
expand_y_positive=200,
):
r"""
create_needle_images_from_annotations(
cocojson=r"C:\Users\Gamer\Documents\Downloads\bboxes_coco (3).zip",
savefolder=r'C:\testannonaton',
outputfolder=r'C:\sadfafdxc22',
expand_x_negative=200,
expand_x_positive=200,
expand_y_negative=200,
expand_y_positive=200,
)
"""
if cocojson.lower().endswith(".zip"):
tempdict_ = TemporaryDirectory()
shutil.unpack_archive(cocojson, tempdict_.name)
cocojson = os.path.join(tempdict_.name, "coco.json")
# r"C:\Users\Gamer\Documents\Downloads\bboxes_coco\coco.json"
if not os.path.exists(outputfolder):
os.makedirs(outputfolder)
outputfolder_cropped = os.path.join(outputfolder, "cropped_view")
if not os.path.exists(outputfolder_cropped):
os.makedirs(outputfolder_cropped)
outputfolder_screen = os.path.join(outputfolder, "screen_view")
if not os.path.exists(outputfolder_screen):
os.makedirs(outputfolder_screen)
with open(cocojson, encoding="utf-8") as f:
jsonfile = ujson.loads(f.read())
df = pd.DataFrame(jsonfile["annotations"])
df = pd.concat(
[
df,
df.bbox.s_explode_lists_and_tuples().rename(
columns={
"bbox_0": "start_x",
"bbox_1": "start_y",
"bbox_2": "needle_width",
"bbox_3": "needle_height",
}
),
],
axis=1,
)
df = df.drop(columns=["start_x", "start_y"])
df = pd.concat(
[
df,
df.segmentation.s_explode_lists_and_tuples().rename(
columns={
"segmentation_0": "x0",
"segmentation_1": "y0",
"segmentation_2": "x1",
"segmentation_3": "y1",
"segmentation_4": "x2",
"segmentation_5": "y2",
"segmentation_6": "x3",
"segmentation_7": "y3",
}
),
],
axis=1,
)
dfkat = pd.DataFrame(jsonfile["categories"])
dfim = pd.DataFrame(jsonfile["images"]).rename(columns={"id": "image_id"})
dfim["file_path"] = dfim.file_name.apply(lambda x: os.path.join(savefolder, x))
dfim["numpy_img"] = dfim.file_path.apply(
lambda x: cv2.imread(x, cv2.IMREAD_UNCHANGED)
)
df = df.d_merge_multiple_dfs_and_series_on_one_column(
[dfkat], "id"
).d_merge_multiple_dfs_and_series_on_one_column([dfim], "image_id")
df["percent_difference_haystack_needle"] = 100 / df.width * df.needle_width
df["needle_start_x"] = df.x0
df["needle_start_y"] = df.y0
df["needle_end_x"] = df.x2
df["needle_end_y"] = df.y2
df["needle_start_searching_x"] = df.x0 - expand_x_negative
df["needle_start_searching_y"] = df.y0 - expand_y_negative
df["needle_end_searching_x"] = df.x2 + expand_x_positive
df["needle_end_searching_y"] = df.y2 + expand_y_positive
columnssearch = [
"needle_start_searching_x",
"needle_start_searching_y",
"needle_end_searching_x",
"needle_end_searching_y",
]
for col in columnssearch:
df.loc[df[col] < 0, col] = 0
if col == "needle_end_searching_x":
df.loc[df[col] > df["width"], col] = df["width"].iloc[0]
if col == "needle_end_searching_y":
df.loc[df[col] > df["height"], col] = df["height"].iloc[0]
for key, item in df.iterrows():
file_ = f'{item["name"]}--{item.needle_start_searching_x}x{item.needle_start_searching_y}--{item.needle_end_searching_x}x{item.needle_end_searching_y}.png'
outputpath = os.path.join(outputfolder, file_)
print(outputpath)
resultpic = cropimage(
item.numpy_img.copy(),
coords=(
item.needle_start_x,
item.needle_start_y,
item.needle_end_x,
item.needle_end_y,
),
)
cv2.imwrite(outputpath, resultpic)
resultpiccropped = cropimage(
item.numpy_img.copy(),
coords=(
item.needle_start_searching_x,
item.needle_start_searching_y,
item.needle_end_searching_x,
item.needle_end_searching_y,
),
)
cv2.imwrite(os.path.join(outputfolder_cropped, file_), resultpiccropped)
cv2.imwrite(os.path.join(outputfolder_screen, file_), item.numpy_img)
return df
def switch_red_blue(img):
if img.shape[1] == 4:
image1 = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
else:
image1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return image1
timest = lambda: strftime("%Y_%m_%d_%H_%M_%S")
class PatternMatchingOnScreen:
def __init__(self, needle_folder=None, scale_percent=50, debug_folder=None):
self.scale_percent = scale_percent
self.scale_percentvice = 100 / scale_percent
self.sc = None # = ScreenShots()
self.monitor = None
self.adb_path = None
self.adb_serial = None
self.hwnd = None
self.needle_images = nested_dict()
self.needle_folder = None
if needle_folder is not None:
self.needle_folder = (
regex.sub(r"[\\/]*\s*$", "", needle_folder) + os.sep + "*.png"
)
self.image_original = None
self.groupedresults = []
self.df = pd.DataFrame()
self.resize_interpolation = cv2.INTER_AREA
self.untouchedimage = None
self.untouchedimage_drawn = None
self.cropped_haystack_images = []
self.all_results_dataframes = []
self.df_filtered_results_dataframes = pd.DataFrame()
self.df = pd.DataFrame()
self.debug_folder = debug_folder
if self.debug_folder is not None:
if not os.path.exists(self.debug_folder):
os.makedirs(self.debug_folder)
self.allres = None
self.lock = Lock()
self.show_results_thread = None
self.show_results_video_thread = None
def reconfigure_scale_size(
self, scale_percent, substract_zoom_percent=10, add_zoom_percent=10
):
self.scale_percent = scale_percent
self.scale_percentvice = 100 / scale_percent
self.get_needle_images(
substract_zoom_percent=substract_zoom_percent,
add_zoom_percent=add_zoom_percent,
)
return self
def get_needle_images(self, substract_zoom_percent=10, add_zoom_percent=10):
needletemp = {
x: {
"original_image": (open_image_in_cv(x, channels_in_output=3)),
"crop_original_image": [
int(__) for __ in flatten_everything([regex_compiled.findall(x)])
],
}
for x in glob.glob(self.needle_folder)
}
counter = 0
for needlet in needletemp.items():
for durchg in range(
int(
self.scale_percent
- self.scale_percent / 100 * substract_zoom_percent
),
int(self.scale_percent + self.scale_percent / 100 * add_zoom_percent),
1,
):
self.needle_images[needlet[0]][durchg]["image"] = resize_image(
needlet[1]["original_image"], scale_percent=durchg
)
self.needle_images[needlet[0]][durchg]["cropped_coords"] = [
int(x / 100 * durchg) for x in needlet[1]["crop_original_image"]
]
self.needle_images[needlet[0]][durchg][
"imagebw"
] = use_black_and_white_pictures(
resize_image(needlet[1]["original_image"], scale_percent=durchg)
)
if self.debug_folder is not None:
cv2.imwrite(
os.path.join(
self.debug_folder, fr"{durchg}{counter}_color.png"
),
self.needle_images[needlet[0]][durchg]["image"],
)
cv2.imwrite(
os.path.join(self.debug_folder, fr"{durchg}{counter}_bw.png"),
self.needle_images[needlet[0]][durchg]["imagebw"],
)
# cropcoords = ([int(x / 100 * durchg) for x in needlet[1]["crop_original_image"]])
cropcoords = [
int(x / self.scale_percentvice)
for x in needlet[1]["crop_original_image"]
]
croppedim = cropimage(self.image_original, cropcoords)
cv2.imwrite(
os.path.join(
self.debug_folder, f"{durchg}{counter}_cropped.png"
),
croppedim,
)
counter += 1
return self
def configure_monitor(self, monitor=1):
self.sc = ScreenShots()
self.monitor = monitor
self.sc.choose_monitor_for_screenshot(monitor)
return self
def configure_adb(
self, adb_path=r"C:\ProgramData\adb\adb.exe", adb_serial="localhost:5555"
):
self.adb_path = adb_path
self.adb_serial = adb_serial
self.sc = ScreenShots(hwnd=None, adb_path=adb_path, adb_serial=adb_serial)
return self
def configure_window(self, regular_expression=None, hwnd=None):
self.sc = ScreenShots(hwnd=hwnd)
if hwnd is None and regular_expression is not None:
self.sc.find_window_with_regex(regular_expression)
self.hwnd = self.sc.hwnd
return self
def get_screenshot(
self, interpolation=cv2.INTER_AREA, grayscale=True, save_in_folder=None
):
if self.adb_path is not None and self.adb_serial is not None:
self.image_original = self.sc.imget_adb().copy()
elif self.hwnd is not None:
self.image_original = self.sc.imget_hwnd().copy()
elif self.monitor is not None:
self.image_original = self.sc.imget_monitor().copy()
self.image_original = open_image_in_cv(
self.image_original, channels_in_output=3
)
self.untouchedimage = self.image_original.copy()
if self.scale_percent != 100:
self.image_original = resize_image(
self.image_original,
scale_percent=self.scale_percent,
interpolation=interpolation,
)
if grayscale:
self.image_original = use_black_and_white_pictures(
open_image_in_cv(self.image_original, channels_in_output=3)
)
if save_in_folder is not None:
untouchedimage = get_enumerated_file_path(
save_in_folder, leading_zeros=8, ending=".png"
)
cv2.imwrite(untouchedimage, self.untouchedimage)
return self
def get_croped_image_coords(self, grayscale=True):
counter = 0
allcheck = []
for key, item in self.needle_images.items():
for key2, item2 in item.items():
cropcoords = item2["cropped_coords"]
croppedim = cropimage(self.image_original, cropcoords)
if self.debug_folder is not None:
cv2.imwrite(
os.path.join(
self.debug_folder, f"{counter}{key2}_cropimage.png"
),
croppedim,
)
counter += 1
if grayscale:
allcheck.append(
(key, key2, *cropcoords, croppedim, (item2["imagebw"]))
)
else:
# allcheck.append((key, key2, *cropcoords, croppedim, (item2["image"])))
allcheck.append(
(key, key2, *cropcoords, croppedim, (item2["image"]),)
)
self.cropped_haystack_images = allcheck.copy()
return self
def template_matching_to_dataframe(self, grayscale=True, workers=3):
allres = find_pics(self.cropped_haystack_images, workers=workers)
self.allres = allres.copy()
if grayscale:
alldataframes = [
(
pd.DataFrame(x[-1]).assign(
aa_filepath=x[0],
aa_zoomfactor=x[1],
aa_crop_x0=x[2],
aa_crop_y0=x[3],
aa_crop_x1=x[4],
aa_crop_y1=x[5],
aa_cropped_haystack_x=x[6].shape[1],
aa_cropped_haystack_y=x[6].shape[0],
aa_cropped_needle_x=x[7].shape[1],
aa_cropped_needle_y=x[7].shape[0],
),
pd.DataFrame(x[-1]),
)
for x in allres
if np.any(x[-1])
]
else:
alldataframes = [
(
pd.DataFrame(np.squeeze(x[-1])).assign(
aa_filepath=x[0],
aa_zoomfactor=x[1],
aa_crop_x0=x[2],
aa_crop_y0=x[3],
aa_crop_x1=x[4],
aa_crop_y1=x[5],
aa_cropped_haystack_x=x[6].shape[1],
aa_cropped_haystack_y=x[6].shape[0],
aa_cropped_needle_x=x[7].shape[1],
aa_cropped_needle_y=x[7].shape[0],
),
pd.DataFrame(np.squeeze(x[-1])),
)
for x in self.allres
if np.any(x[-1])
]
self.all_results_dataframes = alldataframes.copy()
return self
def sort_result_dfs(self):
gefundenebilder = []
for dffull, df in self.all_results_dataframes:
yaxis = df[df.max().sort_values().index[-1]]
ywert = yaxis.max()
y_koordinate = yaxis.name
xaxis = df.loc[
(df[y_koordinate] <= ywert) & (df[y_koordinate] >= ywert - (ywert / 10))
]
x_koordinate = xaxis.index[0]
df.at[x_koordinate, y_koordinate] = -100
x_koordinate, y_koordinate = y_koordinate, x_koordinate
dfnew = (
dffull.loc[xaxis.index][
[
"aa_filepath",
"aa_zoomfactor",
"aa_crop_x0",
"aa_crop_y0",
"aa_crop_x1",
"aa_crop_y1",
"aa_cropped_haystack_x",
"aa_cropped_haystack_y",
"aa_cropped_needle_x",
"aa_cropped_needle_y",
]
].assign(aa_x=x_koordinate, aa_y=y_koordinate, aa_conf=ywert)
).copy()
gefundenebilder.append(dfnew)
self.df_filtered_results_dataframes = pd.concat(
gefundenebilder, ignore_index=True
)
return self
def filter_duplicated_results(self, thresh=0.6):
allresultsx = []
for name, dfx in self.df_filtered_results_dataframes.groupby(["aa_filepath"]):
dfx = dfx.loc[dfx.aa_conf > thresh]
if dfx.empty:
continue
df = (dfx.sort_values(by="aa_conf", ascending=False)).copy()
df["aa_real_x_start"] = df.aa_crop_x0 + df.aa_x
df["aa_real_x_start"] = df["aa_real_x_start"] * self.scale_percentvice
df["aa_real_y_start"] = df.aa_crop_y0 + df.aa_y
df["aa_real_y_start"] = df["aa_real_y_start"] * self.scale_percentvice
df["aa_real_y_start"] = df["aa_real_y_start"].astype(np.uint32)
df["aa_real_x_start"] = df["aa_real_x_start"].astype(np.uint32)
df["aa_width"] = df["aa_cropped_needle_x"] * self.scale_percentvice
df["aa_height"] = +df["aa_cropped_needle_y"] * self.scale_percentvice
df["aa_real_y_end"] = df["aa_real_y_start"] + df["aa_height"]
df["aa_real_x_end"] = df["aa_real_x_start"] + df["aa_width"]
# allresultsx2.append(df.copy())
# allresultsx.append(df.copy())
while not df.empty:
box1 = (
df.aa_real_x_start.iloc[0],
df.aa_real_y_start.iloc[0],
df.aa_real_x_end.iloc[0],
df.aa_real_y_end.iloc[0],
)
df["aa_intersects"] = df.apply(
lambda x: intersects(
box1,
box2=(
x.aa_real_x_start,
x.aa_real_y_start,
x.aa_real_x_end,
x.aa_real_y_end,
),
),
axis=1,
)
allresultsx.append(df[:1].copy())
df = df.loc[df.aa_intersects == False].copy()
try:
df = (
pd.concat(allresultsx)
.reset_index(drop=True)
.sort_values(by="aa_conf", ascending=False)
)
df["aa_pure_filename"] = df.aa_filepath.apply(lambda x: x.split(os.sep)[-1])
self.df = df.copy()
# from a_pandas_ex_less_memory_more_speed import pd_add_less_memory_more_speed
self.df = self.df.reset_index(drop=True)
self.df[
"aa_same_zoom_factor"
] = self.df.aa_zoomfactor.ds_value_counts_to_column()
self.df = self.df.sort_values(
by=["aa_same_zoom_factor", "aa_conf"], ascending=[False, False]
)
self.df = self.df.ds_reduce_memory_size(verbose=False)
except Exception as fe:
print(
f"Error: {fe} if none of the needle images is on the screen, you can ignore it! {timest()}".replace(
"\n", " "
).replace(
"\r", " "
),
end="\r",
)
allcols = [
"aa_filepath",
"aa_zoomfactor",
"aa_crop_x0",
"aa_crop_y0",
"aa_crop_x1",
"aa_crop_y1",
"aa_cropped_haystack_x",
"aa_cropped_haystack_y",
"aa_cropped_needle_x",
"aa_cropped_needle_y",
"aa_x",
"aa_y",
"aa_conf",
"aa_real_x_start",
"aa_real_y_start",
"aa_width",
"aa_height",
"aa_real_y_end",
"aa_real_x_end",
"aa_intersects",
"aa_pure_filename",
"aa_same_zoom_factor",
]
self.df = pd.DataFrame(columns=allcols)
return self
def draw_results(self):
image = self.untouchedimage.copy()
try:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
except Exception:
pass
fontdistance1 = -10
for key, item in self.df.iterrows():
try:
x_1_match0 = int(item.aa_real_x_start)
y_1_match0 = int(item.aa_real_y_start)
height_haystack0 = int(item.aa_real_y_end)
width_haystack0 = int(item.aa_real_x_end)
r_, g_, b_ = (
random.randrange(50, 255),
random.randrange(50, 255),
random.randrange(50, 255),
)
image = cv2.rectangle(
image,
(x_1_match0, y_1_match0),
(width_haystack0, height_haystack0),
color=(0, 0, 0),
thickness=item.aa_same_zoom_factor * 2,
)
image = cv2.rectangle(
image,
(x_1_match0, y_1_match0),
(width_haystack0, height_haystack0),
color=(r_, g_, b_),
thickness=item.aa_same_zoom_factor,
)
image = cv2.putText(
image,
str(item.aa_conf),
(x_1_match0, y_1_match0 - fontdistance1),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(0, 0, 0),
3,
)
image = cv2.putText(
image,
str(item.aa_conf),
(x_1_match0, y_1_match0 - fontdistance1),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(r_, g_, b_),
1,
)
item.aa_pure_filename
image = cv2.putText(
image,
str(item.aa_pure_filename),
(x_1_match0, y_1_match0 - fontdistance1 * 4),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(0, 0, 0),
3,
)
image = cv2.putText(
image,
str(item.aa_pure_filename),
(x_1_match0, y_1_match0 - fontdistance1 * 4),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(r_, g_, b_),
1,
)
except Exception as fe:
self.untouchedimage_drawn = image.copy()
pass
self.untouchedimage_drawn = image.copy()
return image
def get_detection_results_as_df(self):
return self.df
def get_screenshot_and_start_detection(
self,
grayscale=True,
interpolation=cv2.INTER_AREA,
thresh=0.6,
save_screenshot_in_folder=None,
workers=3,
show_results=False,
sleep_time_for_results=0.1,
quit_key_for_results="q",
):
if show_results:
self.lock.acquire()
try:
self.get_screenshot(
interpolation=interpolation,
grayscale=grayscale,
save_in_folder=save_screenshot_in_folder,
)
self.get_croped_image_coords(grayscale=grayscale)
self.template_matching_to_dataframe(grayscale=grayscale, workers=workers)
# alldataframes=self.all_results_dataframes.copy()
self.sort_result_dfs()
self.filter_duplicated_results(thresh=thresh)
except Exception as da:
print(
f"Error: {da} if none of the needle images is on the screen, you can ignore it! {timest()}".replace(
"\n", " "
).replace(
"\r", " "
),
end="\r",
)
allcols = [
"aa_filepath",
"aa_zoomfactor",
"aa_crop_x0",
"aa_crop_y0",
"aa_crop_x1",
"aa_crop_y1",
"aa_cropped_haystack_x",
"aa_cropped_haystack_y",
"aa_cropped_needle_x",
"aa_cropped_needle_y",
"aa_x",
"aa_y",
"aa_conf",
"aa_real_x_start",
"aa_real_y_start",
"aa_width",
"aa_height",
"aa_real_y_end",
"aa_real_x_end",
"aa_intersects",
"aa_pure_filename",
"aa_same_zoom_factor",
]
self.df = pd.DataFrame(columns=allcols)
if show_results:
self.lock.release()
if self.show_results_thread is None:
self.show_results_thread = kthread.KThread(
target=self._show_results,
name="results",
args=(quit_key_for_results, sleep_time_for_results),
)
self.show_results_thread.start()
elif not self.show_results_thread.is_alive():
self.show_results_thread = kthread.KThread(
target=self._show_results,
name="results",
args=(quit_key_for_results, sleep_time_for_results),
)
return self
def save_screenshots_for_creating_needle_images(self, folder, hotkey="ctrl+alt+p"):
def get_screenshot():
self.get_screenshot(
interpolation=cv2.INTER_AREA, grayscale=False, save_in_folder=folder,
)
print("Screenshot saved")
keyboard.add_hotkey(hotkey, get_screenshot)
return self
def _show_results(
self, quit_key="q", sleep_time: Union[float, int] = 0.05,
):
def activate_stop():
nonlocal stop
stop = True
stop = False
keyboard.add_hotkey(quit_key, activate_stop)
cv2.destroyAllWindows()
sleep(1)
while not stop:
screenshot_window = self.draw_results()
if cv2.waitKey(1) & 0xFF == ord(quit_key):
cv2.waitKey(0)
cv2.imshow("", screenshot_window)
sleep(sleep_time)
keyboard.remove_all_hotkeys()
return self
def _show_results_as_video(
self,
grayscale=True,
interpolation=cv2.INTER_AREA,
thresh=0.8,
workers=3,
quit_key="q",
sleep_time: Union[float, int] = 0.05,
):
def activate_stop():
nonlocal stop
stop = True
stop = False
keyboard.add_hotkey(quit_key, activate_stop)
cv2.destroyAllWindows()
sleep(1)
while not stop:
self.get_screenshot_and_start_detection(
grayscale=grayscale,
interpolation=interpolation,
thresh=thresh,
show_results=True,
workers=workers,
sleep_time_for_results=sleep_time,
quit_key_for_results=quit_key,
)
sleep(sleep_time)
keyboard.remove_all_hotkeys()
def show_results_as_video(
self,
grayscale=True,
interpolation=cv2.INTER_AREA,
thresh=0.8,
workers=3,
quit_key="q",
sleep_time: Union[float, int] = 0.05,
):
self.show_results_video_thread = kthread.KThread(
target=self._show_results_as_video,
name="videothread",
args=(grayscale, interpolation, thresh, workers, quit_key, sleep_time,),
)
self.show_results_video_thread.start()
if __name__ == "__main__":
# Let's say to want to automate Bluestacks, and
# therefore, need to detect different icons (needles) on your screen (haystack).
# https://github.com/hansalemaos/screenshots/raw/main/templatematching1.png
# Create an instance
template_matching = PatternMatchingOnScreen(
scale_percent=25, needle_folder=None, debug_folder=None,
)
# Choose a screenshot method to take the screenshot
# configure_window gets screenshots from a specific window, works also for background windows
template_matching.configure_window(
regular_expression=r"[bB]lue[sS]tacks.*", hwnd=None
)
# configure_monitor takes screenshots of the whole screen
template_matching.configure_monitor(monitor=1)
# If you are using bluestacks or an Android Phone, you can also connect over adb
template_matching.configure_adb(
adb_path=r"C:\ProgramData\adb\adb.exe", adb_serial="localhost:5735"
)
# Use save_screenshots_for_creating_needle_images to save screenshots on your HDD each time you press the hotkey
# (This step can also be done with any other screenshot tool)
template_matching.save_screenshots_for_creating_needle_images(
folder="c:\\templatemat", hotkey="ctrl+alt+p"
)
# After you are done, start the annotation tool (https://github.com/drainingsun/ybat) to crop the icons quickly.
# the requested class file (ybat) should look like this. It can be saved as txt
"""
playstore_icon
gamecenter_icon
systemapps_icon
roblox_icon
bluestacks_x_icon
spiele_und_gewinne_icon
kamera_icon
einstellungen_icon
chrome_icon
media_manager_icon
"""
# The files of the tool are included in this module.
# If you encounter any problem, download the ybat files and put them in the folder of this module
# After you are done, click on "Save COCO", and copy the link of the zipfile
# (This step can also be done with any other tool, e.g., Photoshop)
#
# https://github.com/hansalemaos/screenshots/raw/main/templatematching2.png
# https://github.com/hansalemaos/screenshots/blob/main/templatematching3.png
start_annotation_tool()
# After you are done, use this function to format the screenshots
create_needle_images_from_annotations(
cocojson=r"C:\Users\Gamer\Documents\Downloads\bboxes_coco.zip", # generated file by ybat
savefolder=r"C:\screenshots_for_detection",
# The folder where the screenshots you took are located, In my case:"c:\\templatemat"
outputfolder=r"C:\detectiontest", # Folder to save the results, that means the needle images you are going to use.
expand_x_negative=200, # you can limit the search region on the screen - saves time and false positives
expand_x_positive=200,
expand_y_negative=200,
expand_y_positive=200,
)
# After completing this step,
# you should have something like this in your output folder:
# https://github.com/hansalemaos/screenshots/raw/main/templatematching4.png
# (The 2 additional folders in the output folder can be deleted, they are for debugging)
# If you want to change the search region for a picture, you only have to change the file name:
# Here is one example:
# C:\detectiontest\playstore_icon--0x0--200x300.png
# The region (0,0), (200,300) will be checked for the image C:\detectiontest\playstore_icon--0x0--200x300.png
# if we rename the file to
# C:\detectiontest\playstore_icon--0x0--500x600.png
# the region will change to (0,0), (500,600)
# You can rename the file, but don't change the format (NAME)--(XCOORD)x(YCOORD)--(XCOORD)x(YCOORD).png
#
r"""
Some examples of file names
C:\detectiontest\playstore_icon--0x0--478x451.png
C:\detectiontest\gamecenter_icon--244x0--781x451.png
C:\detectiontest\systemapps_icon--528x0--1067x448.png
C:\detectiontest\roblox_icon--833x0--1342x448.png
C:\detectiontest\bluestacks_x_icon--1101x0--1643x449.png
C:\detectiontest\spiele_und_gewinne_icon--1347x0--1920x452.png
C:\detectiontest\kamera_icon--426x203--931x738.png
C:\detectiontest\einstellungen_icon--537x200--1038x735.png
C:\detectiontest\chrome_icon--643x199--1140x734.png