-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen2board.py
More file actions
174 lines (138 loc) · 4.53 KB
/
screen2board.py
File metadata and controls
174 lines (138 loc) · 4.53 KB
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
from __future__ import print_function
import algemy
import argparse
import cv2
import math
import numpy as np
import sys
# Fraction of the top of the screen taken up by the menu.
TOP_CROP_FRAC = 0.25
# Average colors for each board crystal.
COLOR_MAP = {
' ': (255, 255, 255),
'R': (159, 159, 187),
'O': (126, 158, 185),
'Y': (155, 183, 183),
'G': (127, 161, 127),
'B': (187, 168, 168),
'V': (175, 127, 175),
'W': (221, 221, 221),
'X': (102, 122, 142),
}
# Possible colors for the color-wheel, clockwise.
COLORS = ['R', 'O', 'Y', 'G', 'B', 'V']
# Color wheel radius as a fraction of board spacing.
WHEEL_RF = 0.75
def closest_color(r, g, b):
v = 255
ret = ''
for c, (r2, g2, b2) in COLOR_MAP.items():
d = math.sqrt((r - r2)**2 + (g - g2)**2 + (b - b2)**2)
if d < v:
v = d
ret = c
return ret
def dist(p1, p2):
x1, y1 = p1
x2, y2 = p2
return int(math.sqrt((x2 - x1)**2 + (y2 - y1)**2))
def main():
parser = argparse.ArgumentParser(
description='Solve an Algemy board from a screenshot.')
parser.add_argument('screenshot', help='path to screenshot')
parser.add_argument('-x', '--all_colors', help='enable debug mode', action='store_true')
parser.add_argument('-v', '--debug', help='enable debug mode', action='store_true')
args = parser.parse_args()
img = cv2.imread(args.screenshot)
if img is None:
print('# Missing input image')
sys.exit(1)
height, width = img.shape[:2]
# Crop out the top menu bar.
img = img[int(height * TOP_CROP_FRAC):height, 0:width]
# Convert to black & white.
imggray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(imggray, 127, 255, 0)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if contours is None or hierarchy is None:
print('# Missing contours')
sys.exit(1)
# Traverse the contour tree to extract the inside of the grid (level 2 contours)
l2_contours = []
def traverse(i, level):
while i != -1:
if level == 2:
l2_contours.append(contours[i])
i, prev, child, parent = hierarchy[0][i]
if child != -1:
traverse(child, level + 1)
traverse(0, 0)
# Find center points of each grid square.
centers = []
for c in l2_contours:
(x, y), _ = cv2.minEnclosingCircle(c)
centers.append((int(x), int(y)))
# Determine the spacing of our grid.
spacing = min(dist(centers[0], c) for c in centers[1:])
# Generate a top-down, left-right sort and split into rows.
rows = {}
for x, y in centers:
closest_row = None
for ry in rows.keys():
if abs(ry - y) < spacing / 2:
closest_row = ry
if closest_row:
rows[closest_row].append((x, y))
else:
rows[y] = [(x, y)]
rows = list(rows.items())
rows.sort() # Sort top-down.
rows = [sorted(row) for _, row in rows] # Sort left-right.
board = []
for row in rows:
board_row = []
for x, y in row:
dilate = int(spacing / 4)
sample = img[(y-dilate):(y+dilate), (x-dilate):(x+dilate)]
# TODO: filter out black and white pixels for more color averaging that's
# less dependent on sample size.
avg_color = tuple(np.mean(sample, axis=(0,1)))
color = closest_color(*avg_color)
board_row.append(color)
board.append(board_row)
print("# DETECTED")
for row in board:
print('# ' + ' '.join(el.replace(' ','-') for el in row))
print()
solution = algemy.solve_board(board, args.all_colors)
if not solution:
print("# No solution.")
sys.exit(1)
print("# SOLUTION")
for row in solution:
print('# ' + ' '.join(el.replace(' ','-') for el in row))
print()
for r, row in enumerate(solution):
for c, color in enumerate(row):
x = rows[r][c][0]
y = rows[r][c][1]
cv2.circle(img, (x, y), 2, (0, 255, 0), 2)
if color.strip() == '':
continue
cv2.circle(img, (x, y), 2, (0, 0, 255), 3)
cv2.putText(img, str(color), (x - 5, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Compensate for top cropping.
y += int(height * TOP_CROP_FRAC)
# Tap on square to bring up color wheel.
print("adb shell input tap %i %i" % (x, y))
deg = (360 / len(COLORS)) * COLORS.index(color)
xd = math.sin(math.radians(deg)) * WHEEL_RF * spacing
yd = -1 * math.cos(math.radians(deg)) * WHEEL_RF * spacing
# Tap on the correct color.
print("adb shell input tap %i %i" % (x + xd, y + yd))
# Show image processing debug.
if args.debug:
cv2.imshow('debug', img)
cv2.waitKey(0)
if __name__ == '__main__':
main()