-
Notifications
You must be signed in to change notification settings - Fork 2
/
fumenizer.py
executable file
·179 lines (155 loc) · 4.89 KB
/
fumenizer.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
#!/usr/bin/env python
import cv2
import numpy as np
import pyperclip
import fumen
import json
import sys
import os
import argparse
try:
import configparser
except ImportError:
import ConfigParser as configparser
def buildMatrix(imgFilename, threshold, tgm1):
# image needs to be the exact playfield without the border
# todo: recognize playfield automagically by looking for squares
field = cv2.imread(imgFilename)
hsv = cv2.cvtColor(field, cv2.COLOR_BGR2HSV)
height, width = field.shape[:2]
# for i in range(height):
# for j in range(width):
# hue = hsv[i][j][0]
# saturation = hsv[i][j][1]
# value = hsv[i][j][2]
# hsv[i, j] = [hue, saturation, 100]
lower = np.array([0,150,85])
upper = np.array([180,255,255])
mask = cv2.inRange(hsv, lower, upper)
if(tgm1):
field = cv2.bitwise_and(field, field, mask = mask)
# # uncomment for preview for the tgm1 code
# cv2.imshow('f', field)
# cv2.waitKey()
block_height = height / 20.
block_width = width / 10.
end = False
# matrix is a nested list that represents the playfield
# holes are 0's, blocks are 1's
# todo: recognize color
matrix = []
for row in range(20):
line = []
for col in range(10):
y1 = int(round(height - (row + 1) * block_height))
y2 = int(round(height - row * block_height))
x1 = int(round(col * block_width))
x2 = int(round((col + 1) * block_width))
part = field[y1:y2, x1:x2]
thresh = cv2.cvtColor(part, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(thresh, threshold, 255, cv2.THRESH_BINARY)
#sometimes a little tinkering with the 2nd parameter (aka the threshold value)
#of threshold is required (e.g. going higher than 20)
temp_height, temp_width = thresh.shape[:2]
#blocks will now be predominantly white squares, so we're counting
#black and white pixels
black = 0
white = 0
for i in range(temp_height):
for j in range(temp_width):
if thresh[i, j] == 0:
black += 1
else:
white += 1
#tgm1 mode is still kind of hacky and needs improvement
if tgm1:
if white > 60:
line.append(1)
else:
line.append(0)
else:
if white > black:
line.append(1)
else:
line.append(0)
#uncomment this part to go through the coordinates one by one
#cv2.imshow('b', part)
#cv2.imshow('c', thresh)
#ch = 0xFF & cv2.waitKey()
#if ch == 27:
# end = True
# break
matrix.append(line)
if end:
break
return matrix
def exportMatrix(matrix, filename):
with open(filename, 'w') as f:
json.dump(matrix, f)
def fumenize(matrix, showPreview):
#prepare result image
blank = np.zeros((200, 100, 3), np.uint8)
frame = fumen.Frame()
cell = 10
for row in range(20):
for col in range(10):
if matrix[row][col]:
#draw gray rectangles where blocks are
cv2.rectangle(blank, (col * 10 + 2, 190 - row * 10),
(col * 10 + 5 + 2, 190 - row * 10 + 5), (105, 105, 105), cv2.FILLED)
if matrix[19 - row][col]:
frame.field[cell] = 8
else:
frame.field[cell] = 0
cell += 1
fumen_url = fumen.make([frame], 0)
print(fumen_url)
pyperclip.copy(fumen_url)
with open("last_field", 'w') as f:
f.write("############\n")
for row in range(20):
f.write('#')
for col in range(10):
if matrix[19-row][col]:
f.write('|')
else:
f.write(' ')
f.write('#\n')
f.write("############\n")
if showPreview:
#show result image
cv2.imshow('d', blank)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
# Create default settings file
if not os.path.isfile('fumenizer.ini'):
f = open("fumenizer.ini", "w")
f.write("[settings]\n")
f.write("#for TGM a threshold around 20 is recommended\n")
f.write("threshold=20\n")
f.write("preview=1\n")
f.write("tgm1=0\n")
f.close()
# Read our config file with a set of defaults.
config = configparser.ConfigParser()
#config['settings'] = {'threshold' : 20, 'preview' : True, 'tgm1' : False}
config.add_section("settings")
config.set("settings", "threshold", "20")
config.set("settings", "preview", "True")
config.set("settings", "tgm1", "False")
config.read('fumenizer.ini')
# Parse our command line arguments. These will take precedence over config file settings.
argParser = argparse.ArgumentParser(description="Fumenizer Settings")
argParser.add_argument('imageFile', help='Fumenize this image')
argParser.add_argument('-t', dest='threshold', action='store',
type=float, default=float(config.get("settings", "threshold")),
help='Threshold tolerance')
argParser.add_argument('-p', dest='preview', action='store_true',
help='Show preview')
argParser.add_argument('-1', dest='tgm1', action='store_true',
help='TGM1 Compatibility')
args = argParser.parse_args()
matrix = buildMatrix(args.imageFile, args.threshold, args.tgm1 or config.getboolean('settings', 'tgm1'))
fumenize(matrix, args.preview or config.getboolean('settings', 'preview'))