-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
177 lines (148 loc) · 5.08 KB
/
gui.py
File metadata and controls
177 lines (148 loc) · 5.08 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
import tkinter as tk
import random
from time import sleep
from modelTrain3 import NeuralNetwork
import torch
import numpy as np
from pylsl.pylsl import StreamInlet, resolve_stream
import mne
FILENAME = "questions.txt"
RESPONSE_FILENAME = "responses.txt"
file = open(FILENAME, 'r')
questions = file.readlines()
num_questions = len(questions)
for i in range(num_questions) :
questions[i].rstrip("\n")
print(questions[i])
index = 0
responses = []
def get_eeg_info():
ch_names = ['Fp1.', 'Fp2.', 'F3..', 'F4..', 'C3..', 'C4..', 'F7..', 'F8..']
ch_type = ['eeg'] * 8
sfreq = 129
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_type)
return info
def get_streams(name='type', type='EEG'):
streams = resolve_stream(name, type)
if len(streams) == 0:
print("No stream detected. Closing the program. Terminating.")
exit(1)
return streams
def get_data_from_stream(inlet: StreamInlet, chunk_size=129):
samples, _ = inlet.pull_chunk(timeout=1.0, max_samples=chunk_size)
if len(samples) == 0:
print("No samples received from connection. Terminating.")
exit(1)
data = np.array(samples).T
return torch.tensor(data, dtype=torch.float32).unsqueeze(0)
def predict_intended_action(model: NeuralNetwork, input_signal):
with torch.no_grad():
output = model(input_signal)
_, prediction = torch.max(output, dim=1)
action = prediction.item()
return action
def get_data_and_predict():
input_signal = get_data_from_stream(inlet)
action = predict_intended_action(model, input_signal=input_signal)
return action
def on_left():
global index
left_button.config(bg="darkgreen")
root.update()
sleep(0.2)
left_button.config(bg="lightgreen")
responses.append("LEFT\n")
print("LEFT button clicked!")
index += 1
if (index == num_questions) :
file = open(RESPONSE_FILENAME, 'w')
file.writelines(responses)
root.destroy()
def on_right():
global index
right_button.config(bg="darkred")
root.update()
sleep(0.2)
right_button.config(bg="lightcoral")
responses.append("RIGHT\n")
print("RIGHT button clicked!")
index += 1
if (index == num_questions) :
file = open(RESPONSE_FILENAME, 'w')
file.writelines(responses)
root.destroy()
def no_action():
print("No action taken")
def gui_loop():
global root, left_button, right_button, questions
global index
root = tk.Tk()
root.title("Question Window")
def on_left():
global index
left_button.config(bg="darkgreen")
root.update()
sleep(0.2)
left_button.config(bg="lightgreen")
responses.append("LEFT\n")
print("LEFT button clicked!")
index += 1
if index < num_questions :
question_label.config(text=questions[index])
heading_label.config(text=f"QUESTION {index + 1}")
else :
file = open(RESPONSE_FILENAME, 'w')
file.writelines(responses)
root.destroy()
def on_right():
global index
right_button.config(bg="darkred")
root.update()
sleep(0.2)
right_button.config(bg="lightcoral")
responses.append("RIGHT\n")
print("RIGHT button clicked!")
index += 1
if index < num_questions :
question_label.config(text=questions[index])
heading_label.config(text=f"QUESTION {index + 1}")
else :
file = open(RESPONSE_FILENAME, 'w')
file.writelines(responses)
root.destroy()
def no_action():
print("No action taken")
def simulate_button_click():
action = get_data_and_predict()
print(action)
match action:
case 1:
on_left()
case 2:
on_right()
case 0:
no_action()
root.after(1000, simulate_button_click)
root.geometry("400x300")
heading_label = tk.Label(root, text=f"QUESTION {index}", font=("Arial", 16))
heading_label.pack(pady=10)
question_label = tk.Label(root, text=questions[index], font=("Arial", 12))
question_label.pack(pady=20)
button_frame = tk.Frame(root)
button_frame.pack(pady=20, fill="x")
left_button = tk.Button(button_frame, text="YES", font=("Arial", 14), width=10, height=5, bg="lightgreen", command=on_left)
left_button.pack(side="left", padx=50, pady=100)
right_button = tk.Button(button_frame, text="NO", font=("Arial", 14), width=10, height=5, bg="lightcoral", command=on_right)
right_button.pack(side="right", padx=50, pady=100)
root.after(1000, simulate_button_click)
root.mainloop()
info = get_eeg_info()
model = NeuralNetwork(3)
model.load_state_dict(torch.load("models\\other_model_3.pth"))
model.eval()
streams = get_streams()
inlet = StreamInlet(streams[0])
def main():
gui_loop()
if __name__ == "__main__":
main()