-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_gatherer.py
171 lines (127 loc) · 4.25 KB
/
data_gatherer.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
__author__ = 'igor sieradzki'
from emokit.emotiv import Emotiv
import gevent
from datetime import datetime
from time import time, sleep
import os
BUFFER_CAPACITY = 100000000000
TEST = False
def console_gather() :
os.system('clear')
headset = Emotiv()
gevent.spawn(headset.setup)
gevent.sleep(0)
packets = 0
now = datetime.now()
filename = str(now.time()) + "_" + str(now.date())
while True :
dir = str(input("Choose input: \n 1. up\n 2. down\n 3. left\n 4. right\n 0. neutral\n"))
if dir in ['1','2','3','4','0'] : break
filename += "_" + dir
if TEST : filename = "TEST_" + filename
buffers = []
names = 'AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4'.split(' ')
for name in names :
buffers.append(sensor_buffer(name))
print "Training will start in..."; sleep(1); print "3..."; sleep(1); print "2..."; sleep(1); print "1..."; sleep(1); print "Focus!"
qualities = []
timeout = time() + 12
while True:
if time() > timeout :
break
packet = headset.dequeue()
for buffer in buffers :
buffer.update( packet )
packets += 1
gevent.sleep(0)
headset.close()
quality = 0.
f = open("./data/" + filename,'w')
columns = []
for name in names :
columns.append(str(name))
columns.append('Q' + str(name))
f.write(','.join(columns))
f.write('\n')
while packets > 0 :
for buffer in buffers :
f.write( buffer.pop() )
f.write('\n')
packets -= 1
f.close()
print "Finished reading, saved to file %s" % filename
for buffer in buffers :
print "Sensor %s mean quality: %.2f" % (buffer.name, buffer.mean_quality())
def class_gather(label, headset) :
packets = 0
now = datetime.now()
filename = str(now.time()) + "_" + str(now.date())
filename += "_" + label
buffers = []
names = 'AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4'.split(' ')
for name in names :
buffers.append(sensor_buffer(name))
if label == '0' : print "> Neutral position"
elif label == '1' : print "> Forward Movement (UP)"
elif label == '2' : print "> Backward Movement (DOWN)"
elif label == '3' : print "> Left Steering (LEFT)"
elif label == '4' : print "> Right Steering (RIGHT)"
print "Training will start in..."; sleep(1); print "3..."; sleep(1); print "2..."; sleep(1); print "1..."; sleep(1); print "Focus!"
qualities = []
timeout = time() + 12
while True:
if time() > timeout :
break
packet = headset.dequeue()
for buffer in buffers :
buffer.update( packet )
packets += 1
gevent.sleep(0)
f = open("./data/" + filename,'w')
columns = []
for name in names :
columns.append(str(name))
columns.append('Q' + str(name))
f.write(','.join(columns))
f.write('\n')
while packets > 0 :
for buffer in buffers :
f.write( buffer.pop() )
f.write('\n')
packets -= 1
f.close()
def session() :
headset = Emotiv()
gevent.spawn(headset.setup)
gevent.sleep(0)
labels = ['0', '1', '2', '3', '4']
for label in labels :
os.system('clear')
print "Training for class: ", label
raw_input("Press Enter to start training..." )
class_gather(label, headset)
headset.close()
class sensor_buffer :
def __init__(self, name) :
self.name = name
self.buffer = []
self.sum_quality = 0.
self.n_readings = 0
def update(self, packet) :
if len(self.buffer) > BUFFER_CAPACITY :
raise Exception('Buffer overflow')
self.buffer.append( [packet.sensors[self.name]['value'], packet.sensors[self.name]['quality']] )
self.sum_quality += int(packet.sensors[self.name]['quality'])
self.n_readings += 1
def pop(self) :
# if len( self.buffer > 0 ) :
data = self.buffer.pop()
return str( reading(data) )
def mean_quality(self) :
return self.sum_quality / self.n_readings
class reading :
def __init__(self, data):
self.value = data[0]
self.quality = data[1]
def __repr__(self) :
return "%i,%.1f," % ( self.value, self.quality )