-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtg-card-reader.py
executable file
·264 lines (214 loc) · 7.94 KB
/
mtg-card-reader.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
#!/usr/bin/env python3
import os, re, time
import itertools
import arrow, click
import boto3, scrython
import serial
import serial.tools.list_ports
BAUD = 921600
CAPTURE_FOLDER = './captures/'
CARD_EXT = '.jpg'
CARD_UNKNOWN = 'X'
SERIAL_DELIMITER = "|"
def rename_file(path, name, debug=False):
if not os.path.isfile(path):
if debug:
print('File %s does not exist, skipping.' % path)
return False
folder = os.path.dirname(path)
card_count = [f for f in os.listdir(folder) if re.search(r'[0-9]+-' + name + '-[0-9]+' + CARD_EXT, f)]
name = '-' + name + '-{0:03d}'.format(len(card_count) + 1)
new_filename = re.sub(CARD_EXT, name + CARD_EXT, path)
#print('Renaming %s to % s' % (path, new_filename))
os.rename(path, new_filename)
def analyze_card(path, debug=False):
if debug:
print('\nStart analyzing card')
start_analyze = arrow.now()
with open(path, 'rb') as content_file:
image = content_file.read()
client = boto3.client('rekognition')
response = client.detect_text(
Image={
'Bytes': image,
}
)
if response['TextDetections']:
lines = [line for line in response['TextDetections'] if line['Type'] == 'LINE']
if lines:
name_found = lines[0]['DetectedText']
colors = [CARD_UNKNOWN]
colors_text = []
price = None
error = None
# edition = lines[-1]['DetectedText']
# if debug:
# click.echo('Name found: ', nl=False)
# click.secho(name_found, fg='yellow')
try:
card = scrython.cards.Named(exact=name_found)
name = card.name()
price = card.currency('usd')
colors = card.color_identity()
rename_file(path, name, debug)
colors_mapping = {
'U': {'bg': 'blue'},
'R': {'bg': 'red'},
'W': {'bg': 'white', 'fg': 'black'},
'G': {'bg': 'green'},
'B': {'bg': 'black'},
}
colors_text = [click.style(c, **colors_mapping[c]) for c in colors]
except Exception as e:
error = str(e)
name = 'Card not found'
if debug:
click.secho('Card not found: ')
click.secho(error, fg='red')
fg = 'green' if error is None else 'red'
printRow((name_found, (name, fg), '[{}]'.format(', '.join(colors)), price))
duration = (arrow.now() - start_analyze).seconds
if debug:
print('Analyze done in %is.' % duration)
return {
'name': name,
'colors': colors,
'error': error
}
else:
click.echo('%-20s' % (click.style("No lines found.", fg='red')))
raise Exception('No lines found.')
else:
click.echo('%-20s' % (click.style("No text found.", fg='red')))
raise Exception('No text found.')
def capture_card(port, path, debug=False):
if debug:
print('\nWaiting capture...')
# Loop over bytes from Arduino for a single image
written = False
prevbyte = None
done = False
start_capturing = None
while not done:
# Read a byte from Arduino
currbyte = port.read(1)
# If we've already read one byte, we can check pairs of bytes
if prevbyte:
# Start-of-image sentinel bytes: write previous byte to temp file
if ord(currbyte) == 0xd8 and ord(prevbyte) == 0xff:
if debug:
print('Start capturing.')
start_capturing = arrow.now()
# Open output file
outfile = open(path, 'wb')
outfile.write(prevbyte)
written = True
# Inside image, write current byte to file
if written:
outfile.write(currbyte)
# End-of-image sentinel bytes: close temp file and display its contents
if written and ord(currbyte) == 0xd9 and ord(prevbyte) == 0xff:
outfile.close()
done = True
# Track previous byte
prevbyte = currbyte
duration = (arrow.now() - start_capturing).seconds
if debug:
print('Capture done in %is.' % duration)
return True
# helpers --------------------------------------------------------------------------
def sendbyte(port, value):
port.write(bytearray([value]))
columns_padding = (28, 28, 20, 10)
truncated_string = '...'
def printRow(row):
for i, col in enumerate(row):
max_length = columns_padding[i]
text = col
fg = None
if type(col) == tuple:
text = col[0]
fg = col[1]
text = (text[:max_length - len(truncated_string)] + truncated_string) if len(text) > max_length else text
text = text + ' ' * (max_length - len(text))
# print('%s|%s|%s' % (max_length, len(text), padding))
click.secho(text, fg=fg, nl=False)
print('')
# main --------------------------------------------------------------------------
cards = {
'success': [],
'error': []
}
@click.command()
@click.option('--debug', default=False, is_flag=True)
def main(debug):
DEBUG = debug
# Find Arduino port
arduino_ports = [
p.device
for p in serial.tools.list_ports.comports()
if p.manufacturer is not None and 'Arduino' in p.manufacturer
]
if not arduino_ports:
raise IOError("No Arduino found")
if len(arduino_ports) > 1:
print('Multiple Arduinos found - using the first')
# Open connection to Arduino with a timeout of two seconds
port = serial.Serial(arduino_ports[0], BAUD, timeout=None)
# Report acknowledgment from camera
ack = port.readline().decode()
print(ack)
if 'error' in ack.lower():
exit()
time.sleep(0.2)
CAPTURES = CAPTURE_FOLDER + arrow.now().format('YYYY-MM-DD HH:mm:ss') + '/'
if not os.path.exists(CAPTURES):
os.makedirs(CAPTURES)
if debug:
print('Creating capture folder: %s' % CAPTURES)
index = 1
printRow(('TEXT', 'CARD', 'COLORS', 'USD'))
while(1):
path = CAPTURES + '{0:03d}'.format(index) + CARD_EXT
capture_card(port, path, debug)
try:
card_meta = analyze_card(path, debug)
except Exception as e:
if debug:
print(str(e))
card_meta = {
"name": "Card not found",
"colors": [CARD_UNKNOWN],
"error": "Card not found",
}
try:
color = card_meta['colors']
except Exception as e:
color = 'X'
if debug:
print('Sending %s' % str.encode(card_meta['name'] + SERIAL_DELIMITER))
port.write(str.encode(card_meta['name'] + SERIAL_DELIMITER))
if debug:
print('Sending %s' % str.encode("".join(color) + SERIAL_DELIMITER))
port.write(str.encode("".join(color) + SERIAL_DELIMITER))
if (card_meta['error']):
cards['error'].append(card_meta)
else:
cards['success'].append(card_meta)
index = index + 1
def printStatus():
click.secho('---------------------------------------------', fg='yellow')
click.echo('Card scanned: ', nl=False)
click.secho(str(len(cards['success'])), fg='green')
if len(cards['error']):
click.echo('Card with errors: ', nl=False)
click.secho(str(len(cards['error'])), fg='red')
click.echo('Card list:')
for card, group in itertools.groupby(cards['success'], key=lambda x:x['name']):
click.echo(' {}'.format(len(list(group))), nl=False)
click.secho(' {}'.format(card), fg='cyan')
click.secho('---------------------------------------------', fg='yellow')
import atexit
atexit.register(printStatus)
if __name__ == '__main__':
main()