/
environment.py
272 lines (227 loc) · 9.23 KB
/
environment.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
try:
from PIL import Image
except ImportError:
import Image
import numpy as np
from PIL import ImageGrab
# import pyscreenshot as ImageGrab
import cv2
import numpy as np
import time
import time
from pymouse import PyMouse
from pykeyboard import PyKeyboard
import os
import torch
from getkeys import key_check
# Set TOP_LEFT here as per the right pixel by checking mouse position
# when running this script
TOP_LEFT = (180, 420)
HEIGHT, WIDTH = 300, 600
SCREEN = (TOP_LEFT[0], TOP_LEFT[1], TOP_LEFT[0] + WIDTH, TOP_LEFT[1] + HEIGHT)
# BROWN in RGB
BROWN = [88, 52, 20]
WHITE = [255, 255, 255]
# GAME_OVER_PIXEL = (100, 10)
GAME_OVER_PIXEL = (8, 18)
NEW_GAME_PIXEL = (TOP_LEFT[0] + 295, TOP_LEFT[1] + 250)
FRAMES = 1
# 4 Actions: Nothing, UP, LEFT, RIGHT
N_ACTIONS = 4
RESOLUTION = 84
mouse = PyMouse()
keyboard = PyKeyboard()
def init_env():
os.chdir("C:\Program Files\Mozilla Firefox")
os.system('firefox.exe --new-tab "https://www.miniclip.com/games/bug-on-a-wire/en/bug.swf?mc_gamename=Bug+On+A+Wire&mc_hsname=1446&mc_iconBig=bugmedicon.jpg&mc_icon=bugsmallicon.jpg&mc_negativescore=0&mc_players_site=1&mc_scoreistime=0&mc_lowscore=0&mc_width=600&mc_height=300&mc_lang=en&mc_webmaster=0&mc_playerbutton=0&mc_v2=1&loggedin=0&mc_loggedin=0&mc_uid=0&mc_sessid=f78c2dbb92961726d9a87c8f9aa753d2&mc_shockwave=0&mc_gameUrl=%2Fgames%2Fbug-on-a-wire%2Fen%2F&mc_ua=705d28c&mc_geo=us-west-2&mc_geoCode=US&vid=0&vtype=ima&m_vid=1&mc_preroll_check=1&channel=miniclip.preroll&m_channel=miniclip.midroll&s_content=0&mc_plat_id=2&mc_extra=enable_personalized_ads%3D1&mc_image_cdn_path=https%3A%2F%2Favatars.miniclip.com%2F&login_allowed=1&dfp_video_url=https%253A%252F%252Fpubads.g.doubleclick.net%252Fgampad%252Fads%253Fsz%253D600x400%2526iu%253D%252F116850162%252FMiniclip.com_Preroll%2526ciu_szs%2526impl%253Ds%2526gdfp_req%253D1%2526env%253Dvp%2526output%253Dxml_vast2%2526unviewed_position_start%253D1%2526cust_params%253D%2526npa%253D0%2526cust_params%253DgCat%25253Dcategory_13%252526gName%25253Dgame_1446%252526width%25253D600%252526height%25253D300%252526page_domain%25253Dgames%252526gAATF%25253Dgaatf_Y%252526gLanguage%25253Dlanguage_en%252526gPageType%25253Dpagetype_gamepage%252526gDemo1%25253Ddemo1_1%252526gDemo2%25253Ddemo2_2%252526gPageUrl%25253Dhttps%2525253A%2525252F%2525252Fwww.miniclip.com%2525252Fgames%2525252Fbug-on-a-wire%2525252Fen%2525252F%2526url%253D&fn=bug.swf"')
time.sleep(10)
os.chdir(r"C:\Users\nihal\github\bugOnAWireRL")
def generate_state(img, dim=RESOLUTION, channels=1):
bug_position = get_bug_position(img)
bug_state = np.zeros(4)
if bug_position != -1:
bug_state[bug_position] = 1
state = np.zeros((channels, dim, dim))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Color handling
brown_lo = np.array([0, 50, 100])
brown_hi = np.array([0, 100, 200])
mask = cv2.inRange(img, brown_lo, brown_hi)
img[mask>0] = (0, 0, 0)
blue_lo = np.array([180, 140, 20])
blue_hi = np.array([255, 220, 120])
mask = cv2.inRange(img, blue_lo, blue_hi)
img[mask>0] = (0, 0, 0)
crow_lo = np.array([40, 40, 40])
crow_hi = np.array([60, 60, 60])
mask = cv2.inRange(img, crow_lo, crow_hi)
img[mask>0] = (0, 0, 255)
# Remove corners
img[:40, :200, :] = 0
img[:40, -200:, :] = 0
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = cv2.resize(img,(dim, dim)) / 255.0
# if np.random.random_sample() > 0.5:
# mouse.click(TOP_LEFT[0] + 10, TOP_LEFT[1] + 10, 1)
# cv2.imshow('img', img)
# cv2.waitKey(0)
state[0] = img
return (state, bug_state)
def start_new_game():
mouse.click(TOP_LEFT[0], TOP_LEFT[1], 1)
mouse.click(NEW_GAME_PIXEL[0], NEW_GAME_PIXEL[1], 1)
frame = np.array(ImageGrab.grab(bbox=SCREEN))
time.sleep(0.1)
return generate_state(frame)
def is_game_over(frame):
'''
Checks pixel GAME_OVER_PIXEL corresponding to a patch in the scoreboard
that turns from white to brown upon game over screen transition.
If that pixel has value != WHITE
return True i.e. game is over
'''
return list(frame[GAME_OVER_PIXEL]) != WHITE
def press_key_with_wait(key):
keyboard.press_key(key)
time.sleep(0.1)
keyboard.release_key(key)
def get_reward_and_next_state(action):
if action == 0:
# NO ACTION
time.sleep(0.1)
elif action == 1:
# UP KEY
press_key_with_wait(keyboard.up_key)
elif action == 2:
# RIGHT KEY
press_key_with_wait(keyboard.right_key)
elif action == 3:
# LEFT KEY
press_key_with_wait(keyboard.left_key)
else:
raise Exception
state = np.array(ImageGrab.grab(bbox=SCREEN))
reward = sum(get_crows_positions(state)) * 1
bug_position = get_bug_position(state)
if (bug_position == 0 and action == 3 or \
bug_position == 3 and action == 2):
reward -= 1
if is_game_over(state):
state = None
frame_diff = None
reward = -5
else:
image_arrays = []
frame = np.array(ImageGrab.grab(bbox=SCREEN))
state = generate_state(frame)
return reward, state
def get_action_reward_and_next_state():
action = key_check()
state = np.array(ImageGrab.grab(bbox=SCREEN))
reward = sum(get_crows_positions(state)) * 1
if is_game_over(state):
state = None
frame_diff = None
reward = -5
else:
image_arrays = []
frame = np.array(ImageGrab.grab(bbox=SCREEN))
time.sleep(0.1)
state = generate_state(frame)
return action, reward, state
def get_crows_positions(img):
'''
Takes img as the unprocessed screen capture
Returns whether crows exist at the bug's line
in a boolean array like
[0 1 0 0] means bug only at wire_1
'''
crow_lo = np.array([190, 190, 0])
crow_hi = np.array([250, 250, 0])
wire_0 = img[630-420:, 220-180:360-180, :]
wire_1 = img[630-420:, 380-180:480-180, :]
wire_2 = img[630-420:, 500-180:600-180, :]
wire_3 = img[630-420:, 605-180:700-180, :]
wire = img[680-420:, 295-180:660-180, :]
mask_0 = cv2.inRange(wire_0, crow_lo, crow_hi)
mask_1 = cv2.inRange(wire_1, crow_lo, crow_hi)
mask_2 = cv2.inRange(wire_2, crow_lo, crow_hi)
mask_3 = cv2.inRange(wire_3, crow_lo, crow_hi)
crows = [0, 0, 0, 0]
if np.sum(np.nonzero(mask_0)) > 0:
crows[0] = 1
if np.sum(np.nonzero(mask_1)) > 0:
crows[1] = 1
if np.sum(np.nonzero(mask_2)) > 0:
crows[2] = 1
if np.sum(np.nonzero(mask_3)) > 0:
crows[3] = 1
# cv2.imshow('window', wire_3)
return crows
def get_bug_position(img):
'''
Takes img as the unprocessed screen capture
Returns bug's position as wire number
0 through 3 from Left to Right
'''
green_lo = np.array([0, 90, 0])
green_hi = np.array([0, 150, 0])
wire_0 = img[650-420:700-420, 295-180:340-180, :]
wire_1 = img[650-420:700-420, 405-180:450-180, :]
wire_2 = img[650-420:700-420, 505-180:550-180, :]
wire_3 = img[650-420:700-420, 615-180:660-180, :]
mask_0 = cv2.inRange(wire_0, green_lo, green_hi)
mask_1 = cv2.inRange(wire_1, green_lo, green_hi)
mask_2 = cv2.inRange(wire_2, green_lo, green_hi)
mask_3 = cv2.inRange(wire_3, green_lo, green_hi)
position = -1
if np.sum(np.nonzero(mask_0)) > 0:
position = 0
elif np.sum(np.nonzero(mask_1)) > 0:
position = 1
elif np.sum(np.nonzero(mask_2)) > 0:
position = 2
elif np.sum(np.nonzero(mask_3)) > 0:
position = 3
# cv2.imshow('window', mask_0)
return position
def screen_record():
last_time = time.time()
while(True):
img = np.array(ImageGrab.grab(bbox=SCREEN))
printscreen = cv2.cvtColor(img ,cv2.COLOR_BGR2RGB)
# print('loop took {} seconds'.format(time.time() - last_time))
m_pos = mouse.position()
color = img[(np.clip(m_pos[1] - TOP_LEFT[1], 0, HEIGHT - 1),
np.clip(m_pos[0] - TOP_LEFT[0], 0, WIDTH - 1))]
# Color handling
brown_lo = np.array([0, 50, 100])
brown_hi = np.array([0, 100, 200])
mask = cv2.inRange(printscreen, brown_lo, brown_hi)
printscreen[mask>0] = (0, 0, 0)
blue_lo = np.array([180, 140, 20])
blue_hi = np.array([255, 220, 120])
mask = cv2.inRange(printscreen, blue_lo, blue_hi)
printscreen[mask>0] = (0, 0, 0)
crow_lo = np.array([40, 40, 40])
crow_hi = np.array([60, 60, 60])
mask = cv2.inRange(printscreen, crow_lo, crow_hi)
printscreen[mask>0] = (0, 0, 255)
# Remove corners
printscreen[:40, :200, :] = 0
printscreen[:40, -200:, :] = 0
# print("Mouse position: {} Color: {}".format(m_pos, color))
# print("Game Over? " + str(is_game_over(img)))
last_time = time.time()
print("Bug position: {}".format(get_bug_position(img)))
r = sum(get_crows_positions(img))
if r > 0 and not is_game_over(img):
print("Crow positions: {}".format(get_crows_positions(img)))
printscreen = cv2.cvtColor(printscreen, cv2.COLOR_RGB2GRAY)
printscreen = cv2.resize(printscreen,(84, 84)) / 255.0
cv2.imshow('window', printscreen)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
if __name__ == '__main__':
screen_record()