-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbox.py
239 lines (173 loc) · 5.83 KB
/
toolbox.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
import math
from point import Point
from gesture import Gesture
from tkinter import simpledialog
# Constants
NumPoints = 64
SquareSize = 250.0
HalfDiagonal = 0.5 * math.sqrt(250 * 250 + 250 * 250)
AngleRange = 45.0
AnglePrecision = 2.0
Phi = 0.5 * (-1.0 + math.sqrt(5.0)) # Golden Ratio
GENERAL_THRESHOLD = 5000.0 # Must be tuned according to the
# Global variables
strokes = []
current_stroke = []
templates = []
# Distance between points
def distance(p1, p2):
dx = p2.x - p1.x
dy = p2.y - p1.y
return math.sqrt(dx * dx + dy * dy)
# Resample points in a gesture
def resample(points):
I = path_length(points) / (NumPoints - 1)
D = 0.0
new_points = [points[0]]
i = 1
while i < len(points):
d = distance(points[i - 1], points[i])
if D + d >= I:
qx = points[i - 1].x + ((I - D) / d) * \
(points[i].x - points[i - 1].x)
qy = points[i - 1].y + ((I - D) / d) * \
(points[i].y - points[i - 1].y)
q = Point(qx, qy)
new_points.append(q)
points.insert(i, q)
D = 0.0
else:
D += d
i += 1
return new_points
# Compute the path length of a list of points
def path_length(points):
d = 0.0
for i in range(1, len(points)):
d += distance(points[i - 1], points[i])
return d
# Scale the points to fit within a square of size SquareSize
def scale(points):
min_x = min(p.x for p in points)
max_x = max(p.x for p in points)
min_y = min(p.y for p in points)
max_y = max(p.y for p in points)
scale_factor = SquareSize / max(max_x - min_x, max_y - min_y)
new_points = []
for p in points:
qx = p.x * scale_factor
qy = p.y * scale_factor
new_points.append(Point(qx, qy))
return new_points
# Translate points to the origin
def translate_to_origin(points):
centroid_x = sum(p.x for p in points) / len(points)
centroid_y = sum(p.y for p in points) / len(points)
new_points = []
for p in points:
qx = p.x - centroid_x
qy = p.y - centroid_y
new_points.append(Point(qx, qy))
return new_points
# Recognize a gesture
def recognize(gesture, templates):
min_distance = float('inf')
best_match = None
for template in templates:
d = distance_at_best_angle(gesture.points, template)
if d < min_distance:
min_distance = d
best_match = template.name
return best_match, min_distance
def distance_at_best_angle(points, template):
Phi = 0.5 * (-1.0 + (5.0 ** 0.5))
theta_a = -45.0
theta_b = 45.0
threshold = 2.0
d1 = distance_at_angle(points, template, Phi *
theta_a + (1 - Phi) * theta_b)
d2 = distance_at_angle(points, template, (1 - Phi)
* theta_a + Phi * theta_b)
while abs(theta_a - theta_b) > threshold:
if d1 < d2:
theta_b = Phi * theta_a + (1 - Phi) * theta_b
d2 = d1
d1 = distance_at_angle(
points, template, Phi * theta_a + (1 - Phi) * theta_b)
else:
theta_a = (1 - Phi) * theta_a + Phi * theta_b
d1 = d2
d2 = distance_at_angle(
points, template, (1 - Phi) * theta_a + Phi * theta_b)
return min(d1, d2)
# Helper function for distance_at_best_angle
def distance_at_angle(points, template, theta):
new_points = rotate_by(points, theta)
d = path_distance(new_points, template.points)
return d
# Rotate points by a given angle
def rotate_by(points, theta):
centroid_x = sum(p.x for p in points) / len(points)
centroid_y = sum(p.y for p in points) / len(points)
angle = math.radians(theta)
cos_val = math.cos(angle)
sin_val = math.sin(angle)
new_points = []
for p in points:
qx = (p.x - centroid_x) * cos_val - \
(p.y - centroid_y) * sin_val + centroid_x
qy = (p.x - centroid_x) * sin_val + \
(p.y - centroid_y) * cos_val + centroid_y
new_points.append(Point(qx, qy))
return new_points
def path_distance(points1, points2):
d = 0
n = min(len(points1), len(points2)) # Take the length of the shorter list
for i in range(n):
d += distance(points1[i], points2[i])
return d
# Capture points when the mouse is pressed and dragged
def on_drag(event):
x, y = event.x, event.y
canvas.create_oval(x, y, x+5, y+5, fill='black')
current_stroke.append(Point(x, y))
# End the current stroke when the mouse is released
def on_release(event):
global strokes, current_stroke
if current_stroke:
strokes.append(current_stroke)
current_stroke = []
# Clear the canvas and strokes
def clear_canvas():
global strokes
canvas.delete("all")
strokes = []
# Save a new gesture template
def save_template():
global strokes, templates
if not strokes:
return
name = simpledialog.askstring("Input", "Enter gesture name:")
if name:
points = [point for stroke in strokes for point in stroke]
points = resample(points)
points = scale(points)
points = translate_to_origin(points)
templates.append(Gesture(name, points))
clear_canvas()
# Recognize a gesture based on the current strokes
def recognize_gesture():
global strokes, templates
if not strokes or not templates:
return
points = [point for stroke in strokes for point in stroke]
points = resample(points)
points = scale(points)
points = translate_to_origin(points)
gesture = Gesture("unknown", points)
name, min_distance = recognize(gesture, templates)
if name and min_distance < GENERAL_THRESHOLD:
messagebox.showinfo("Recognized", f"Gesture recognized as {name}")
else:
messagebox.showinfo("Not recognized", "Gesture not recognized")
clear_canvas()