-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore_board.py
259 lines (188 loc) · 6.71 KB
/
score_board.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
import cv2
import numpy as np
import pickle
import sys
import os
from collections import Counter, defaultdict, OrderedDict
import time
from tqdm import tqdm
import torch
import torchvision.models as models
import torchvision.transforms as transforms
import torch.nn as nn
from PIL import Image
RATIO = 79.5 / 52.5
WIDTH = 1500
HEIGHT = int(WIDTH / RATIO) # 990 if width = 1500
TRAIN_WIDTH = 100
TRAIN_HEIGHT = 100
def predict(image, model):
label_names = ["red", "green", "blue", "black", "yellow", "none"]
preprocess = transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
input_img = preprocess(img).unsqueeze(0)
output = model(input_img)
pred = torch.argmax(output[0]).item()
return label_names[pred]
def extract(image, pts, target_width, target_height):
pts_src = np.array(pts)
pts_dst = np.array(
[[0, 0], [target_width-1, 0],
[target_width-1, target_height-1], [0, target_height-1]])
# top left, top right, bottom right, bottom left
h, status = cv2.findHomography(pts_src, pts_dst)
im_out = cv2.warpPerspective(image, h, (target_width, target_height))
return im_out
# Accepts a cropped image
def score(image):
tracks = pickle.load( open( "tracks.p", "rb" ) )
print(f"{len(tracks)} tracks")
model = models.resnet18()
num_ftrs = model.fc.in_features
# model.fc = nn.Linear(num_ftrs, 6)
fc = nn.Sequential(OrderedDict([
('fc1', nn.Linear(num_ftrs,100)),
('relu', nn.ReLU()),
('dropout', nn.Dropout(.2)),
('fc2', nn.Linear(100,6)),
('output', nn.LogSoftmax(dim=1))
]))
model.fc = fc
model.load_state_dict(torch.load("ticket_to_ride_model.pt", map_location='cpu'))
model.eval()
# labeled_boxes = []
labeled_tracks = []
score_map = {
1: 1,
2: 2,
3: 4,
4: 7,
5: 10,
6: 15
}
color_scores = defaultdict(int)
print("Scoring...")
start = time.time()
for i, track in enumerate(tqdm(tracks)):
colors = Counter()
for box in track:
pts = np.array(box, np.int32).reshape((-1,1,2))
box_img = extract(image, box, TRAIN_WIDTH, TRAIN_HEIGHT)
pred_color = predict(box_img, model)
# labeled_boxes.append((pts, pred_color))
colors[pred_color] += 1
color = colors.most_common(1)[0][0]
# if top 2 are tied and one of them is none, use the other color.
if len(colors) >= 2:
top_2 = colors.most_common(2)
if color == 'none' and top_2[0][1] == top_2[1][1]:
color = top_2[1][0]
if color != 'none':
color_scores[color] += score_map[len(track)]
labeled_tracks.append((track, color))
print(f"Finished in {time.time() - start} seconds.")
print("====== Scores ======")
place = 0
for color, score in sorted(color_scores.items(), key=lambda x: -x[1]):
print(f"{place + 1}. {color.title()}: {score}")
place += 1
colors = {
"red": (0,0,255),
"green": (0,255,0),
"blue": (255,0,0),
"black": (0,0,0),
"yellow": (0,255,255),
"none": (255,255,255)
}
# for pts, color in labeled_boxes:
# cv2.polylines(image,[pts],True, colors[color], 3)
for track, color in labeled_tracks:
for box in track:
pts = np.array(box, np.int32).reshape((-1,1,2))
cv2.polylines(image,[pts],True, colors[color], 3)
return image, color_scores
img_path = sys.argv[1]
image = cv2.imread(img_path)
h, w, _ = image.shape
target_width = 1000
image = cv2.resize(image, (target_width, int(target_width * (h/w))))
source_image = image.copy()
mouse_coords = []
def create_point(x, y):
global mouse_coords, image
if len(mouse_coords) >= 4:
print("4 points already placed.")
return
cv2.circle(image,(x,y),3,(255,255,0),-1)
mouse_coords.append([x, y])
cv2.imshow('ResNet to Ride',image)
def delete_point():
global mouse_coords, image
if len(mouse_coords) == 0:
print("No points to delete.")
return
last_x, last_y = mouse_coords.pop()
cv2.circle(image,(last_x, last_y), 3, (0,0,0), -1)
cv2.imshow('ResNet to Ride',image)
def on_mouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
create_point(x, y)
h, w, _ = image.shape
image = cv2.rectangle(image, (30, h//2- 20), (w - 30, h//2+65), (0,0,0), -1)
image = cv2.putText(image,
'Click on each corner of the board. Please try to be as accurate as possible.',
(30, h//2), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255) , 2, cv2.LINE_AA)
image = cv2.putText(image,
'Press \'d\' to undo a corner point',
(30, h//2+30), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255) , 2, cv2.LINE_AA)
image = cv2.putText(image,
'Press \'n\' after selecting corners to score the board.',
(30, h//2+60), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255) , 2, cv2.LINE_AA)
cv2.imshow('ResNet to Ride', image)
cv2.setMouseCallback('ResNet to Ride', on_mouse)
while True:
cv2.imshow('ResNet to Ride',image)
k = cv2.waitKey(0) & 0xFF
if k == 27:
break
elif k == ord('n'):
print("Cropping board.")
pts = mouse_coords[-4:]
sort_x = sorted(pts, key=lambda x: x[0])
if sort_x[0][1] < sort_x[1][1]:
top_left = sort_x[0]
bottom_left = sort_x[1]
else:
top_left = sort_x[1]
bottom_left = sort_x[0]
if sort_x[-1][1] < sort_x[-2][1]:
top_right = sort_x[-1]
bottom_right = sort_x[-2]
else:
top_right = sort_x[-2]
bottom_right = sort_x[-1]
ordered_pts = [top_left, top_right, bottom_right, bottom_left]
cropped = extract(source_image, ordered_pts, WIDTH, HEIGHT)
image, scores = score(cropped)
h, w, _ = image.shape
start_x = 30
start_y = h - 200
image = cv2.rectangle(image, (start_x, start_y-30), (start_x+160, start_y+120), (0,0,0), -1)
place = 0
for color, score in sorted(scores.items(), key=lambda x: -x[1]):
image = cv2.putText(image, f"{place + 1}. {color.title()}: {score}",
(start_x, start_y + place * 25), cv2.FONT_HERSHEY_SIMPLEX, .75, (255, 255, 255) , 2,cv2.LINE_AA)
place += 1
cv2.imshow('ResNet to Ride', image)
elif k == ord('d'):
# print(mouseX,mouseY)
print("Deleting point.")
delete_point()
cv2.destroyAllWindows()
# cv2.waitKey(0)
# cv2.destroyAllWindows()