-
Notifications
You must be signed in to change notification settings - Fork 412
/
nn_play.py
212 lines (191 loc) · 8.81 KB
/
nn_play.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
# -*- coding:utf-8 -*-
# Created Time: 六 12/30 13:49:21 2017
# Author: Taihong Xiao <[email protected]>
import numpy as np
import time
import os, glob, shutil
import cv2
import argparse
import tensorflow as tf
from model import JumpModel
from model_fine import JumpModelFine
def multi_scale_search(pivot, screen, range=0.3, num=10):
H, W = screen.shape[:2]
h, w = pivot.shape[:2]
found = None
for scale in np.linspace(1-range, 1+range, num)[::-1]:
resized = cv2.resize(screen, (int(W * scale), int(H * scale)))
r = W / float(resized.shape[1])
if resized.shape[0] < h or resized.shape[1] < w:
break
res = cv2.matchTemplate(resized, pivot, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= res.max())
pos_h, pos_w = list(zip(*loc))[0]
if found is None or res.max() > found[-1]:
found = (pos_h, pos_w, r, res.max())
if found is None: return (0,0,0,0,0)
pos_h, pos_w, r, score = found
start_h, start_w = int(pos_h * r), int(pos_w * r)
end_h, end_w = int((pos_h + h) * r), int((pos_w + w) * r)
return [start_h, start_w, end_h, end_w, score]
class WechatAutoJump(object):
def __init__(self, phone, sensitivity, serverURL, debug, resource_dir):
self.phone = phone
self.sensitivity = sensitivity
self.debug = debug
self.resource_dir = resource_dir
self.step = 0
self.ckpt = os.path.join(self.resource_dir, 'train_logs_coarse/best_model.ckpt-13999')
self.ckpt_fine = os.path.join(self.resource_dir, 'train_logs_fine/best_model.ckpt-53999')
self.serverURL = serverURL
self.load_resource()
if self.phone == 'IOS':
import wda
self.client = wda.Client(self.serverURL)
self.s = self.client.session()
if self.debug:
if not os.path.exists(self.debug):
os.mkdir(self.debug)
def load_resource(self):
self.player = cv2.imread(os.path.join(self.resource_dir, 'player.png'), 0)
# network initization
self.net = JumpModel()
self.net_fine = JumpModelFine()
self.img = tf.placeholder(tf.float32, [None, 640, 720, 3], name='img')
self.img_fine = tf.placeholder(tf.float32, [None, 320, 320, 3], name='img_fine')
self.label = tf.placeholder(tf.float32, [None, 2], name='label')
self.is_training = tf.placeholder(np.bool, name='is_training')
self.keep_prob = tf.placeholder(np.float32, name='keep_prob')
self.pred = self.net.forward(self.img, self.is_training, self.keep_prob)
self.pred_fine = self.net_fine.forward(self.img_fine, self.is_training, self.keep_prob)
self.sess = tf.Session()
self.sess.run(tf.global_variables_initializer())
all_vars = tf.all_variables()
var_coarse = [k for k in all_vars if k.name.startswith('coarse')]
var_fine = [k for k in all_vars if k.name.startswith('fine')]
self.saver_coarse = tf.train.Saver(var_coarse)
self.saver_fine = tf.train.Saver(var_fine)
self.saver_coarse.restore(self.sess, self.ckpt)
self.saver_fine.restore(self.sess, self.ckpt_fine)
print('==== successfully restored ====')
def get_current_state(self):
if self.phone == 'Android':
os.system('adb shell screencap -p /sdcard/1.png')
os.system('adb pull /sdcard/1.png state.png')
elif self.phone == 'IOS':
self.client.screenshot('state.png')
if not os.path.exists('state.png'):
raise NameError('Cannot obtain screenshot from your phone! Please follow the instructions in readme!')
if self.debug:
shutil.copyfile('state.png', os.path.join(self.debug, 'state_{:03d}.png'.format(self.step)))
state = cv2.imread('state.png')
self.resolution = state.shape[:2]
scale = state.shape[1] / 720.
state = cv2.resize(state, (720, int(state.shape[0] / scale)), interpolation=cv2.INTER_NEAREST)
if state.shape[0] > 1280:
s = (state.shape[0] - 1280) // 2
state = state[s:(s+1280),:,:]
elif state.shape[0] < 1280:
s1 = (1280 - state.shape[0]) // 2
s2 = (1280 - state.shape[0]) - s1
pad1 = 255 * np.ones((s1, 720, 3), dtype=np.uint8)
pad2 = 255 * np.ones((s2, 720, 3), dtype=np.uint8)
state = np.concatenate((pad1, state, pad2), 0)
return state
def get_player_position(self, state):
state = cv2.cvtColor(state, cv2.COLOR_BGR2GRAY)
pos = multi_scale_search(self.player, state, 0.3, 10)
h, w = int((pos[0] + 13 * pos[2])/14.), (pos[1] + pos[3])//2
return np.array([h, w])
def get_target_position(self, state, player_pos):
feed_dict = {
self.img: np.expand_dims(state[320:-320], 0),
self.is_training: False,
self.keep_prob: 1.0,
}
pred_out = self.sess.run(self.pred, feed_dict=feed_dict)
pred_out = pred_out[0].astype(int)
x1 = pred_out[0] - 160
x2 = pred_out[0] + 160
y1 = pred_out[1] - 160
y2 = pred_out[1] + 160
if y1 < 0:
y1 = 0
y2 = 320
if y2 > state.shape[1]:
y2 = state.shape[1]
y1 = y2 - 320
img_fine_in = state[x1: x2, y1: y2, :]
feed_dict_fine = {
self.img_fine: np.expand_dims(img_fine_in, 0),
self.is_training: False,
self.keep_prob: 1.0,
}
pred_out_fine = self.sess.run(self.pred_fine, feed_dict=feed_dict_fine)
pred_out_fine = pred_out_fine[0].astype(int)
out = pred_out_fine + np.array([x1, y1])
return out
def get_target_position_fast(self, state, player_pos):
state_cut = state[:player_pos[0],:,:]
m1 = (state_cut[:, :, 0] == 245)
m2 = (state_cut[:, :, 1] == 245)
m3 = (state_cut[:, :, 2] == 245)
m = np.uint8(np.float32(m1 * m2 * m3) * 255)
b1, b2 = cv2.connectedComponents(m)
for i in range(1, np.max(b2) + 1):
x, y = np.where(b2 == i)
if len(x) > 280 and len(x) < 310:
r_x, r_y = x, y
h, w = int(r_x.mean()), int(r_y.mean())
return np.array([h, w])
def jump(self, player_pos, target_pos):
distance = np.linalg.norm(player_pos - target_pos)
press_time = distance * self.sensitivity
press_time = int(np.rint(press_time))
press_h, press_w = int(0.82*self.resolution[0]), self.resolution[1]//2
if self.phone == 'Android':
cmd = 'adb shell input swipe {} {} {} {} {}'.format(press_w, press_h, press_w, press_h, press_time)
print(cmd)
os.system(cmd)
elif self.phone == 'IOS':
self.s.tap_hold(press_w, press_h, press_time / 1000.)
def debugging(self):
current_state = self.state.copy()
cv2.circle(current_state, (self.player_pos[1], self.player_pos[0]), 5, (0,255,0), -1)
cv2.circle(current_state, (self.target_pos[1], self.target_pos[0]), 5, (0,0,255), -1)
cv2.imwrite(os.path.join(self.debug, 'state_{:03d}_res_h_{}_w_{}.png'.format(self.step, self.target_pos[0], self.target_pos[1])), current_state)
def play(self):
self.state = self.get_current_state()
self.player_pos = self.get_player_position(self.state)
if self.phone == 'IOS':
self.target_pos = self.get_target_position(self.state, self.player_pos)
print('CNN-search: %04d' % self.step)
else:
try:
self.target_pos = self.get_target_position_fast(self.state, self.player_pos)
print('fast-search: %04d' % self.step)
except UnboundLocalError:
self.target_pos = self.get_target_position(self.state, self.player_pos)
print('CNN-search: %04d' % self.step)
if self.debug:
self.debugging()
self.jump(self.player_pos, self.target_pos)
self.step += 1
time.sleep(1.5)
def run(self):
try:
while True:
self.play()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--phone', default='Android', choices=['Android', 'IOS'], type=str, help='mobile phone OS')
parser.add_argument('--sensitivity', default=2.045, type=float, help='constant for press time')
parser.add_argument('--serverURL', default='http://localhost:8100', type=str, help='ServerURL for wda Client')
parser.add_argument('--resource', default='resource', type=str, help='resource dir')
parser.add_argument('--debug', default=None, type=str, help='debug mode, specify a directory for storing log files.')
args = parser.parse_args()
# print(args)
AI = WechatAutoJump(args.phone, args.sensitivity, args.serverURL, args.debug, args.resource)
AI.run()