-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognizer.py
More file actions
216 lines (181 loc) · 17.2 KB
/
recognizer.py
File metadata and controls
216 lines (181 loc) · 17.2 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
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
import math
RESAMPLE_SIZE = 64
SCALING_SIZE = 128.0
CIRCLE = [(214, 105), (210, 105), (205, 105), (205, 105), (201, 105), (194, 105), (194, 105), (190, 105), (186, 107), (181, 109), (181, 109), (176, 112), (171, 116), (171, 116), (167, 120), (161, 126), (154, 134), (154, 134), (149, 141), (143, 148), (143, 148), (138, 157), (135, 164), (133, 174), (133, 174), (133, 183), (133, 192), (133, 192), (133, 204), (135, 215), (138, 227), (138, 227), (143, 239), (149, 252), (149, 252), (156, 263), (166, 275), (166, 275), (176, 284), (188, 292), (200, 299), (200, 299), (215, 303), (231, 306), (231, 306), (247, 308), (263, 308), (279, 308), (279, 308), (294, 305), (305, 302), (305, 302), (316, 296), (324, 291), (330, 285), (330, 285), (335, 276), (338, 266), (338, 266), (340, 256), (341, 246), (341, 246), (341, 236), (338, 224), (335, 214), (335, 214), (331, 202), (328, 193), (328, 193), (323, 183), (318, 174), (318, 174), (313, 165), (306, 156), (301, 149), (301, 149), (295, 142), (288, 135), (288, 135), (281, 129), (274, 124), (274, 124), (267, 119), (260, 116), (254, 113), (254, 113), (246, 110), (237, 107), (237, 107), (229, 105), (222, 104), (216, 103), (216, 103), (214, 103), (214, 103)]
SQUARE = [(123, 149), (123, 149), (123, 150), (123, 152), (123, 156), (123, 160), (123, 167), (123, 167), (123, 175), (123, 184), (123, 184), (123, 194), (123, 205), (123, 218), (123, 218), (124, 230), (124, 240), (124, 240), (125, 249), (126, 258), (126, 258), (126, 266), (126, 273), (126, 276), (126, 276), (126, 280), (127, 284), (127, 284), (127, 289), (128, 296), (128, 296), (130, 301), (130, 304), (130, 304), (130, 305), (130, 308), (130, 310), (130, 310), (130, 316), (130, 323), (130, 328), (130, 328), (130, 331), (131, 332), (131, 332), (131, 332), (131, 334), (131, 334), (132, 334), (133, 335), (136, 336), (136, 336), (141, 337), (149, 338), (160, 339), (160, 339), (173, 340), (187, 340), (187, 340), (202, 341), (219, 342), (219, 342), (234, 342), (249, 343), (264, 345), (264, 345), (280, 345), (298, 347), (298, 347), (312, 348), (326, 348), (340, 349), (340, 349), (355, 349), (367, 350), (367, 350), (379, 351), (390, 353), (390, 353), (401, 354), (410, 354), (419, 354), (419, 354), (427, 354), (433, 354), (433, 354), (433, 354), (433, 354), (433, 354), (436, 352), (437, 351), (437, 346), (437, 346), (437, 341), (437, 333), (438, 324), (438, 324), (439, 315), (439, 304), (439, 304), (439, 292), (439, 280), (439, 280), (439, 269), (438, 259), (437, 251), (437, 251), (437, 243), (437, 234), (437, 234), (437, 226), (436, 216), (436, 209), (436, 209), (436, 200), (436, 194), (436, 194), (436, 187), (436, 180), (436, 180), (436, 173), (436, 167), (436, 160), (436, 160), (436, 155), (436, 150), (436, 150), (436, 144), (436, 139), (436, 135), (436, 135), (436, 130), (436, 126), (436, 126), (436, 121), (436, 117), (436, 117), (436, 113), (436, 111), (436, 111), (436, 111), (434, 109), (433, 108), (433, 108), (429, 108), (424, 108), (417, 108), (417, 108), (409, 109), (401, 109), (393, 110), (393, 110), (384, 110), (374, 111), (374, 111), (364, 111), (354, 112), (344, 113), (344, 113), (336, 113), (328, 113), (328, 113), (321, 113), (313, 113), (305, 113), (305, 113), (298, 113), (290, 113), (290, 113), (284, 113), (277, 114), (277, 114), (270, 114), (264, 114), (257, 114), (257, 114), (252, 114), (246, 114), (241, 115), (241, 115), (236, 115), (231, 116), (231, 116), (225, 116), (220, 116), (220, 116), (214, 116), (210, 117), (210, 117), (204, 117), (200, 117), (196, 117), (196, 117), (192, 118), (187, 118), (187, 118), (182, 118), (177, 118), (172, 119), (172, 119), (167, 120), (163, 120), (163, 120), (159, 121), (154, 121), (150, 121), (150, 121), (145, 121), (141, 121), (141, 121), (138, 121), (136, 121), (136, 121), (136, 121), (136, 121), (136, 121), (136, 121), (134, 121), (134, 121), (134, 121), (134, 121), (134, 121), (134, 122), (134, 122), (134, 122), (134, 122)]
TRIANGLE = [(89, 369), (89, 369), (89, 369), (89, 369), (90, 369), (94, 364), (97, 359), (97, 359), (102, 352), (106, 345), (106, 345), (112, 337), (117, 331), (122, 323), (122, 323), (127, 316), (131, 310), (131, 310), (135, 303), (138, 296), (142, 289), (142, 289), (146, 282), (150, 276), (150, 276), (153, 270), (157, 264), (160, 258), (160, 258), (164, 252), (167, 246), (167, 246), (170, 240), (173, 233), (173, 233), (176, 228), (179, 223), (179, 223), (182, 217), (184, 212), (187, 207), (187, 207), (191, 201), (193, 196), (193, 196), (196, 191), (199, 186), (201, 181), (201, 181), (204, 175), (208, 170), (208, 170), (210, 165), (213, 158), (216, 153), (216, 153), (219, 147), (221, 142), (221, 142), (222, 138), (225, 134), (226, 133), (226, 133), (226, 133), (226, 133), (226, 133), (226, 133), (226, 133), (228, 135), (228, 135), (229, 139), (230, 143), (230, 143), (232, 148), (234, 153), (234, 153), (237, 160), (240, 167), (242, 174), (242, 174), (245, 179), (249, 185), (249, 185), (252, 191), (257, 196), (259, 201), (259, 201), (263, 207), (268, 213), (268, 213), (272, 218), (277, 225), (281, 231), (281, 231), (286, 237), (291, 244), (291, 244), (295, 250), (302, 256), (302, 256), (307, 261), (312, 267), (316, 273), (316, 273), (321, 277), (326, 283), (326, 283), (330, 287), (334, 293), (339, 298), (339, 298), (345, 303), (350, 308), (350, 308), (356, 313), (360, 318), (360, 318), (366, 322), (371, 326), (376, 330), (376, 330), (381, 334), (385, 338), (385, 338), (389, 341), (393, 344), (393, 344), (398, 348), (402, 352), (402, 352), (406, 354), (409, 357), (412, 359), (412, 359), (413, 360), (413, 360), (413, 361), (413, 361), (413, 361), (413, 361), (413, 361), (412, 362), (411, 362), (411, 362), (409, 363), (407, 363), (404, 364), (404, 364), (400, 364), (395, 365), (395, 365), (388, 365), (381, 365), (374, 365), (374, 365), (365, 365), (358, 365), (358, 365), (351, 365), (343, 365), (336, 365), (336, 365), (328, 365), (328, 365), (320, 365), (313, 365), (306, 365), (306, 365), (299, 365), (291, 365), (284, 364), (284, 364), (278, 364), (270, 364), (264, 364), (264, 364), (256, 364), (249, 364), (249, 364), (241, 364), (233, 364), (233, 364), (226, 364), (219, 364), (219, 364), (212, 365), (206, 365), (200, 365), (200, 365), (193, 365), (188, 365), (188, 365), (182, 366), (177, 367), (172, 367), (172, 367), (167, 367), (163, 367), (163, 367), (159, 368), (154, 368), (150, 368), (150, 368), (144, 368), (139, 368), (139, 368), (134, 369), (129, 369), (125, 369), (125, 369), (121, 369), (117, 369), (114, 369), (114, 369), (110, 369), (108, 369), (108, 369), (104, 369), (101, 369), (101, 369), (98, 369), (98, 369), (98, 369), (98, 369), (98, 369), (98, 369), (98, 369), (98, 369), (98, 369)]
DOWN_V = [(94, 112), (94, 112), (94, 112), (94, 112), (94, 112), (94, 112), (94, 112), (94, 114), (94, 114), (96, 118), (98, 122), (98, 122), (99, 128), (101, 132), (103, 138), (103, 138), (105, 144), (108, 149), (109, 156), (109, 156), (113, 163), (116, 169), (116, 169), (118, 175), (120, 179), (120, 179), (123, 183), (125, 188), (128, 195), (128, 195), (130, 202), (133, 208), (133, 208), (136, 213), (138, 219), (138, 219), (141, 223), (144, 229), (146, 233), (146, 233), (148, 238), (150, 241), (152, 246), (152, 246), (153, 251), (156, 255), (156, 255), (158, 261), (161, 266), (163, 271), (163, 271), (166, 278), (169, 284), (169, 284), (171, 290), (174, 295), (174, 295), (177, 302), (179, 309), (182, 315), (182, 315), (185, 322), (187, 330), (187, 330), (190, 336), (193, 342), (195, 348), (195, 348), (197, 354), (199, 359), (199, 359), (201, 364), (202, 369), (204, 372), (204, 372), (204, 372), (205, 375), (206, 376), (206, 376), (206, 376), (206, 376), (206, 376), (207, 376), (208, 374), (208, 374), (210, 370), (212, 366), (215, 361), (215, 361), (219, 355), (223, 350), (223, 350), (226, 345), (231, 340), (235, 334), (235, 334), (238, 329), (242, 324), (242, 324), (245, 319), (249, 311), (253, 304), (253, 304), (257, 296), (262, 288), (265, 281), (265, 281), (271, 274), (275, 266), (275, 266), (280, 258), (285, 250), (285, 250), (288, 245), (292, 237), (295, 232), (295, 232), (298, 225), (302, 218), (302, 218), (305, 212), (308, 207), (311, 201), (311, 201), (314, 195), (317, 190), (317, 190), (320, 184), (322, 179), (322, 179), (326, 173), (329, 167), (331, 161), (331, 161), (334, 156), (338, 147), (338, 147), (341, 141), (343, 135), (346, 130), (346, 130), (348, 125), (350, 119), (350, 119), (352, 116), (352, 116), (353, 113), (353, 113), (353, 113), (353, 113), (353, 113)]
CARROT = [(90, 361), (90, 361), (90, 361), (90, 361), (91, 361), (91, 361), (93, 358), (96, 353), (96, 353), (102, 346), (109, 336), (115, 326), (115, 326), (124, 311), (132, 297), (132, 297), (140, 284), (148, 271), (154, 260), (154, 260), (162, 248), (167, 236), (167, 236), (174, 227), (178, 217), (178, 217), (184, 208), (187, 201), (191, 193), (191, 193), (194, 186), (197, 180), (197, 180), (199, 176), (201, 171), (201, 171), (202, 166), (204, 162), (205, 161), (205, 161), (205, 161), (205, 161), (205, 161), (205, 162), (206, 164), (208, 169), (208, 169), (211, 177), (215, 186), (218, 194), (218, 194), (223, 203), (227, 213), (227, 213), (231, 223), (236, 235), (241, 246), (241, 246), (246, 257), (252, 268), (252, 268), (256, 277), (259, 284), (259, 284), (262, 291), (267, 299), (271, 306), (271, 306), (274, 313), (278, 318), (278, 318), (282, 323), (284, 326), (284, 326), (287, 330), (289, 334), (290, 336), (290, 336), (293, 338), (295, 341), (295, 341), (297, 345), (301, 348), (302, 351), (302, 351), (304, 353), (304, 353), (306, 354), (306, 355), (306, 355), (306, 355), (307, 355), (307, 355), (307, 355), (307, 355), (307, 355), (307, 355)]
FISH = [(119, 129), (119, 129), (119, 129), (119, 130), (119, 130), (120, 132), (124, 135), (130, 138), (130, 138), (136, 142), (142, 146), (142, 146), (151, 152), (160, 157), (169, 163), (169, 163), (179, 169), (189, 176), (189, 176), (199, 183), (209, 188), (209, 188), (220, 194), (229, 198), (239, 202), (239, 202), (252, 206), (265, 208), (278, 209), (278, 209), (290, 211), (302, 211), (302, 211), (312, 211), (321, 209), (328, 205), (328, 205), (334, 199), (338, 192), (338, 192), (341, 184), (343, 177), (344, 172), (344, 172), (344, 166), (344, 159), (344, 159), (343, 153), (340, 148), (340, 148), (335, 143), (331, 139), (326, 136), (326, 136), (322, 133), (315, 131), (309, 129), (309, 129), (304, 129), (299, 129), (299, 129), (291, 129), (286, 130), (279, 131), (279, 131), (273, 133), (265, 136), (265, 136), (258, 138), (252, 141), (252, 141), (244, 144), (237, 147), (230, 151), (230, 151), (225, 153), (220, 157), (220, 157), (214, 161), (209, 165), (204, 171), (204, 171), (197, 176), (191, 182), (183, 188), (183, 188), (176, 194), (169, 200), (162, 207), (162, 207), (155, 212), (152, 216), (152, 216), (149, 217), (148, 219), (147, 220), (147, 220), (147, 220), (147, 220), (147, 220), (147, 220), (147, 220)]
class Template:
def __init__(self, name, points):
self.name = name
self.points = points
@classmethod
def generate_template(cls, name, points, resample_rate=RESAMPLE_SIZE, scaling_size=SCALING_SIZE):
if isinstance(points[0], tuple):
fixed_points = list()
for point in points:
fixed_points.append(Point.from_tuple(point))
return cls(name, resample(fixed_points, resample_rate, scaling_size))
return cls(name, resample(points, resample_rate, scaling_size))
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self) -> str:
return f"({self.x}, {self.y})"
def __repr__(self) -> str:
return str(self)
@classmethod
def from_str(cls, string):
string = string.replace(')', '').replace('(', '')
l, r = string.split(',')
x = int(l)
y = int(r)
return cls(x, y)
@classmethod
def from_tuple(cls, tup):
x = tup[0]
y = tup[1]
return cls(float(x), float(y))
@staticmethod
def dist(a, b):
if not isinstance(a, Point) or not isinstance(b, Point):
raise SyntaxError(f"Cannot get the distance of a non <Point> object!\n\tA ({a}) Type: {type(a)} B ({b}) Type: {type(b)}")
dx = b.x - a.x
dx2 = math.pow(dx, 2)
dy = b.y - a.y
dy2 = math.pow(dy, 2)
return math.sqrt(dx2 + dy2)
@classmethod
def centroid(cls, points:list):
num_points = len(points)
sum_x = 0
sum_y = 0
for point in points:
sum_x += point.x
sum_y += point.y
cx = sum_x / num_points
cy = sum_y / num_points
return cls(cx, cy)
@staticmethod
def rotate_by_angle(points:list, center, angle:float):
rotated_points = list()
for point in points:
qx = (point.x - center.x) * math.cos(angle) - (point.y - center.y) * math.sin(angle) + center.x
qy = (point.x - center.x) * math.sin(angle) + (point.y - center.y) * math.cos(angle) + center.y
rotated_points.append(Point(qx, qy))
return rotated_points
@staticmethod
def get_bounding_box(points:list):
min_x = math.inf
max_x = -math.inf
min_y = math.inf
max_y = -math.inf
for point in points:
if point.x < min_x:
min_x = point.x
elif point.x > max_x:
max_x = point.x
if point.y < min_y:
min_y = point.y
elif point.y > max_y:
max_y = point.y
return (Point(min_x, min_y), Point(max_x, max_y))
@staticmethod
def scale_to_square(points:list, scaling_size:float):
scaled_points = list()
lower_corner, upper_corner = Point.get_bounding_box(points)
box_width = max(1.0, upper_corner.x - lower_corner.x)
box_height = max(1.0, upper_corner.y - lower_corner.y)
for point in points:
qx = point.x * (scaling_size / box_width)
qy = point.y * (scaling_size / box_height)
scaled_points.append(Point(qx, qy))
return scaled_points
def resample(points:list, resample_size:int=RESAMPLE_SIZE, scaling_size:float=SCALING_SIZE):
normalized_step_distance = get_path_length(points) / float(resample_size - 1)
distance_sum = 0
new_points = [points[0]]
i = 1
while i < len(points):
curr_point = points[i]
prev_point = points[i - 1]
step_distance = Point.dist(prev_point, curr_point)
if distance_sum + step_distance >= normalized_step_distance:
delta = (normalized_step_distance - distance_sum) / step_distance
qx = prev_point.x + delta * (curr_point.x - prev_point.x)
qy = prev_point.y + delta * (curr_point.y - prev_point.y)
q = Point(qx, qy)
new_points.append(q)
points.insert(i, q)
distance_sum = 0
else:
distance_sum += step_distance
i+=1
if len(new_points) == resample_size - 1:
new_points.append(points[-1])
if len(new_points) != resample_size:
raise RuntimeError(f"Resample did not produce {resample_size} points from {len(points)} points: {len(new_points)}")
new_points = rotate_to_zero(new_points)
new_points = Point.scale_to_square(new_points, scaling_size)
new_points = translate_to_origin(new_points, Point.centroid(new_points))
return new_points
def get_path_length(points:list):
distance = 0
for i in range(1, len(points)):
distance += Point.dist(points[i-1], points[i])
return distance
def rotate_to_zero(points):
c = Point.centroid(points)
angle_to_zero = math.atan2(c.y - points[0].y, c.x - points[0].x)
return Point.rotate_by_angle(points, c, -angle_to_zero)
def translate_to_origin(points, origin):
translated_points = list()
for point in points:
qx = point.x - origin.x
qy = point.y - origin.y
translated_points.append(Point(qx, qy))
return translated_points
def recognize(points, templates, scaling_size=SCALING_SIZE):
if len(points) != RESAMPLE_SIZE:
points = resample(points)
b = math.inf
best_template = None
for template in templates:
best_dist = get_distance_at_best_angle(points, template, -math.pi * .25, math.pi * .25, math.pi * .0111111)
if best_dist < b:
b = best_dist
best_template = template
score = 1 - b / (.5 * math.sqrt(2 * math.pow(scaling_size, 2)))
return (best_template, score)
def get_distance_at_best_angle(points, template, alpha, beta, delta):
phi = .5 * (-1 + math.sqrt(5))
x1 = phi * alpha + (1-phi)*beta
f1 = get_distance_at_angle(points, template, x1)
x2 = (1 - phi) * alpha + phi * beta
f2 = get_distance_at_angle(points, template, x2)
while abs(beta - alpha) > delta:
if f1 < f2:
beta = x2
x2 = x1
f2 = f1
x1 = phi * alpha + (1 - phi) * beta
f1 = get_distance_at_angle(points, template, x1)
else:
alpha = x1
x1 = x2
f1 = f2
x2 = (1 - phi) * alpha + phi * beta
f2 = get_distance_at_angle(points, template, x2)
return min(f1, f2)
def get_distance_at_angle(points, template, angle):
new_points = Point.rotate_by_angle(points, Point.centroid(points), angle)
dist = get_path_distance(new_points, template.points)
return dist
def get_path_distance(a, b):
dist = 0
for i in range(0, len(a)):
dist += Point.dist(a[i], b[i])
return dist / len(a)