Created
January 17, 2013 16:38
-
-
Save lifthrasiir/4557355 to your computer and use it in GitHub Desktop.
Score data extraction from Jukebeat screenshot (ripples theme only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PIL import Image | |
import numpy | |
#import colorsys | |
def parse(path): | |
im = Image.open(path).convert('RGB') | |
w, h = im.size | |
score = im.crop((w*137>>8, h*35>>8, w*254>>8, h*49>>8)).resize((350, 65), Image.BILINEAR) | |
score = numpy.asarray(score).astype('int16') | |
score = score[:,:,0] + score[:,:,1] + score[:,:,2] | |
score = ~((200 < score) & (score < 500)) | |
parts = [] | |
for i in xrange(7): | |
part = score[0:40,i*50+5:i*50+45] | |
#Image.fromarray((part * 255).astype('uint8')).save('tmp%d.png'%i) | |
part = abs(numpy.fft.rfft2(part)) | |
if part[0,0] != 0: part /= part[0,0] | |
parts.append(list(part[:4,:4].flatten())[1:]) | |
return parts | |
def train(): | |
samples = [ | |
' 0', 'IMG_0025.PNG', | |
' 22530', 'IMG_0026.PNG', | |
' 134561', 'IMG_0027.PNG', | |
' 208539', 'IMG_0028.PNG', | |
' 335966', 'IMG_0029.PNG', | |
' 424464', 'IMG_0030.PNG', | |
' 500945', 'IMG_0031.PNG', | |
' 604589', 'IMG_0032.PNG', | |
' 712864', 'IMG_0033.PNG', | |
' 713740', 'IMG_0034.PNG', | |
' 752044', 'IMG_0035.PNG', | |
' 813754', 'IMG_0036.PNG', | |
' 897738', 'IMG_0037.PNG', | |
' 177496', 'IMG_0038.PNG', | |
' 296786', 'IMG_0040.PNG', | |
' 413323', 'IMG_0041.PNG', | |
' 521473', 'IMG_0042.PNG', | |
' 757051', 'IMG_0043.PNG', | |
' 793602', 'IMG_0044.PNG', | |
' 849461', 'IMG_0045.PNG', | |
' 973712', 'jukebeat-ripples-ex.png', | |
' 942379', 'jukebeat-ripples-ex2.png', | |
] | |
keys = [(j,i) for i in (True, False) for j in ' 0123456789'] | |
keys.remove((' ', True)) | |
sums = dict((k, [0]*15) for k in keys) | |
numbers = dict((k, 0) for k in keys) | |
for path, actual in zip(samples[1::2], samples[0::2]): | |
yellow = int(actual) >= 700000 | |
for i, v in enumerate(parse(path)): | |
k = actual[i], yellow and actual[i] != ' ' | |
#if numbers[k] > 0: | |
# d = sum(abs(sums[k][j]/numbers[k] - v[j]) for j in xrange(15)) | |
# print d | |
numbers[k] += 1 | |
for j in xrange(15): sums[k][j] += v[j] | |
print 'AVERAGES = [' | |
for (c, yellow), n in sorted(numbers.items()): | |
print ' (%r, [%s]),' % (c, ', '.join('%5.3f' % (v/n) for v in sums[c, yellow])) | |
print ']' | |
#train() | |
AVERAGES = [ | |
(' ', [0.001, 0.001, 0.000, 0.002, 0.001, 0.001, 0.000, 0.002, 0.001, 0.001, 0.000, 0.002, 0.001, 0.001, 0.000]), | |
('0', [0.133, 0.140, 0.078, 0.206, 0.254, 0.103, 0.078, 0.173, 0.058, 0.060, 0.011, 0.047, 0.056, 0.023, 0.025]), | |
('0', [0.041, 0.009, 0.055, 0.018, 0.051, 0.005, 0.036, 0.028, 0.021, 0.012, 0.014, 0.015, 0.021, 0.016, 0.019]), | |
('1', [0.230, 0.110, 0.072, 0.113, 0.033, 0.047, 0.051, 0.086, 0.089, 0.037, 0.008, 0.018, 0.041, 0.039, 0.022]), | |
('1', [0.063, 0.016, 0.025, 0.020, 0.020, 0.014, 0.015, 0.020, 0.018, 0.014, 0.004, 0.014, 0.016, 0.010, 0.008]), | |
('2', [0.136, 0.163, 0.032, 0.190, 0.191, 0.110, 0.062, 0.129, 0.162, 0.031, 0.017, 0.056, 0.048, 0.056, 0.036]), | |
('2', [0.033, 0.019, 0.013, 0.018, 0.064, 0.013, 0.011, 0.018, 0.033, 0.018, 0.060, 0.036, 0.020, 0.038, 0.036]), | |
('3', [0.199, 0.150, 0.025, 0.130, 0.127, 0.094, 0.067, 0.205, 0.184, 0.053, 0.045, 0.031, 0.021, 0.034, 0.015]), | |
('3', [0.056, 0.035, 0.012, 0.025, 0.031, 0.021, 0.006, 0.054, 0.044, 0.024, 0.027, 0.027, 0.017, 0.020, 0.032]), | |
('4', [0.221, 0.144, 0.064, 0.182, 0.212, 0.067, 0.064, 0.069, 0.025, 0.134, 0.054, 0.033, 0.027, 0.037, 0.032]), | |
('4', [0.040, 0.025, 0.025, 0.050, 0.048, 0.028, 0.013, 0.020, 0.020, 0.017, 0.026, 0.031, 0.014, 0.024, 0.052]), | |
('5', [0.154, 0.212, 0.077, 0.084, 0.140, 0.075, 0.042, 0.343, 0.100, 0.053, 0.038, 0.034, 0.063, 0.043, 0.016]), | |
('5', [0.028, 0.021, 0.015, 0.029, 0.028, 0.015, 0.041, 0.072, 0.015, 0.009, 0.022, 0.023, 0.046, 0.018, 0.018]), | |
('6', [0.100, 0.183, 0.032, 0.148, 0.130, 0.135, 0.074, 0.300, 0.111, 0.111, 0.048, 0.035, 0.017, 0.032, 0.025]), | |
('6', [0.021, 0.020, 0.035, 0.035, 0.016, 0.015, 0.033, 0.048, 0.036, 0.022, 0.015, 0.011, 0.022, 0.022, 0.026]), | |
('7', [0.194, 0.077, 0.027, 0.236, 0.138, 0.146, 0.053, 0.218, 0.076, 0.067, 0.063, 0.091, 0.040, 0.020, 0.022]), | |
('7', [0.050, 0.016, 0.006, 0.049, 0.049, 0.008, 0.027, 0.032, 0.013, 0.016, 0.041, 0.025, 0.011, 0.011, 0.005]), | |
('8', [0.174, 0.221, 0.024, 0.202, 0.092, 0.149, 0.060, 0.193, 0.203, 0.034, 0.043, 0.104, 0.029, 0.031, 0.017]), | |
('8', [0.025, 0.026, 0.015, 0.029, 0.018, 0.014, 0.025, 0.031, 0.027, 0.027, 0.048, 0.056, 0.007, 0.026, 0.016]), | |
('9', [0.041, 0.228, 0.081, 0.170, 0.214, 0.090, 0.058, 0.228, 0.095, 0.087, 0.031, 0.097, 0.083, 0.028, 0.011]), | |
('9', [0.010, 0.018, 0.054, 0.036, 0.039, 0.018, 0.045, 0.043, 0.016, 0.014, 0.007, 0.029, 0.020, 0.018, 0.017]), | |
] | |
def detect(parts): | |
score = '' | |
for p in parts: | |
bestc = None | |
bestd = 9e9 | |
for c, v in AVERAGES: | |
d = sum(abs(a - b) for a, b in zip(v, p)) | |
if d < bestd: | |
bestd = d | |
bestc = c | |
score += bestc | |
return score | |
print detect(parse(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment