-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
299 lines (231 loc) · 10.1 KB
/
main.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import time
import tkinter as tk
import tkinter.font as tk_font
from functools import partial
import os
import tobii_research as tr
import constant
def get_time_stamp():
return int((time.time() - constant.START_TIME) * 1000)
def color_widget(widget, color):
if constant.PLATFORM == "Darwin":
widget['highlightbackground'] = color
else:
widget['bg'] = color
def key_press(key):
global is_typing
log_key_event('Key Press', key)
if key == 'Enter':
load_new_phrase()
input_entry.delete(0, 'end')
switch_typing(False)
else:
switch_typing(True)
if key == 'Back':
input_entry.delete(len(input_entry.get()) - 1)
elif key == 'Space':
input_entry.insert('end', ' ')
else:
input_entry.insert('end', key.lower())
def switch_typing(value):
global is_typing
global typing_time
global total_typing_time
global first_phrase_loaded
if is_typing != value:
is_typing = value
if is_typing:
typing_time = get_time_stamp()
else:
if not first_phrase_loaded:
first_phrase_loaded = True
else:
total_typing_time += get_time_stamp() - typing_time
if total_typing_time >= constant.TYPING_TIME:
finish()
def setup_input():
global input_entry
global input_label
font_input = tk_font.Font(size=20)
input_label = tk.Label(frame, fg=constant.COLOR_HIGHLIGHT, bg=constant.COLOR_BACKGROUND, font=font_input)
input_label.pack(side='top', pady=(15, 0))
input_entry = tk.Entry(frame, width=40, bg=constant.COLOR_HIGHLIGHT, justify='center', fg='black',
insertbackground='black',
font=font_input)
input_entry.pack(side='top', pady=(0, 15))
input_entry.focus_set()
def setup_input2():
global input_entry
global input_label
global dwell_time_label
input_frame = tk.Frame(frame, bg=constant.COLOR_BACKGROUND)
input_frame.pack(side='top', fill='both', pady=(0, 15))
left_frame = tk.Frame(input_frame, bg=constant.COLOR_BACKGROUND)
left_frame.pack(side='left')
font_input = tk_font.Font(size=20)
input_label = tk.Label(left_frame, fg=constant.COLOR_HIGHLIGHT, bg=constant.COLOR_BACKGROUND, font=font_input,
text='Find a dwell time which suits you')
input_label.pack(side='top', padx=(6, 0), anchor='w', pady=(15, 0))
input_entry = tk.Entry(left_frame, width=40, bg=constant.COLOR_HIGHLIGHT, fg='black',
insertbackground='black',
font=font_input)
input_entry.pack(side='top', padx=(6, 0))
input_entry.focus_set()
dwell_time_frame = tk.Frame(input_frame, bg=constant.COLOR_BACKGROUND)
dwell_time_frame.pack(side='right', anchor='s')
minus_button = tk.Button(dwell_time_frame, text='-50ms', width=5, height=2, bd=0, highlightthickness=0,
font=font_input, bg=constant.COLOR_BACKGROUND, fg=constant.COLOR_HIGHLIGHT,
command=partial(change_dwell_time, -50))
dwell_time_label = tk.Label(dwell_time_frame, fg=constant.COLOR_HIGHLIGHT, bg=constant.COLOR_BACKGROUND,
font=font_input, text=str(constant.START_DWELL_TIME) + 'ms')
plus_button = tk.Button(dwell_time_frame, text='+50ms', width=5, height=2, bd=0, highlightthickness=0,
font=font_input, bg=constant.COLOR_BACKGROUND, fg=constant.COLOR_HIGHLIGHT,
command=partial(change_dwell_time, 50))
minus_button.pack(side='left', padx=(0, 7))
dwell_time_label.pack(side='left', padx=(0, 7))
plus_button.pack(side='left')
color_widget(minus_button, constant.COLOR_NEUTRAL)
color_widget(plus_button, constant.COLOR_NEUTRAL)
def setup_log_files():
global file_path
participant_number = str(len(os.listdir(constant.LOG_FILE_FOLDER)))
for i in range(3 - len(participant_number)):
participant_number = '0' + participant_number
participant_number = 'P' + participant_number
file_path = constant.LOG_FILE_FOLDER / participant_number
os.mkdir(file_path)
log_file_keys = open(file_path / constant.LOG_FILE_KEYS, 'w')
log_file_keys.write('time, event, related key, current phrase, current input\n')
log_file_keys.close()
log_file_gaze = open(file_path / constant.LOG_FILE_GAZE, 'w')
log_file_gaze.write(', gaze point, , gaze point validity, , pupil size, , pupil validity\n')
log_file_gaze.write('time, X, Y, L, R, L, R, L, R\n')
log_file_gaze.close()
def change_dwell_time(change_value):
global dwell_time
dwell_time += change_value
dwell_time_label['text'] = str(dwell_time) + 'ms'
# Load Phrases
def load_phrases():
file_phrases = open(constant.FILE_PHRASES, 'r')
loaded_phrases = file_phrases.readlines()
file_phrases.close()
return loaded_phrases
def setup_keyboard():
font_keys = tk_font.Font(size=24)
for i in range(0, len(constant.KEYS_QWERTY)):
store_key_row = tk.Canvas(frame, bg=constant.COLOR_BACKGROUND, highlightthickness=0)
store_key_row.pack(anchor='center')
# Placeholder to indent key rows according to physical layout
placeholder = tk.Canvas(store_key_row, width=(56 * i - 33), height=50, bg=constant.COLOR_BACKGROUND,
highlightthickness=0)
placeholder.pack(side='left', fill='y')
for k in constant.KEYS_QWERTY[i]:
k = k.capitalize()
store_key = tk.Button(store_key_row, text=k, width=5, height=3, bd=0, highlightthickness=0, font=font_keys,
command=partial(key_press, k), fg=constant.COLOR_HIGHLIGHT)
if k == 'Enter' or k == 'Back':
store_key.pack(side='right', padx=(20, 0), pady=(0, 3))
else:
store_key.pack(side='left', anchor='w', padx=(0, 3), pady=(0, 3))
color_widget(store_key, constant.COLOR_NEUTRAL)
if k == 'Space':
store_key['width'] = 12
def setup_eyetracker():
found_eyetrackers = tr.find_all_eyetrackers()
found_eyetrackers[0].subscribe_to(tr.EYETRACKER_GAZE_DATA, check_gaze, as_dictionary=True)
def load_new_phrase():
global phrases
log_key_event('New Phrase', None)
new_phrase = phrases.pop(0)
new_phrase = new_phrase[0:len(new_phrase) - 1]
input_label['text'] = new_phrase
def log_gaze_event(gaze_data):
file = open(file_path / constant.LOG_FILE_GAZE, 'a')
gaze_point = get_gaze_point_on_screen(gaze_data)
file.write('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}\n'.format(
get_time_stamp(), gaze_point[0], gaze_point[1],
gaze_data['left_gaze_point_validity'], gaze_data['right_gaze_point_validity'],
gaze_data['left_pupil_diameter'], gaze_data['right_pupil_diameter'],
gaze_data['left_pupil_validity'], gaze_data['right_pupil_validity']))
file.close()
def log_key_event(event, related_key):
file = open(file_path / constant.LOG_FILE_KEYS, 'a')
file.write(
'{0}, {1}, {2}, {3}, {4}\n'.format(get_time_stamp(), event, related_key, input_label['text'],
input_entry.get()))
file.close()
def get_gaze_point_on_screen(gaze_data):
left = gaze_data['left_gaze_point_on_display_area']
right = gaze_data['right_gaze_point_on_display_area']
x_on_screen = ((left[0] + right[0]) / 2) * canvas.winfo_width()
y_on_screen = ((left[1] + right[1]) / 2) * canvas.winfo_height()
return x_on_screen, y_on_screen
def check_gaze(gaze_data):
global selected_widget
global selection_time
global key_pressed
log_gaze_event(gaze_data)
gaze_point = get_gaze_point_on_screen(gaze_data)
hovered_widget = root.winfo_containing(gaze_point[0], gaze_point[1])
if not key_pressed:
if type(hovered_widget) == tk.Button:
if hovered_widget == selected_widget:
if (get_time_stamp() - selection_time) > dwell_time:
# key press
selected_widget.invoke()
key_pressed = True
color_widget(selected_widget, constant.COLOR_PRESSED)
else:
if selected_widget:
# Deselect previously selected key
color_widget(selected_widget, constant.COLOR_NEUTRAL)
log_key_event('Key Deselected', selected_widget['text'])
# Select new key
selected_widget = hovered_widget
color_widget(selected_widget, constant.COLOR_HOVERED)
log_key_event('Key Selected', selected_widget['text'])
selection_time = get_time_stamp()
else:
# Deselect previously selected key
color_widget(selected_widget, constant.COLOR_NEUTRAL)
log_key_event('Key Deselected', selected_widget['text'])
elif get_time_stamp() - selection_time >= dwell_time + constant.PAUSE_TIME:
# End selection pause
key_pressed = False
color_widget(selected_widget, constant.COLOR_NEUTRAL)
log_key_event('Pause Ended', selected_widget['text'])
selected_widget = None
def finish():
# found_eyetrackers = tr.find_all_eyetrackers()
# found_eyetrackers[0].unsubscribe_from(tr.EYETRACKER_GAZE_DATA, check_gaze)
root.destroy()
# Variables
selected_widget = None
selection_time = None
key_pressed = False
file_path = ''
dwell_time = constant.START_DWELL_TIME
is_typing = False
total_typing_time = 0
typing_time = 0
first_phrase_loaded = False
input_label = None
input_entry = None
dwell_time_label = None
# Setup Tkinter
root = tk.Tk()
root.title("Gaze Typing")
root.attributes('-fullscreen', True)
canvas = tk.Canvas(root, highlightthickness=0, bg=constant.COLOR_BACKGROUND)
canvas.pack(fill='both', expand=True)
frame = tk.Frame(canvas, bg=constant.COLOR_BACKGROUND)
frame.pack(fill="none", expand=True)
# setup_input()
setup_input2()
setup_log_files()
phrases = load_phrases()
# load_new_phrase()
setup_keyboard()
# setup_eyetracker()
root.mainloop()