-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdb2txt.py
424 lines (373 loc) · 13.1 KB
/
db2txt.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python
import sys
import os
sys.path.insert(0, os.path.dirname(__file__) + '/..')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "monkeys.settings")
import django
django.setup()
from typer.models import TypedDie
import argparse
import ast
import re
import json
from PIL import Image, ImageDraw
'''
Each are 8x8
'''
warnings = []
warnings_field = {}
def romwf(rom, gcol, grow, fieldm, overrides):
absrow = grow * 8
abscol = gcol * 8
entries = sum(fieldm.values())
if 0:
if (gcol, grow) != (0, 0):
return
print fieldm
# Vote on all individual bits
# if 4/5 match consider it a win
def pervote1(fieldm):
ret = [None] * (8 * 8)
# If 4/5 of any individual bits consider it good
for biti in xrange(8 * 8):
freqa = {}
for view, freq in fieldm.iteritems():
bit = view[biti]
freqa[bit] = freqa.get(bit, 0) + freq
if len(freqa) == 1:
for k, v in fieldm.iteritems():
bit = k
elif len(freqa) == 2:
for k, v in fieldm.iteritems():
if v == entries - 1 or (okay() and v == entries - 2):
bit = k
elif v == 1:
continue
else:
return None
else:
return None
if bit not in '01':
return None
ret[biti] = bit
ret = ''.join(ret)
if len(ret) != 64:
print ret
raise ValueError()
return ret
def pervote1m(fieldm):
ret = [None] * (8 * 8)
# If 4/5 of any individual bits consider it good
for biti in xrange(8 * 8):
freqa = {}
for view, freq in fieldm.iteritems():
bit = view[biti]
freqa[bit] = freqa.get(bit, 0) + freq
if len(freqa) == 1:
for k, _v in freqa.iteritems():
bit = k
elif len(freqa) == 2:
bit = '?'
for k, v in fieldm.iteritems():
if v == entries - 1 or (okay() and v == entries - 2):
bit = k
else:
bit = '?'
ret[biti] = bit
ret = ''.join(ret)
if len(ret) != 64:
print ret
raise ValueError()
return ret
def warn(gcol, grow, msg):
print 'WARNING: %s, %s: %s' % (gcol, grow, msg)
warnings.append((gcol, grow))
warnings_field[(gcol, grow)] = pervote1m(fieldm)
override = overrides.get((gcol, grow), None)
if override and override != 'ok':
print 'NOTE: overriding %s, %s' % (gcol, grow)
field = overrides[(gcol, grow)]
else:
def okay():
return overrides.get((gcol, grow), None) == 'ok'
'''
This is probably too aggressive
Sometimes a user inputs a single ?
'''
if 0:
for k, v in fieldm.iteritems():
if '?' in k:
warn(gcol, grow, '? %s' % (fieldm.values(),))
return
# Vote on best field
# Easy case: all the same
field = None
pervote_res = field = pervote1(fieldm)
if len(fieldm) == 1:
field = fieldm.keys()[0]
elif pervote_res:
field = pervote_res
elif len(fieldm) == 2:
# Alarm if its not a single error
for k, v in fieldm.iteritems():
if v == entries - 1 or (okay() and v == entries - 2):
field = k
elif v == 1:
continue
elif not okay():
warn(gcol, grow, 'freq2 %s' % (fieldm.values(),))
return
elif len(fieldm) == 3:
# Alarm if its not two single errors
for k, v in fieldm.iteritems():
# These are suspicious
# maybe best to review them
if okay():
if v == entries - 2:
field = k
else:
warn(gcol, grow, 'freq3 %s' % (fieldm.values(),))
return
else:
warn(gcol, grow, 'freq %s' % (fieldm.values(),))
return
if '?' in field:
warn(gcol, grow, '? field %s' % (fieldm.values(),))
return
if not field:
warn(gcol, grow, 'bad field %s' % (fieldm.values(),))
return
field = field.strip().replace(' ', '').replace('\n', '')
for frow in xrange(8):
for fcol in xrange(8):
c = field[frow * 8 + fcol]
bit = {'0': 0, '1': 1}.get(c, None)
if bit is None:
warn(gcol, grow, 'char %s' % (c,))
return
#rom[(absrow + frow) * 32 + abscol + fcol] = bit
romw(rom, abscol + fcol, absrow + frow, bit)
def romw(rom, col, row, v):
'''Write ROM state'''
if col >= 32 * 8 or row >= 32 * 8:
raise ValueError('%s, %s' % (col, row))
rom[row * 32 * 8 + col] = v
def romr(rom, col, row):
'''Read ROM state'''
if col >= 32 * 8 or row >= 32 * 8:
raise ValueError('%s, %s' % (col, row))
try:
return rom[row * 32 * 8 + col]
except:
print('%s, %s' % (col, row))
raise
def isimg(td, img_want):
'''Is this for the image we are working on?'''
return str(td.dieImage.image).find(img_want) == 0
def get_dist(img_want):
'''
Get typed bits distribution per field
Get a distribution for a position via ret[(gcol, grow)][(fcol, frow)]
'''
dist = {}
matches = 0
for tdi, td in enumerate(TypedDie.objects.all()):
if not isimg(td, img_want):
if tdi % 256 == 0:
sys.stdout.write('.')
sys.stdout.flush()
continue
if tdi % 256 == 0:
sys.stdout.write('+')
sys.stdout.flush()
# sega_315-5571_xpol/sega_315-5571_xpol_01_09.png
fn = str(td.dieImage.image)
m = re.match(r'.*/(.*)_([0-9]*)_([0-9]*).png', fn)
if not m:
raise Exception()
#print fn
matches += 1
#img = m.group(1)
#if img != img_want:
# continue
col = int(m.group(2))
row = int(m.group(3))
#print row, col
if (col, row) in dist:
distv = dist[(col, row)]
else:
distv = {}
dist[(col, row)] = distv
typed = str(td.typedField).strip().replace('\n', '')
distv[typed] = distv.get(typed, 0) + 1
#print 'Matches: %d' % matches
if matches != (5 * 32 * 32):
raise ValueError()
return dist
def dist_by_bits(dist):
'''Return a per bit instead of per solution dist'''
ret = {}
for solution, frequency in dist.iteritems():
fi = 0
for frow in xrange(8):
for fcol in xrange(8):
pos_freqs = ret.setdefault((fcol, frow), {})
bitchar = solution[fi]
pos_freqs[bitchar] = pos_freqs.setdefault(bitchar, 0) + frequency
fi += 1
return ret
def im_roi(icol, irow):
'''Get an ROI for just one bit in a task image'''
if icol >= 8 or irow >= 8:
raise ValueError()
imwh = 14
xmin = 1 + 14 * icol
ymin = 1 + 14 * irow
# The crop rectangle, as a (left, upper, right, lower)-tuple.
crop = (xmin, ymin, xmin + imwh - 1, ymin + imwh - 1)
#return im_full.crop(crop)
return crop
def save_txt(rom, f):
'''Save ROM state to file as .txt representing image layout'''
# FIXME: generate these
cols = 32 * 8
rows = 32 * 8
ret = ''
for row in xrange(rows):
for col in xrange(cols):
if col % 8 == 0 and col:
f.write(' ')
f.write(str(romr(rom, col, row)))
f.write('\n')
if row % 8 == 0 and row:
f.write('\n')
return ret
# XXX: too many open files without ulimit
# maybe change to gen_images
# Load images
load_images_cache = {}
def load_images(img_name, toload):
for (gcol, grow) in toload:
load_images_cache[(gcol, grow)] = get_image(img_name, gcol, grow)
return load_images_cache
def get_image(img_name, gcol, grow):
fn = 'static/%s/%s_%02d_%02d.png' % (img_name, img_name, gcol, grow)
return Image.open(fn)
def gen_warning_images(out_dir, img_name):
print 'Generating warning images'
images = load_images(img_name, warnings)
# Mask
#print images.keys()
#print warnings_field.keys()
for (gcol, grow), field in warnings_field.iteritems():
im = images[(gcol, grow)]
d = ImageDraw.Draw(im)
for frow in xrange(8):
for fcol in xrange(8):
c = field[frow * 8 + fcol]
if c not in '01':
roi = im_roi(fcol, frow)
d.rectangle(roi, fill=None, outline=(0xFF, 0xFF, 0))
# Save
for (gcol, grow), im in images.iteritems():
im.save(os.path.join(out_dir, '%02d_%02d_m.png' % (gcol, grow)))
def gen_cv_images(meta_dir, img_name, dist, rom):
print 'Generating CV images'
#images = load_images(img_name, dist)
meta_bits = {}
for (gcol, grow), field in dist.iteritems():
field_bits = dist_by_bits(field)
#im = images[(gcol, grow)]
im = get_image(img_name, gcol, grow)
for frow in xrange(8):
arow = grow * 8 + frow
for fcol in xrange(8):
acol = gcol * 8 + fcol
roi = im_roi(fcol, frow)
if meta_dir:
bitfn = "%02dgc-%02dgr_%02dfc-%02dfr.png" % (gcol, grow, fcol, frow)
meta_bit = {
# Global absolute coordinates
'col': acol,
'row': arow,
# Global tile coordinates
'gcol': gcol,
'grow': grow,
# Field (within a tile) coordinates
'frow': frow,
'fcol': fcol,
# Answer frequency distribution like
# {'0': 3, '1': 1, '?': 1}
"dist": field_bits[(fcol, frow)],
"best": romr(rom, acol, arow),
}
#print meta_bit
imc = im.crop(roi)
imc.save(os.path.join(meta_dir, "bit", bitfn))
imc.close()
meta_bits[bitfn] = meta_bit
im.close()
return meta_bits
def run(img_name, overrides=None, fn_out=None, meta_dir=None):
if not overrides:
overrides = {}
# Reassemble the ROM
print 'Assemble test'
# 32 x 32, 8 x 8
# row major order
# Contains only 0/1 as integers
rom = [None] * 8192 * 8
print 'Querying...'
dist = get_dist(img_name)
print ' done'
if meta_dir:
if not os.path.exists(meta_dir):
os.mkdir(meta_dir)
if not os.path.exists(meta_dir + '/bit'):
os.mkdir(meta_dir + '/bit')
meta = {
"meta": {
"source": "django-monkeys/db2txt",
"override": str(overrides),
"img": img_name,
},
#"bit": meta_bits
}
for (col, row), fields in sorted(dist.iteritems()):
romwf(rom, col, row, fields, overrides=overrides)
# warning images trashes, run first
if meta_dir:
meta['bit'] = gen_cv_images(meta_dir, img_name, dist, rom)
if warnings:
print 'Warning template (%d)' % len(warnings)
for (gcol, grow), field in sorted(warnings_field.iteritems()):
#if (gcol, grow) != 0, 0:
# continue
print ' (%d, %d): """' % (gcol, grow)
for frow in xrange(8):
base = frow * 8
print ' %s' % (field[base:base+8],)
print ' """,'
out_dir = 'db2bin/%s' % img_name
os.path.exists(out_dir) or os.mkdir(out_dir)
gen_warning_images(out_dir, img_name)
raise Exception("Resolve warnings before continuing")
if not fn_out:
fn_out = '%s_cs.txt' % img_name
print 'Writing %s' % fn_out
save_txt(rom, open(fn_out, 'w'))
if meta_dir:
json.dump(meta, open(os.path.join(meta_dir, 'meta.json'), 'w'),
sort_keys=True, indent=4, separators=(',', ': '))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Extract ROM .bin from monkey DB')
parser.add_argument('--verbose', '-v', action='store_true', help='verbose')
parser.add_argument('--cf', '-c', help='Correction file')
parser.add_argument('--meta', '-m', help='Output CV metadata training file to given directory')
parser.add_argument('image', help='Image file to process (ex: sega_315-5571_xpol)')
parser.add_argument('fn_out', nargs='?', default=None, help='Output file name')
args = parser.parse_args()
overrides = None
if args.cf:
overrides = ast.literal_eval(open(args.cf, 'r').read())
run(img_name=args.image, overrides=overrides, fn_out=args.fn_out, meta_dir=args.meta)