-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
146 lines (122 loc) · 4.98 KB
/
run.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
import cv2
import time
import os
import collections
import pyautogui
import PIL.ImageGrab as ImageGrab
global run_status
run_status = 0
global screenshot
#----------------------------
#
#
screenshot = "images/1.png" #
#
#
#----------------------------
os.system('color 0a')
def main():
print("running in loop through while command")
play = True
while play:
try:
work()
print()
#time.sleep(1) #at 0.08 score 240
os.system("pause")
play = False #delete this line if you want to play in loop
except Exception as e:
time.sleep(1) #score = 235
play = False
print(e)
def click(ord):
pyautogui.moveTo(ord)#moves to x,y (top left as reference)
pyautogui.click()
def capture():
im = ImageGrab.grab()
#im.save('scr_shot.png','png') #originally this was made to play virtual game but for this tutorial i am using a image instead
def work():
global run_status
print("run status is: " + str(run_status))
run_status += 1
capture()
#Reading image and cropping Numbers =========================================================================================
#deleting already written photos==================================
print('deleting photos from recognition/temp_photos if any')
try:
#os.system("call recognition/temp_photos/cleaner.bat")
os.system("cleaner.exe")
except Exception as e:
print(e)
pass
if not os.path.exists('recognition/temp_photos/arranged'):
os.mkdir('recognition/temp_photos/arranged')
if not os.path.exists('recognition/temp_photos/cropped'):
os.mkdir('recognition/temp_photos/cropped')
print('finding contour and writing photos')
#=================================================================
info_dict = {}
image = cv2.imread(screenshot)
image_blur = cv2.GaussianBlur(image.copy(), (5, 5), 0)
gray=cv2.cvtColor(image_blur,cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 10, 250)
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
idx = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
if w>50 and w<85 and h>50 and h<85:
idx+=1
c_cord = (x+40,y+40)
#print('idx: '+str(idx)+', Coord: '+str(c_cord))
info_dict.update({idx : c_cord})
new_img=image[y:y+h,x:x+w]
cv2.imwrite('recognition/temp_photos/cropped/'+str(idx) + '.png', new_img)
#============================================================================================================================
#object for recognizing number =============================================================================================
print('running find_number.py, to recognize the numbers')
from recognition.find_number import my_recognizer
my_recognizer = my_recognizer.start()
#============================================================================================================================
#object for recognized number list ==========================================================================================
print('running dictionary.py, to get the informations of recognized numbers')
from recognition.temp_photos.dictionary import my_dict
my_dict = my_dict.start('recognition/temp_photos/arranged')
#============================================================================================================================
#Altering lists to find information==========================================================================================
print('val_dict: '+str(my_dict)) #idx, value
print('coord_dict: '+str(info_dict)) #idx, coord)
print('altering lists to link found informations')
x_x = list(my_dict)
y_y = list(info_dict)
#lets concatenate above two dictionaries
fin_dict = {}
for i in my_dict:
x_x = my_dict[i]
y_y = info_dict[i]
fin_dict.update({x_x:y_y})
print('finally')
print(fin_dict)
fin_dict = collections.OrderedDict(sorted(fin_dict.items()))
print('sorted')
print(fin_dict)
list_num_map = list(fin_dict)
print('Digits: ',end='')
print(list_num_map)
print()
#accessing dictionary items without knowing key values using index after storing in list===========
num_idx = len(list_num_map)
print('Number of digits found: '+str(num_idx))
print()
#============================================================================================================================
#finally clicking in ascending order=========================================================================================
for idx in range (0, num_idx):
print('idx: '+str(idx))
print('digit: '+str(list_num_map[idx]))
print('(x,y): '+str(fin_dict[list_num_map[idx]]))
print('click'+str(fin_dict[list_num_map[idx]]))
#click(fin_dict[list_num_map[idx]])
print()
#============================================================================================================================
###################################################################################################
if __name__ == "__main__":
main()