-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdss.py
1639 lines (1391 loc) · 54.5 KB
/
dss.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 28 10:12:03 2021
@author: janusz
"""
import logging
import time
import tkinter as tk
import numpy as np
import pyautogui
from cv2 import cv2 as cv
from win32gui import GetForegroundWindow, GetWindowText
logging.basicConfig(level=logging.DEBUG)
LOAD_IMAGE = 0
# LOAD_IMAGE = 1
# ^ swap comment on LOAD_IMAGE to test in game
IMAGE_DEBUG_MODE = 0
IMAGE_DEBUG_MODE_FULLSCREEN = 0
X_FIRST_CHAMPION_CARD = 505
PADDING_BETWEEN_CHAMPION_CARDS = 14
W_CHAMPION_CARD = 175
CARDS_TO_BUY_AMOUNT = 5
Y_FIRST_CHAMPION_CARD = 865
H_CHAMPION_CARD = 135
LINE_TYPE = cv.LINE_4
MARKER_TYPE = cv.MARKER_CROSS
ORIGIN_LABEL_POSITION_COLUMN = 1
SHIFT_BETWEEN_ORIGINS = 6
CARDS_CENTER_LIST = [
(592, 932),
(781, 932),
(970, 932),
(1159, 932),
(1348, 932),
]
BUY_XP_CENTER = (400, 925)
REFRESH_CENTER = (400, 995)
CROPPING_X_CHAMPIONS = 450
CROPPING_Y_CHAMPIONS = 1000
CROPPING_WIDTH_CHAMPIONS = 1000
CROPPING_HEIGHT_CHAMPIONS = 30
CROPPING_X_ROUND = 760
CROPPING_X_ROUND_FIRST = 820
CROPPING_Y_ROUND = 30
CROPPING_WIDTH_ROUND = 60
CROPPING_HEIGHT_ROUND = 40
CROPPING_X_GOLD = 820
CROPPING_Y_GOLD = 850
CROPPING_WIDTH_GOLD = 100
CROPPING_HEIGHT_GOLD = 40
# screenshot = cv.imread("examples/windowed_pyauto_ss.jpg", cv.IMREAD_UNCHANGED)
screenshot = cv.imread("examples/pyautogui_ss_round_1.jpg", cv.IMREAD_UNCHANGED)
crop_img_champions = cv.imread("examples/windowed_pyauto_ss.jpg", cv.IMREAD_UNCHANGED)
crop_img_rounds = cv.imread("examples/windowed_pyauto_ss.jpg", cv.IMREAD_UNCHANGED)
crop_img_gold = cv.imread("examples/windowed_pyauto_ss.jpg", cv.IMREAD_UNCHANGED)
ocr_results_champions = [
([[67, 5], [113, 5], [113, 21], [67, 21]], "Leona", 0.9666508436203003),
([[255, 5], [303, 5], [303, 23], [255, 23]], "Vayne", 0.995654284954071),
([[445, 5], [507, 5], [507, 21], [445, 21]], "Vladimir", 0.9939249753952026),
([[633, 5], [685, 5], [685, 21], [633, 21]], "Aatrox", 0.9471874833106995),
([[823, 5], [889, 5], [889, 21], [823, 21]], "Warwick", 0.9830108880996704),
]
ocr_results_round = 11
ocr_results_gold = 0
sorted_champions_to_buy = ["Brand", "Leona", "Udyr", "Vayne", "Soraka"]
pyautogui.PAUSE = 0.02
pyautogui.FAILSAFE = False
def filling_list_with_counter_for_namedtuple(
field_to_check,
input_list,
origin_list_,
class_list_,
origin_counters_,
class_counters_,
df_,
):
"""
Parameters
----------
field_to_check : {4:"origin_prim", 5:"origin_sec", 6:"class_prim",
7:"class_sec"}
input_list : List with 4,5,6,7 field == field_to_check. The default is champion_info.
Returns
-------
list_of_counters : List of GUI counters
"""
logging.debug("Function filling_list_with_counter_for_namedtuple() called")
field_to_check_2_string = {
4: "origin_prim",
5: "origin_sec",
6: "class_prim",
7: "class_sec",
}
field_to_check_2_check_list = {
4: origin_list_,
5: origin_list_,
6: class_list_,
7: class_list_,
}
field_to_check_2_check_list_string = {
4: "origin_list",
5: "origin_list",
6: "class_list",
7: "class_list",
}
field_to_check_2_counters_list = {
4: origin_counters_,
5: origin_counters_,
6: class_counters_,
7: class_counters_,
}
list_of_counters = [None] * len(df_.champion)
for i, champ in enumerate(df_.champion):
if input_list[i][field_to_check] == "None":
logging.info("Champion name: %s, champion index = %d", champ, i)
logging.info(
"Field with index %d == 'NONE' filling None as %sCounter",
field_to_check,
field_to_check_2_string[field_to_check],
)
list_of_counters[i] = None
else:
logging.info("%s IS NOT 'NONE'", field_to_check_2_string[field_to_check])
for j, class_or_origin in enumerate(
field_to_check_2_check_list[field_to_check]
):
if input_list[i][field_to_check] == class_or_origin:
logging.info("Champion name: %s, champion index = %d", champ, i)
logging.info(
"Found match in champion %s and %s for : %s",
field_to_check_2_string[field_to_check],
field_to_check_2_check_list_string[field_to_check],
class_or_origin,
)
logging.info(
"Filling %s counter: %s",
field_to_check_2_string[field_to_check],
field_to_check_2_counters_list[field_to_check][j],
)
list_of_counters[i] = field_to_check_2_counters_list[
field_to_check
][j]
logging.debug("Function filling_list_with_counter_for_namedtuple() end")
return list_of_counters
def append_counters_to_input_list(
input_list, origin_list_, class_list_, origin_counters_, class_counters_, df_
):
"""
Parameters
----------
input_list : Appending counters to list with fields like {4:"origin_prim",
5:"origin_sec", 6:"class_prim", 7:"class_sec"}.
Returns
-------
None.
"""
logging.debug("Function filling_list_with_counter_for_namedtuple() called")
counters_to_append = [4, 5, 6, 7]
for j in counters_to_append:
list_of_counters_to_append = filling_list_with_counter_for_namedtuple(
j,
input_list,
origin_list_,
class_list_,
origin_counters_,
class_counters_,
df_,
)
for i, champ in enumerate(input_list):
champ.append(list_of_counters_to_append[i])
logging.debug("Function filling_list_with_counter_for_namedtuple() end")
def add(counter):
"""Adding one to counter"""
logging.debug("Function add() called")
logging.info("input = %d", counter.get())
counter.set(counter.get() + 1)
logging.info("after call = %d", counter.get())
logging.debug("Function add() end")
def sub(counter):
"""Minus one to counter"""
logging.debug("Function sub() called")
logging.info("input = %d", counter.get())
if counter.get() > 0:
counter.set(counter.get() - 1)
logging.info("after call = %d", counter.get())
logging.debug("Function sub() end")
def imshow_fullscreen(window_name="img", image=[0]):
"""
https://stackoverflow.com/questions/9446733/opencv-window-in-fullscreen-and-without-any-borders#comment97925305_53005272
Parameters
----------
image : openCV image matrix.
window_name : string, name for hidden window. The default is "img".
Returns
-------
None.
"""
cv.namedWindow(window_name, cv.WND_PROP_FULLSCREEN)
cv.setWindowProperty(window_name, cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
cv.imshow(window_name, image)
def make_ss(
DSS_ON=1,
IMAGE_DEBUG_MODE_=IMAGE_DEBUG_MODE,
IMAGE_DEBUG_MODE_FULLSCREEN_=IMAGE_DEBUG_MODE_FULLSCREEN,
):
"""
Parameters
----------
IMAGE_DEBUG_MODE_ : 0 or 1, calls cv.imshow().
The default is IMAGE_DEBUG_MODE.
IMAGE_DEBUG_MODE_FULLSCREEN_ : 0 or 1, calls dss.imshow_fullscreen().
The default is IMAGE_DEBUG_MODE_FULLSCREEN.
Returns
-------
screenshot : screenshot of game.
"""
logging.debug("Function make_ss() called")
activate_window(mode="game", delay=0.2)
screenshot = pyautogui.screenshot()
screenshot = cv.cvtColor(np.array(screenshot), cv.COLOR_RGB2BGR)
if DSS_ON:
activate_window(mode="dss", delay=0.2)
if IMAGE_DEBUG_MODE_:
if not IMAGE_DEBUG_MODE_FULLSCREEN_:
cv.imshow("make_ss() screenshot", screenshot)
else:
imshow_fullscreen(window_name="make_ss() screenshot", image=screenshot)
logging.debug("Function make_ss() end")
return screenshot
def update_curent_ss(DSS_ON_=1):
"""
Updates global state current screenshot.
Returns
-------
None.
"""
global screenshot
logging.debug("Function update_curent_ss() called")
screenshot = make_ss(
DSS_ON=DSS_ON_, IMAGE_DEBUG_MODE_=1, IMAGE_DEBUG_MODE_FULLSCREEN_=0
)
logging.debug("Function update_curent_ss() end")
def crop_ss(
screenshot_=screenshot,
cropping_x=CROPPING_X_CHAMPIONS,
cropping_y=CROPPING_Y_CHAMPIONS,
cropping_width=CROPPING_WIDTH_CHAMPIONS,
cropping_height=CROPPING_HEIGHT_CHAMPIONS,
IMAGE_DEBUG_MODE_=IMAGE_DEBUG_MODE,
IMAGE_DEBUG_MODE_FULLSCREEN_=IMAGE_DEBUG_MODE_FULLSCREEN,
):
"""
Crops given screenshot.
Parameters
----------
screenshot_ : OPENCV IMAGE, ss from game. The default is screenshot.
cropping_x : INT. The default is CROPPING_X_CHAMPIONS.
cropping_y : INT. The default is CROPPING_Y_CHAMPIONS.
cropping_width : INT. The default is CROPPING_WIDTH_CHAMPIONS.
cropping_height : INT. The default is CROPPING_HEIGHT_CHAMPIONS.
IMAGE_DEBUG_MODE_ : BOOLEAN, False no cv.imshow() call, True cv.imshow() is called.
The default is IMAGE_DEBUG_MODE.
IMAGE_DEBUG_MODE_FULLSCREEN_ : BOOLEAN, True opens cv.imshow() in fullscreen mode.
The default is IMAGE_DEBUG_MODE_FULLSCREEN.
Returns
-------
crop_img : OPENCV IMAGE
cropped img of given screenshot.
"""
logging.debug("Function crop_ss() called")
crop_img = screenshot_[
cropping_y : cropping_y + cropping_height,
cropping_x : cropping_x + cropping_width,
]
if IMAGE_DEBUG_MODE_:
if not IMAGE_DEBUG_MODE_FULLSCREEN_:
cv.imshow("crop_ss() screenshot", screenshot_)
cv.imshow("crop_ss() crop_img", crop_img)
else:
imshow_fullscreen(window_name="crop_ss() screenshot", image=screenshot_)
cv.imshow("crop_ss() crop_img", crop_img)
logging.debug("Function crop_ss() end")
return crop_img
def update_curent_cropped_ss_with_champions():
"""
Crops global state current screenshot.
Returns
-------
None.
"""
global crop_img_champions
logging.debug("Function update_curent_cropped_ss_with_champions() called")
crop_img_champions = crop_ss(screenshot_=screenshot)
logging.debug("Function update_curent_cropped_ss_with_champions() end")
def update_curent_cropped_ss_with_rounds(cropping_x__=CROPPING_X_ROUND):
"""
Crops global state current screenshot.
Returns
-------
None.
"""
global crop_img_rounds
logging.debug("Function update_curent_cropped_ss_with_rounds() called")
crop_img_rounds = crop_ss(
screenshot_=screenshot,
cropping_x=cropping_x__,
cropping_y=CROPPING_Y_ROUND,
cropping_width=CROPPING_WIDTH_ROUND,
cropping_height=CROPPING_HEIGHT_ROUND,
IMAGE_DEBUG_MODE_=IMAGE_DEBUG_MODE,
IMAGE_DEBUG_MODE_FULLSCREEN_=IMAGE_DEBUG_MODE_FULLSCREEN,
)
logging.debug("Function update_curent_cropped_ss_with_rounds() end")
def update_curent_cropped_ss_with_gold():
"""
Crops global state current screenshot.
Returns
-------
None.
"""
global crop_img_gold
logging.debug("Function update_curent_cropped_ss_with_gold() called")
crop_img_gold = crop_ss(
screenshot_=screenshot,
cropping_x=CROPPING_X_GOLD,
cropping_y=CROPPING_Y_GOLD,
cropping_width=CROPPING_WIDTH_GOLD,
cropping_height=CROPPING_HEIGHT_GOLD,
IMAGE_DEBUG_MODE_=IMAGE_DEBUG_MODE,
IMAGE_DEBUG_MODE_FULLSCREEN_=IMAGE_DEBUG_MODE_FULLSCREEN,
)
logging.debug("Function update_curent_cropped_ss_with_gold() end")
def ocr_on_cropped_img(cropped_ss_with_champion_card_names=None, reader_=None):
"""
Parameters
----------
cropped_ss_with_champion_card_names : for example if want to OCR card names then
input there crop_img which can be updated by update_curent_cropped_ss_with_champions().
Returns
-------
ocr_result :
"""
logging.debug("Function ocr_on_cropped_img() called")
ocr_result = reader_.readtext(cropped_ss_with_champion_card_names)
logging.info("OCR results(return): %s", ocr_result)
logging.debug("Function ocr_on_cropped_img() end")
return ocr_result
def update_ocr_results_champions(reader_=None):
global crop_img_champions, ocr_results_champions
logging.debug("Function update_ocr_results_champions() called")
ocr_results_champions = ocr_on_cropped_img(
cropped_ss_with_champion_card_names=crop_img_champions, reader_=reader_
)
logging.debug("Function update_ocr_results_champions() end")
def update_ocr_results_round(reader_=None, round_counter=None):
logging.debug("Function update_ocr_results_round() called")
global crop_img_rounds, ocr_results_round
update_curent_cropped_ss_with_rounds(cropping_x__=CROPPING_X_ROUND)
OCRResult = ocr_on_cropped_img(
cropped_ss_with_champion_card_names=crop_img_rounds, reader_=reader_
)
if not OCRResult:
logging.warning("OCRResult is empty probably rounds from first stage 1-x")
update_curent_cropped_ss_with_rounds(cropping_x__=CROPPING_X_ROUND_FIRST)
OCRResult = ocr_on_cropped_img(
cropped_ss_with_champion_card_names=crop_img_rounds, reader_=reader_
)
try:
print("Round: ", OCRResult[0][1])
ocr_results_round = OCRResult[0][1]
ocr_results_round = ocr_results_round.replace("-", "")
logging.info("Found round: %s", ocr_results_round)
logging.debug("Function update_ocr_results_round() end")
if round_counter:
round_counter.set(ocr_results_round)
return ocr_results_round
except (IndexError):
logging.info("Couldnt find round")
logging.debug("Function update_ocr_results_round() end")
return None
def update_ocr_results_gold(reader_=None, gold_counter=None):
global crop_img_gold, ocr_results_gold
logging.debug("Function update_ocr_results_gold() called")
OCRResult = ocr_on_cropped_img(
cropped_ss_with_champion_card_names=crop_img_gold, reader_=reader_
)
try:
print("Gold: ", OCRResult[0][1])
ocr_results_gold = OCRResult[0][1]
logging.info("Found gold: %s", ocr_results_gold)
logging.debug("Function update_ocr_results_gold() end")
gold_counter.set(ocr_results_gold)
return ocr_results_gold
except (IndexError):
logging.info("Couldnt find gold")
logging.debug("Function update_ocr_results_gold() end")
return None
logging.debug("Function update_ocr_results_gold() end")
def sort_detected_champions_to_buy_by_position(
ocr_results_sorted=None, champions_list_for_ocr_=None
):
"""
Sorting input in order from left to right by placement on the screen
(lowest width is first).Then filters out champion names, numbers(champions cost)
are discarded.
Parameters
----------
ocr_results_sorted : Typical == ocr_results_champions which is global state.
champions_list_for_ocr_ : Typical == champions_list_for_ocr.
Returns
-------
sorted_champions_to_buy : List of champions that were found in input..
"""
logging.debug("Function sort_detected_champions_to_buy_by_position() called")
# sort from lowest width (left to right side)
ocr_results_sorted = sorted(ocr_results_sorted, key=lambda x: x[0])
sorted_champions_to_buy = []
for text in ocr_results_sorted:
for champ in champions_list_for_ocr_:
if champ in text: # filters champion names
sorted_champions_to_buy.append(champ)
logging.info(
"from for loop in sort_detected_champions_to_buy_by_position()"
)
logging.info("found %s", champ)
logging.info("return in sort_detected_champions_to_buy_by_position()")
logging.info("List of sorted champions to buy: %s", sorted_champions_to_buy)
logging.debug("Function sort_detected_champions_to_buy_by_position() end")
return sorted_champions_to_buy
def update_sorted_champions_to_buy(champions_list_for_ocr_=None):
"""
Updates sorted_champions_to_buy global state.
Parameters
----------
champions_list_for_ocr_ : The default is None.
Returns
-------
None.
"""
global ocr_results_champions, sorted_champions_to_buy
logging.debug("Function update_sorted_champions_to_buy() called")
sorted_champions_to_buy = sort_detected_champions_to_buy_by_position(
ocr_results_sorted=ocr_results_champions,
champions_list_for_ocr_=champions_list_for_ocr_,
)
logging.debug("Function update_sorted_champions_to_buy() end")
def test(ocr_on_cropped_img, **kwargs):
# https://stackoverflow.com/questions/6289646/python-function-as-a-function-argument
return ocr_on_cropped_img(**kwargs)
def generate_list_of_champions_to_buy_this_turn(
sort_detected_champions_to_buy_by_position, **kwargs
):
list_of_champs_to_buy_this_turn = sort_detected_champions_to_buy_by_position(
**kwargs
)
print(list_of_champs_to_buy_this_turn)
return list_of_champs_to_buy_this_turn
# generate_list_of_champions_to_buy_this_turn(sort_detected_champions_to_buy_by_position, ocr_results_sorted=ocr_on_cropped_img(make_cropped_ss(LOAD_IMAGE_=1)[0], reader_=reader),champions_list_for_ocr_=champions_list_for_ocr)
def full_state_update_champions_ocr(
DSS_ON_=1, reader_=None, champions_list_for_ocr_=None
):
"""
Updates sorted_champions_to_buy global variable.
Parameters
----------
reader_ : easyOCR reader. The default is None.
champions_list_for_ocr_ : champions_list_for_ocr. The default is None.
Returns
-------
None.
"""
logging.debug("Function full_state_update_champions_ocr() called")
update_curent_ss(DSS_ON_=DSS_ON_)
update_curent_cropped_ss_with_champions()
update_ocr_results_champions(reader_=reader_)
update_sorted_champions_to_buy(champions_list_for_ocr_=champions_list_for_ocr_)
logging.debug("Function full_state_update_champions_ocr() end")
def full_state_update_rounds_ocr(DSS_ON_=1, reader_=None, round_counter=None):
"""
Updates ocr_results_round global variable.
Parameters
----------
reader_ : easyOCR reader. The default is None.
Returns
-------
None.
"""
logging.debug("Function full_state_update_rounds_ocr() called")
update_curent_ss(DSS_ON_=DSS_ON_)
update_ocr_results_round(reader_=reader_, round_counter=round_counter)
logging.debug("Function full_state_update_rounds_ocr() end")
def full_state_update_gold_ocr(DSS_ON_=1, reader_=None, gold_counter=None):
"""
Updates ocr_results_gold global variable.
Parameters
----------
reader_ : easyOCR reader. The default is None.
Returns
-------
None.
"""
logging.debug("Function full_state_update_gold_ocr() called")
update_curent_ss(DSS_ON_=DSS_ON_)
update_curent_cropped_ss_with_gold()
update_ocr_results_gold(reader_=reader_, gold_counter=gold_counter)
logging.debug("Function full_state_update_gold_ocr() end")
def update_champions_to_buy_from_ocr_detection(
sorted_champions_to_buy_,
champions_list_for_ocr__,
origin_champs_counters_to_buy_,
reader_,
DSS_ON_=1,
):
"""
Add 1 to every champion to buy counter detected in ocr_result.
champion to buy counters GLOBAL STATE CHANGE !!!!!!!!!!!!!!!!!!!!
Returns
-------
None.
"""
global sorted_champions_to_buy
logging.debug("Function update_champions_to_buy_from_ocr_detection() called")
full_state_update_champions_ocr(
DSS_ON_=DSS_ON_,
reader_=reader_,
champions_list_for_ocr_=champions_list_for_ocr__,
)
champs_to_buy_indexes = []
for champ_to_buy in sorted_champions_to_buy:
for i, champ in enumerate(champions_list_for_ocr__):
if champ_to_buy == champ:
logging.info(
"IF inside for loop in update_champions_to_buy_from_ocr_detection()"
)
logging.info("Index in champions_list_for_ocr that is detected: %d", i)
logging.info("Champ name in this index: %s", champ)
if origin_champs_counters_to_buy_:
add(origin_champs_counters_to_buy_[i])
champs_to_buy_indexes.append(i)
break
logging.info("Champions to buy indexes: %s", champs_to_buy_indexes)
logging.debug("Function update_champions_to_buy_from_ocr_detection() end")
return sorted_champions_to_buy, champs_to_buy_indexes
def calculate_card_position_on_screen(
card_index,
X_FIRST_CHAMPION_CARD_=X_FIRST_CHAMPION_CARD,
PADDING_BETWEEN_CHAMPION_CARDS_=PADDING_BETWEEN_CHAMPION_CARDS,
W_CHAMPION_CARD_=W_CHAMPION_CARD,
):
"""
Parameters
----------
card_index : simply from 0-4(first to fifth card)
Returns
-------
x_card : x Position on the screen of the top left corner for card
"""
logging.debug("Function calculate_card_position_on_screen() called")
x_card = (
X_FIRST_CHAMPION_CARD_
+ PADDING_BETWEEN_CHAMPION_CARDS_ * card_index
+ W_CHAMPION_CARD_ * card_index
)
logging.info("X coord of card with index= %d is: %s", card_index, x_card)
logging.debug("Function calculate_card_position_on_screen() end")
return x_card
def build_list_of_champion_cards_rectangles(
CARDS_TO_BUY_AMOUNT_=CARDS_TO_BUY_AMOUNT,
Y_FIRST_CHAMPION_CARD_=Y_FIRST_CHAMPION_CARD,
W_CHAMPION_CARD_=W_CHAMPION_CARD,
H_CHAMPION_CARD_=H_CHAMPION_CARD,
):
"""
This function building list of card rectangles position on screen.
Returns
-------
cards_rectangles : list of card rectangles position on screen
"""
logging.debug("Function build_list_of_champion_cards_rectangles() called")
cards_rectangles = [0] * CARDS_TO_BUY_AMOUNT_
for i in range(0, CARDS_TO_BUY_AMOUNT_):
x_current_champ_card = calculate_card_position_on_screen(i)
top_left = (x_current_champ_card, Y_FIRST_CHAMPION_CARD_)
bottom_right = (
x_current_champ_card + W_CHAMPION_CARD_,
Y_FIRST_CHAMPION_CARD_ + H_CHAMPION_CARD_,
)
center = (
top_left[0] + W_CHAMPION_CARD_ // 2,
top_left[1] + H_CHAMPION_CARD_ // 2,
)
# print("Type" ,type(center))
cards_rectangles[i] = [top_left, bottom_right, center]
logging.info(
"Card rectangle = [top_left, bottom_right, center]: %s", cards_rectangles[i]
)
logging.debug("Function build_list_of_champion_cards_rectangles() end")
return cards_rectangles
# https://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list
def draw_rectangles_show_points_show_buttons_reset_counters(
rgb_colours_list_,
sorted_champions_to_buy_,
champions_list_for_ocr_,
origin_champs_counters_to_buy_,
reader_,
champions_list_,
tk_window,
origin_champs_counters_,
df_,
origin_list_,
origin_counters_,
class_list_,
class_counters_,
round_counter,
gold_counter,
mode="points",
CARDS_TO_BUY_AMOUNT_=CARDS_TO_BUY_AMOUNT,
LINE_TYPE_=LINE_TYPE,
MARKER_TYPE_=MARKER_TYPE,
):
"""
This function is making OCR detection on champion cards, and then draws by
input mode like default points on screenshot.
Parameters
----------
rgb_colours_list_ : ["worst", "medium3", "medium2", "medium1", "best"]. list of RGB tuples.
The default is rgb_colours_list.
mode : The default is "points". Also there are cross and rectangle.
Returns
-------
None.
"""
logging.debug(
"Function draw_rectangles_show_points_show_buttons_reset_counters() called"
)
reset_counters_in_list(origin_champs_counters_to_buy_)
(
list_of_champs_to_buy_this_turn,
index_list,
) = update_champions_to_buy_from_ocr_detection(
sorted_champions_to_buy_=sorted_champions_to_buy_,
champions_list_for_ocr__=champions_list_for_ocr_,
origin_champs_counters_to_buy_=origin_champs_counters_to_buy_,
reader_=reader_,
)
champions_to_buy_in_order_as_in_screen = list_of_champs_to_buy_this_turn
champions_to_buy_points_and_position = show_nonzero_counters_with_points_from_ocr(
tk_window,
origin_champs_counters_,
origin_champs_counters_to_buy_,
champions_list_,
df_,
index_list,
origin_list_,
origin_counters_,
class_list_,
class_counters_,
)
champions_position_to_buy_ordered_by_screen = [
champions_list_for_ocr_.index(i) for i in champions_to_buy_in_order_as_in_screen
]
logging.info(
"champions_position_to_buy_ordered_by_screen: %s",
champions_position_to_buy_ordered_by_screen,
)
champions_to_buy_points = list(zip(*champions_to_buy_points_and_position))[0]
champions_to_buy_position = list(zip(*champions_to_buy_points_and_position))[1]
logging.info(
"Points (in alphabetical by champ name order?): %s", champions_to_buy_points
)
logging.info(
"Champions position (in alphabetical by champ name order?): %s",
champions_to_buy_position,
)
sorted_champions_to_buy_points_and_position = sorted(
champions_to_buy_points_and_position
)
logging.info(
"Points and Champions position (in alphabetical by champ name order?): %s",
sorted_champions_to_buy_points_and_position,
)
sorted_champions_to_buy_position = list(
zip(*sorted_champions_to_buy_points_and_position)
)[1]
logging.info(
"sorted_champions_to_buy_position in alphabetical order?: %s",
sorted_champions_to_buy_position,
)
values_by_points_indexes_order_by_position_on_screen = [
sorted_champions_to_buy_position.index(i)
for i in champions_position_to_buy_ordered_by_screen
]
logging.info(
"values_by_points_indexes_order_by_position_on_screen 0 worst 4 best card: %s",
values_by_points_indexes_order_by_position_on_screen,
)
cards_rectangles = build_list_of_champion_cards_rectangles()
# at the end
# values_by_points_indexes_order_by_position_on_screen contains champions
# sorted by points from lowest(0) to highest(4)
# and indexes represents champion placement on the screen
if mode == "rectangle":
for i in range(0, CARDS_TO_BUY_AMOUNT_):
cv.rectangle(
screenshot,
cards_rectangles[i][0],
cards_rectangles[i][1],
color=rgb_colours_list_[
values_by_points_indexes_order_by_position_on_screen[i]
],
lineType=LINE_TYPE_,
thickness=2,
)
cv.imshow(
"draw_rectangles_show_points_show_buttons_reset_counters()", screenshot
)
elif mode == "cross":
for i in range(0, CARDS_TO_BUY_AMOUNT_):
# Draw the center point
cv.drawMarker(
screenshot,
cards_rectangles[i][2],
color=rgb_colours_list_[
values_by_points_indexes_order_by_position_on_screen[i]
],
markerType=MARKER_TYPE_,
markerSize=40,
thickness=2,
)
cv.imshow(
"draw_rectangles_show_points_show_buttons_reset_counters()", screenshot
)
elif mode == "points":
for i in range(0, CARDS_TO_BUY_AMOUNT_):
# Draw the center point
cv.putText(
screenshot,
"{:.3f}".format(
sorted_champions_to_buy_points_and_position[
values_by_points_indexes_order_by_position_on_screen[i]
][0]
),
cards_rectangles[i][2],
cv.FONT_HERSHEY_SIMPLEX,
0.6,
rgb_colours_list_[
values_by_points_indexes_order_by_position_on_screen[i]
],
2,
)
cv.imshow(
"draw_rectangles_show_points_show_buttons_reset_counters()", screenshot
)
update_ocr_results_round(reader_=reader_, round_counter=round_counter)
update_curent_cropped_ss_with_gold()
update_ocr_results_gold(reader_=reader_, gold_counter=gold_counter)
logging.debug(
"Function draw_rectangles_show_points_show_buttons_reset_counters() end"
)
def create_gui_counter_with_plus_minus(
window_tk,
origin_index,
counter,
shift_between_upside_downside,
i=0,
SHIFT_BETWEEN_ORIGINS_=SHIFT_BETWEEN_ORIGINS,
):
"""
Creating counter with plus and minus buttons.
Parameters
----------
window_tk : tkinter window.
origin_index : to organize origins in columns.
counter : Name of counter.
shift_between_upside_downside : Shift to place upside or downside.
i : counter index. The default is 0 for adding single button without loop.
SHIFT_BETWEEN_ORIGINS_ : The default is SHIFT_BETWEEN_ORIGINS.
Returns
-------
None.
"""
tk.Entry(window_tk, textvariable=counter, width=2).grid(
row=2 + i + shift_between_upside_downside,
column=SHIFT_BETWEEN_ORIGINS_ * origin_index + 1,
)
tk.Button(window_tk, text="+", command=lambda counter=counter: add(counter),).grid(
row=2 + i + shift_between_upside_downside,
column=SHIFT_BETWEEN_ORIGINS_ * origin_index + 2,
)
tk.Button(window_tk, text="-", command=lambda counter=counter: sub(counter),).grid(
row=2 + i + shift_between_upside_downside,
column=SHIFT_BETWEEN_ORIGINS_ * origin_index + 3,
)
def show_champions_from_origin(
window_tk,
origin_index,
origin_champs_from_df_list_,
origin_list_,
champions_list_,
shift_between_upside_downside,
ORIGIN_LABEL_POSITION_COLUMN_=ORIGIN_LABEL_POSITION_COLUMN,
SHIFT_BETWEEN_ORIGINS_=SHIFT_BETWEEN_ORIGINS,
):
"""Adding buttons and text labels for single origin.
In: origin_index - its used to pickup origin from origin_list_,
and place text on the window.
origin_champs_from_df_list_ - list of champions in origin.
champions_list_ - list of champions namedtuples.
shift_between_upside_downside - placing on the window, UPSIDE is upper location,
DOWNSIDE is lower location.
"""
logging.debug("Function show_champions_from_origin() called")
tk.Label(window_tk, text=origin_list_[origin_index]).grid(
row=1 + shift_between_upside_downside,
column=ORIGIN_LABEL_POSITION_COLUMN_ * SHIFT_BETWEEN_ORIGINS_ * origin_index,
)
for i, champ_name in enumerate(origin_champs_from_df_list_):
tk.Label(window_tk, text=champ_name).grid(
row=2 + i + shift_between_upside_downside,
column=ORIGIN_LABEL_POSITION_COLUMN_
* SHIFT_BETWEEN_ORIGINS_
* origin_index,
)
for champ in champions_list_:
if champ.name == champ_name:
create_gui_counter_with_plus_minus(
window_tk=window_tk,
origin_index=origin_index,
counter=champ.ChampCounter,
shift_between_upside_downside=shift_between_upside_downside,
i=i,
SHIFT_BETWEEN_ORIGINS_=SHIFT_BETWEEN_ORIGINS,
)
break
logging.debug("Function show_champions_from_origin() end")
def show_classes_or_origins(
window_tk,