-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualizer.py
271 lines (252 loc) · 10.8 KB
/
visualizer.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
import subprocess
import os
import pickle
import re
import shutil
import tempfile
from game_controller import GameController
from textwrap import wrap
from PIL import Image, ImageDraw, ImageFont
from math import log10
from fnmatch import fnmatch
def get_image_format(data):
'''Gets image format from its data (bytearray).'''
# Source: getimageinfo.py (from code.google.com)
if data[:3] == b'GIF':
return '.gif'
elif data.startswith(b'\211PNG\r\n\032\n'):
return '.png'
elif data.startswith(b'\377\330'):
return '.jpeg'
class NoJuryStatesException(Exception):
'''Thrown if no jury states found.'''
pass
class VideoVisualizer:
'''Composes video file from game data.'''
def __init__(self, _framerate, _painter_obj, _file_mask, _working_dir='.',
_silent=False, _table_drawer=None):
'''
Constructor. Parameters:
* _framerate - framerate of video (max=24)
* _painter_obj - painter class
* _file_mask - file mask of GameController files (regular
expression)
* _working_dir - directory with GameController files
* _silent - if True, compiler will write nothing to screen
'''
self.painter = _painter_obj
self.table_drawer = _table_drawer
self.file_mask = _file_mask
self.working_dir = _working_dir
self.framerate = _framerate
self.inframe = int(48.0 / _framerate)
self.file_list = None
# Image filemask given to ffmpeg
self.imagefile_name = None
# Image size
self.size = None
self._paths = [os.path.abspath('.'), tempfile.mkdtemp()]
self._frame_count = 0
self.log = not _silent
self._tempfiles = []
self.ext = None
self.mode = None
def _create_tempfile(self, suffix=''):
self._tempfiles.append(tempfile.mkstemp(suffix))
return self._tempfiles[-1]
def _change_path(self, num):
if num == 1 and os.path.abspath('.') != self._paths[1]:
self._paths[0] = os.path.abspath('.')
os.chdir(self._paths[num])
def _create_frame(self, fname, number):
self._change_path(1)
begname = '{:09d}'.format(self._frame_count) + self.ext
shutil.copyfile(fname[1], begname)
for loop in range(number * self.inframe - 1):
os.link(begname, '{:09d}'.format(self._frame_count +
loop + 1) + self.ext)
self._frame_count += self.inframe * number
self._change_path(0)
os.close(fname[0])
os.remove(fname[1])
def _get_game_controller(self, filename):
'''Unpickles a game controller.'''
with open(filename, 'rb') as file:
return pickle.load(file)
def _generate_game_images(self, controller):
'''Generates frames for video.'''
if len(controller.jury_states) == 0:
if self.ext is None:
raise NoJuryStatesException('First GameController contains no '
'jury states - cannot visualize '
'an empty game.')
else:
print('One of GameControllers consists no jury states - the '
'game will be empty.')
# We need filenames with leading zeroes for ffmpeg
zero_count = int(log10(len(controller.jury_states)) + 1)
file_list = []
painter = self.painter(controller._players)
for ind, jstate in enumerate(controller.jury_states):
if self.log:
print(chr(13) + ' Generating game images... {}/{}'.format(
ind + 1, len(controller.jury_states)),
end=''
)
image = painter.paint(jstate)
self.ext = self.ext or get_image_format(image)
file_list.append(self._create_tempfile(self.ext))
with open(file_list[-1][1], 'wb') as f:
f.write(image)
if self.size is None or self.mode is None:
im = Image.open(file_list[-1][1])
self.size = im.size
self.mode = im.mode
return file_list
def _draw_tournament_status(self, round_id):
if self.table_drawer is not None:
tfile = self._create_tempfile(self.ext)
with open(tfile[1], 'wb') as f:
imagew = self.table_drawer(
os.path.join(self.working_dir, 'tournament.data'),
round_id,
self.mode,
self.ext
)
f.write(imagew)
im = Image.open(tfile[1])
ratio = min(self.size[0] / im.size[0], self.size[1] / im.size[1])
ratio_turned = min(self.size[1] / im.size[0],
self.size[0] / im.size[1])
if ratio_turned > ratio:
im = im.transpose(Image.ROTATE_270)
im = im.resize(
(
int(im.size[0] * ratio_turned),
int(im.size[1] * ratio_turned)
),
Image.ANTIALIAS
)
else:
im = im.resize((int(im.size[0] * ratio),
int(im.size[1] * ratio)), Image.ANTIALIAS)
res = Image.new(self.mode, self.size, 'white')
res.paste(im, ((self.size[0] - im.size[0]) // 2,
(self.size[1] - im.size[1]) // 2,
(self.size[0] + im.size[0]) // 2,
(self.size[1] + im.size[1]) // 2))
resfile = self._create_tempfile(self.ext)
res.save(resfile[1], self.ext[1:])
del im
os.close(tfile[0])
os.remove(tfile[1])
self._create_frame(resfile, 2 * self.framerate)
def generate_tournament_status(self, contr):
'''Generates a frame with a tournament status.'''
temptitle = self._create_tempfile(self.ext)
# Text displayed on the frame:
info = (wrap('Tournament: ' + str(contr.signature.tournament_id),
width=40) +
wrap('Round: ' + str(contr.signature.round_id) + (
'' if contr.signature.round_name is None else (
'(' + contr.signature.round_name + ')')), width=40) +
wrap('Series: ' + str(contr.signature.series_id),
width=40) +
wrap('Game: ' + str(contr.signature.game_id), width=40) +
[''] +
wrap('Players: ' + ', '.join(
map(
lambda x: x.bot_name + ' by ' + x.author_name,
contr._players
)
), width=40)
)
im = Image.new(self.mode, self.size, 'blue')
draw = ImageDraw.Draw(im)
cfsize = 100
done_once = False
# Here we should find the best fitting font size.
self._change_path(0)
while True:
font = ImageFont.truetype(os.path.join('fonts',
'Lucida Console.ttf'), cfsize,
encoding='unic')
textlen = max(map(lambda x: font.getsize(x)[0], info))
textheight = (font.getsize('T')[1] + 1) * len(info)
if (textlen < self.size[0] - 10 and textheight < self.size[1] - 10
and done_once):
break
done_once = True
cfsize = min(cfsize - 1, int(cfsize * min((self.size[0] - 10)
/ textlen, (self.size[1] - 10) / textheight)))
# Distance between corners of texts, starting position:
dy = font.getsize('T')[1] + 1
y = (self.size[1] - dy * len(info)) / 2
# Finally, we draw it:
for line in info:
width = font.getsize(line)[0]
draw.text(((self.size[0] - width) / 2, y), line, font=font,
fill=(255, 255, 255))
y += dy
im.save(temptitle[1])
# Compiling a video file:
self._create_frame(temptitle, 2 * self.framerate)
def compile(self, output_name):
'''
Compiles all games given by the specified filemask into one video file.
The file will be saved into the log folder.
'''
controllers = []
for filename in os.listdir(self.working_dir):
if fnmatch(filename, self.file_mask):
controllers.append(self._get_game_controller(os.path.join(
self.working_dir, filename)))
# The games should be given in the right order:
controllers = list(sorted(controllers))
vfile_list = []
prev_rnd = controllers[0].signature.round_id
for ind, controller in enumerate(controllers):
if self.log:
print('Processing game {}:{}:{}:{} ({} of {}):'.format(
controller.signature.tournament_id,
controller.signature.round_id,
controller.signature.series_id,
controller.signature.game_id, ind + 1, len(controllers)))
t = self._generate_game_images(controller)
if controller.signature.round_id > prev_rnd:
prev_rnd = controller.signature.round_id
self._draw_tournament_status(prev_rnd)
self.generate_tournament_status(controller)
if self.log:
print('\n Creating frames...')
for fname in t:
self._create_frame(fname, 1)
self._draw_tournament_status(prev_rnd + 1)
self._change_path(1)
print('Compiling the video file...')
try:
with open(os.devnull, 'w') as fnull:
subprocess.Popen(
'ffmpeg -i %09d{} -r 48 -s {}x{} {}'.format(
self.ext,
self.size[0],
self.size[1],
output_name
).split(),
stderr=fnull,
stdin=subprocess.PIPE).communicate('y\n'.encode() * 10)
self._change_path(0)
shutil.copyfile(os.path.join(self._paths[1], output_name),
os.path.join(self.working_dir, output_name))
except FileNotFoundError:
raise FileNotFoundError('You need to install ffmpeg to create'
' videos.')
print('Compiling finished.')
def __del__(self):
if self.log:
print('Cleaning up...')
for fname in self._tempfiles:
if os.path.exists(fname[1]):
os.close(fname[0])
os.remove(fname[1])
shutil.rmtree(self._paths[1])