forked from Sentdex/BCI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_and_making_data.py
139 lines (104 loc) · 3.78 KB
/
testing_and_making_data.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
from pylsl import StreamInlet, resolve_stream
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib import style
from collections import deque
import cv2
import os
import random
import tensorflow as tf
MODEL_NAME = "models/61.4-acc-loss-2.39-top.model" # your model path here.
model = tf.keras.models.load_model(MODEL_NAME)
reshape = (-1, 16, 60)
model.predict( np.zeros((32,16,60)).reshape(reshape) )
ACTION = 'left' # THIS IS THE ACTION YOU'RE THINKING
FFT_MAX_HZ = 60
HM_SECONDS = 10 # this is approximate. Not 100%. do not depend on this.
TOTAL_ITERS = HM_SECONDS*25 # ~25 iters/sec
BOX_MOVE = "model" # random or model
last_print = time.time()
fps_counter = deque(maxlen=150)
# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
streams = resolve_stream('type', 'EEG')
# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])
WIDTH = 800
HEIGHT = 800
SQ_SIZE = 50
MOVE_SPEED = 1
square = {'x1': int(int(WIDTH)/2-int(SQ_SIZE/2)),
'x2': int(int(WIDTH)/2+int(SQ_SIZE/2)),
'y1': int(int(HEIGHT)/2-int(SQ_SIZE/2)),
'y2': int(int(HEIGHT)/2+int(SQ_SIZE/2))}
box = np.ones((square['y2']-square['y1'], square['x2']-square['x1'], 3)) * np.random.uniform(size=(3,))
horizontal_line = np.ones((HEIGHT, 10, 3)) * np.random.uniform(size=(3,))
vertical_line = np.ones((10, WIDTH, 3)) * np.random.uniform(size=(3,))
total = 0
left = 0
right = 0
none = 0
correct = 0
channel_datas = []
for i in range(TOTAL_ITERS): # how many iterations. Eventually this would be a while True
channel_data = []
for i in range(16): # each of the 16 channels here
sample, timestamp = inlet.pull_sample()
channel_data.append(sample[:FFT_MAX_HZ])
fps_counter.append(time.time() - last_print)
last_print = time.time()
cur_raw_hz = 1/(sum(fps_counter)/len(fps_counter))
print(cur_raw_hz)
env = np.zeros((WIDTH, HEIGHT, 3))
env[:,HEIGHT//2-5:HEIGHT//2+5,:] = horizontal_line
env[WIDTH//2-5:WIDTH//2+5,:,:] = vertical_line
env[square['y1']:square['y2'], square['x1']:square['x2']] = box
cv2.imshow('', env)
cv2.waitKey(1)
network_input = np.array(channel_data).reshape(reshape)
out = model.predict(network_input)
print(out[0])
if BOX_MOVE == "random":
move = random.choice([-1,0,1])
square['x1'] += move
square['x2'] += move
elif BOX_MOVE == "model":
choice = np.argmax(out)
if choice == 0:
if ACTION == "left":
correct += 1
square['x1'] -= MOVE_SPEED
square['x2'] -= MOVE_SPEED
left += 1
elif choice == 2:
if ACTION == "right":
correct += 1
square['x1'] += MOVE_SPEED
square['x2'] += MOVE_SPEED
right += 1
else:
if ACTION == "none":
correct += 1
none += 1
total += 1
channel_datas.append(channel_data)
#plt.plot(channel_datas[0][0])
#plt.show()
datadir = "data"
if not os.path.exists(datadir):
os.mkdir(datadir)
actiondir = f"{datadir}/{ACTION}"
if not os.path.exists(actiondir):
os.mkdir(actiondir)
print(len(channel_datas))
print(f"saving {ACTION} data...")
np.save(os.path.join(actiondir, f"{int(time.time())}.npy"), np.array(channel_datas))
print("done.")
for action in ['left', 'right', 'none']:
#print(f"{action}:{len(os.listdir(f'data/{action}'))}")
print(action, sum(os.path.getsize(f'data/{action}/{f}') for f in os.listdir(f'data/{action}'))/1_000_000, "MB")
print(ACTION, correct/total)
print(f"left: {left/total}, right: {right/total}, none: {none/total}")
with open("accuracies.csv", "a") as f:
f.write(f"{int(time.time())},{ACTION},{correct/total},{MODEL_NAME},{left/total},{right/total},{none/total}\n")