forked from appcell/OverwatchDataAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.py
327 lines (263 loc) · 10.2 KB
/
frame.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
import cv2
import numpy as np
from skimage import measure
import overwatch as OW
import gui as Gui
from utils import image as ImageUtils
from player import Player
from killfeed import Killfeed
class Frame(object):
"""Class of a Frame object.
Retrieves and stores all info captured in one frame.
Attributes:
is_valid: If the frame itself is valid, i.e. during a match
players: List of Player instances in current frame, usually 12
killfeeds: List of Killfeed instances in current frame, 6 at max
image: image of the frame, resized to 720p
time: time of frame in video, in seconds
game: the Game object who fathers all frames
"""
def __init__(self, frame_image, frame_time, game):
"""Initialize a Frame object.
Author:
Appcell
Args:
frame_image: the image cropped from video for this frame
frame_time: time of frame in video, in seconds
game: the Game object which controls all frames
Returns:
None
"""
self.is_valid = False
self.players = []
self.killfeeds = []
self.image = ImageUtils.resize(frame_image, 1280, 720)
self.time = frame_time
self.game = game
if self.game.ult_colors is None:
self.game.set_ult_colors(self)
# Gui.gui_instance.show_progress(self.time)
print self.time
self.get_players()
self.get_killfeeds()
self.validate()
self.free()
def free(self):
"""Free RAM by removing images from the Frame instance.
Done after analysis.
Author:
Appcell
Args:
None
Returns:
None
"""
del self.image
def get_players(self):
"""Get all players info in this frame.
Author:
Appcell
Args:
None
Returns:
None
"""
for i in range(0, 12):
player = Player(i, self)
self.players.append(player)
def get_team_colors_from_image(self):
"""Get team colors from this frame.
Author:
Appcell
Args:
None
Returns:
None
"""
pos = OW.get_team_color_pick_pos()[self.game.game_type]
return {
"left": self.image[pos[0][0], pos[0][1]],
"right": self.image[pos[1][0], pos[1][1]]
}
def get_team_colors(self):
if self.game.team_colors is not None:
return self.game.team_colors
else:
return self.get_team_colors_from_image()
def get_ult_colors_from_image(self):
"""Get ultimate charge number colors from this frame.
Author:
Rigel
Args:
None
Returns:
@ult_color: array of int, -1: white number, 1: black number
"""
left_pre_pos = OW.get_ult_charge_color_pre_pos(True)[self.game.game_type]
left_pre_image = ImageUtils.rgb_to_gray(ImageUtils.crop(self.image, left_pre_pos))
left_shear = ImageUtils.shear(left_pre_image, OW.get_tf_shear(True)[self.game.game_type])
left_pos = OW.get_ult_charge_color_pos(True)[self.game.game_type]
left_image = ImageUtils.crop(left_shear, left_pos)
left_image_g = ImageUtils.contrast_adjust_log(left_image, OW.ULT_ADJUST_LOG_INDEX)
left_bin = ImageUtils.binary_otsu(left_image_g)
right_pre_pos = OW.get_ult_charge_color_pre_pos(False)[self.game.game_type]
right_pre_image = ImageUtils.rgb_to_gray(ImageUtils.crop(self.image, right_pre_pos))
right_shear = ImageUtils.shear(right_pre_image, OW.get_tf_shear(False)[self.game.game_type])
right_pos = OW.get_ult_charge_color_pos(False)[self.game.game_type]
right_image = ImageUtils.crop(right_shear, right_pos)
right_image_g = ImageUtils.contrast_adjust_log(right_image, OW.ULT_ADJUST_LOG_INDEX)
right_bin = ImageUtils.binary_otsu(right_image_g)
return {
"left": np.sign(2 * np.sum(left_bin) - np.size(left_bin)),
"right": np.sign(2 * np.sum(right_bin) - np.size(right_bin))
}
def get_ult_colors(self):
if self.game.ult_colors is not None:
return self.game.ult_colors
else:
return self.get_ult_colors_from_image()
def get_killfeeds(self):
"""Get killfeed info in this frame.
Author:
Appcell
Args:
None
Returns:
None
"""
for i in range(6):
killfeed = Killfeed(self, i)
if killfeed.is_valid is True:
if self.game.frames and self.game.frames[-1].killfeeds:
last_killfeed = self.game.frames[-1].killfeeds[-1]
if killfeed == last_killfeed:
self.killfeeds.append(killfeed)
break
else:
self.killfeeds.append(killfeed)
else:
self.killfeeds.append(killfeed)
elif i >= 1:
break
self.killfeeds.reverse()
def validate(self):
"""Validate this frame, set up Game obj if it's not set.
Validation by:
1) Test if there's any players detectable. If none, frame is invalid
2) Test if top-right corner is white. If not, frame is invalid
If frame is valid and Game info (i.e. team colors, avatars) are not
set, set them up.
Author:
Appcell
Args:
None
Returns:
None
"""
flag = False
for player in self.players:
if player.is_dead is False:
flag = True
if flag == False:
self.is_valid = False
return
else:
self.is_valid = True
validation_roi = ImageUtils.crop(self.image,
OW.FRAME_VALIDATION_POS[self.game.game_type])
std = np.max([np.std(validation_roi[:, :, 0]),
np.std(validation_roi[:, :, 1]),
np.std(validation_roi[:, :, 2])])
mean = [np.mean(validation_roi[:, :, 0]),
np.mean(validation_roi[:, :, 1]),
np.mean(validation_roi[:, :, 2])]
if std < OW.FRAME_VALIDATION_COLOR_STD[self.game.game_type] \
and np.mean(mean) > OW.FRAME_VALIDATION_COLOR_MEAN[self.game.game_type] \
and flag is True:
self.is_valid = True
else:
self.is_valid = False
return
replay_icon = ImageUtils.crop(
self.image, OW.get_replay_icon_pos()[self.game.game_type])
replay_icon_preseason = ImageUtils.crop(
self.image, OW.get_replay_icon_preseason_pos()[self.game.game_type])
max_val = measure.compare_ssim(
replay_icon, self.game.replay_icon_ref, multichannel=True)
max_val_preseason = measure.compare_ssim(
replay_icon_preseason, self.game.replay_icon_ref, multichannel=True)
# TODO: another situation: after replay effect there might be a blue
# rectangle remaining on screen.
max_val = max_val if max_val > max_val_preseason else max_val_preseason
if max_val < OW.FRAME_VALIDATION_REPLAY_PROB[self.game.game_type]:
self.is_valid = True
else:
self.is_valid = False
return
if self.is_valid is True and self.game.team_colors is None:
self.game.set_team_colors(self)
self.game.avatars_ref = self._get_avatars_before_validation()
def _get_avatars_before_validation(self):
"""Get fused avatar icons for this frame.
Used when Game info is not set. First pick team colors from current
frame, then overlay transparent icons on them and form new avatars.
Color of background image corresponds to team color of current player.
Author:
Appcell
Args:
None
Returns:
A dict of all avatar icons fused
"""
team_colors = self.get_team_colors()
avatars_left_ref = {}
avatars_small_left_ref = {}
avatars_right_ref = {}
avatars_small_right_ref = {}
# Create background image with team color
bg_image_left = ImageUtils.create_bg_image(
team_colors["left"], OW.AVATAR_WIDTH_REF, OW.AVATAR_HEIGHT_REF)
bg_image_right = ImageUtils.create_bg_image(
team_colors["right"], OW.AVATAR_WIDTH_REF, OW.AVATAR_HEIGHT_REF)
avatars_ref = OW.get_avatars_ref()
# Overlay transparent reference avatar on background
for (name, avatar_ref) in avatars_ref.iteritems():
avatars_left_ref[name] = ImageUtils.overlay(
bg_image_left, avatar_ref)
avatars_small_left_ref[name] = ImageUtils.resize(
ImageUtils.overlay(bg_image_left, avatar_ref), 33, 26)
avatars_right_ref[name] = ImageUtils.overlay(
bg_image_right, avatar_ref)
avatars_small_right_ref[name] = ImageUtils.resize(
ImageUtils.overlay(bg_image_right, avatar_ref), 33, 26)
return {
"left": avatars_left_ref,
"left_small": avatars_small_left_ref,
"right": avatars_right_ref,
"right_small": avatars_small_right_ref
}
def get_avatars(self, index):
"""Get fused avatar icons for a player.
If Game info is set, retrieves avatars from Game obj.
If not, generate avatars from frame itself.
Author:
Appcell
Args:
Index: Index of player.
Returns:
A dict of avatars.
"""
all_avatars = {}
if self.game.team_colors is not None:
all_avatars = self.game.avatars_ref
else:
all_avatars = self._get_avatars_before_validation()
if index < 6:
return {
"normal": all_avatars['left'],
"small": all_avatars['left_small']
}
return {
"normal": all_avatars['right'],
"small": all_avatars['right_small']
}