-
Notifications
You must be signed in to change notification settings - Fork 0
/
MGEmotiveLogger.py
247 lines (199 loc) · 8.87 KB
/
MGEmotiveLogger.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
import sys,os
import time
import ctypes
from ctypes import *
import threading
import logging
import MGUtil
import datetime
import dateutil.parser
VERBOSITY = False
DEBUG = False
GLOBAL_PARAMS = {}
class
try:
if sys.platform.startswith('win32'):
libEDK = cdll.LoadLibrary("edk.dll")
if sys.platform.startswith('linux'):
srcDir = os.getcwd()
libPath = srcDir = "/libedk.so.1.0.0"
libEDK = CDLL(libPath)
except:
raise MGHeadSetError('Cannot load edk dll/lib')
#------------------------------------------------------------------------------
EE_EmoEngineEventCreate = libEDK.EE_EmoEngineEventCreate
EE_EmoEngineEventCreate.restype = c_void_p
EE_EmoEngineEventGetEmoState = libEDK.EE_EmoEngineEventGetEmoState
EE_EmoEngineEventGetEmoState.argtypes = [c_void_p, c_void_p]
EE_EmoEngineEventGetEmoState.restype = None
EE_EmoEngineEventGetUserId = libEDK.EE_EmoEngineEventGetUserId
EE_EmoEngineEventGetUserId.argtypes = [c_void_p, POINTER(c_uint)]
EE_EmoEngineEventGetUserId.restype = None
EE_EmoEngineEventGetType = libEDK.EE_EmoEngineEventGetType
EE_EmoEngineEventGetType.argtypes = [c_void_p]
EE_EmoEngineEventGetType.restype = c_int
EE_EngineGetNextEvent = libEDK.EE_EngineGetNextEvent
EE_EngineGetNextEvent.argtypes = [c_void_p]
EE_EngineGetNextEvent.restype = c_int
ES_GetTimeFromStart = libEDK.ES_GetTimeFromStart
ES_GetTimeFromStart.argtypes = [ctypes.c_void_p]
ES_GetTimeFromStart.restype = c_float
EE_EmoStateCreate = libEDK.EE_EmoStateCreate
EE_EmoStateCreate.restype = c_void_p
ES_GetWirelessSignalStatus = libEDK.ES_GetWirelessSignalStatus
ES_GetWirelessSignalStatus.restype = c_int
ES_GetWirelessSignalStatus.argtypes = [c_void_p]
ES_ExpressivIsBlink = libEDK.ES_ExpressivIsBlink
ES_ExpressivIsBlink.restype = c_int
ES_ExpressivIsBlink.argtypes = [c_void_p]
ES_ExpressivIsLeftWink = libEDK.ES_ExpressivIsLeftWink
ES_ExpressivIsLeftWink.restype = c_int
ES_ExpressivIsLeftWink.argtypes = [c_void_p]
ES_ExpressivIsRightWink = libEDK.ES_ExpressivIsRightWink
ES_ExpressivIsRightWink.restype = c_int
ES_ExpressivIsRightWink.argtypes = [c_void_p]
ES_ExpressivIsLookingLeft = libEDK.ES_ExpressivIsLookingLeft
ES_ExpressivIsLookingLeft.restype = c_int
ES_ExpressivIsLookingLeft.argtypes = [c_void_p]
ES_ExpressivIsLookingRight = libEDK.ES_ExpressivIsLookingRight
ES_ExpressivIsLookingRight.restype = c_int
ES_ExpressivIsLookingRight.argtypes = [c_void_p]
ES_ExpressivGetUpperFaceAction = libEDK.ES_ExpressivGetUpperFaceAction
ES_ExpressivGetUpperFaceAction.restype = c_int
ES_ExpressivGetUpperFaceAction.argtypes = [c_void_p]
ES_ExpressivGetUpperFaceActionPower = libEDK.ES_ExpressivGetUpperFaceActionPower
ES_ExpressivGetUpperFaceActionPower.restype = c_float
ES_ExpressivGetUpperFaceActionPower.argtypes = [c_void_p]
ES_ExpressivGetLowerFaceAction = libEDK.ES_ExpressivGetLowerFaceAction
ES_ExpressivGetLowerFaceAction.restype = c_int
ES_ExpressivGetLowerFaceAction.argtypes = [c_void_p]
ES_ExpressivGetLowerFaceActionPower = libEDK.ES_ExpressivGetLowerFaceActionPower
ES_ExpressivGetLowerFaceActionPower.restype = c_float
ES_ExpressivGetLowerFaceActionPower.argtypes = [c_void_p]
ES_AffectivGetExcitementShortTermScore = libEDK.ES_AffectivGetExcitementShortTermScore
ES_AffectivGetExcitementShortTermScore.restype = c_float
ES_AffectivGetExcitementShortTermScore.argtypes = [c_void_p]
ES_AffectivGetExcitementLongTermScore = libEDK.ES_AffectivGetExcitementLongTermScore
ES_AffectivGetExcitementLongTermScore.restype = c_float
ES_AffectivGetExcitementLongTermScore.argtypes = [c_void_p]
ES_AffectivGetEngagementBoredomScore = libEDK.ES_AffectivGetEngagementBoredomScore
ES_AffectivGetEngagementBoredomScore.restype = c_float
ES_AffectivGetEngagementBoredomScore.argtypes = [c_void_p]
ES_CognitivGetCurrentAction = libEDK.ES_CognitivGetCurrentAction
ES_CognitivGetCurrentAction.restype = c_int
ES_CognitivGetCurrentAction.argtypes = [c_void_p]
ES_CognitivGetCurrentActionPower = libEDK.ES_CognitivGetCurrentActionPower
ES_CognitivGetCurrentActionPower.restype = c_float
ES_CognitivGetCurrentActionPower.argtypes = [c_void_p]
EE_EmoStateUpdated = 64 # libEDK.EE_Event_enum.EE_EmoStateUpdated
EDK_OK = 0
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
def logEmoState(userID, eState):
print >>f, ES_GetTimeFromStart(eState), ", ",
print >>f, userID.value, ", ",
print >>f, ES_GetWirelessSignalStatus(eState), ", ",
print >>f, ES_ExpressivIsBlink(eState), ", ",
print >>f, ES_ExpressivIsLeftWink(eState), ", ",
print >>f, ES_ExpressivIsRightWink(eState), ", ",
print >>f, ES_ExpressivIsLookingLeft(eState), ", ",
print >>f, ES_ExpressivIsLookingRight(eState), ", ",
expressivStates = {}
expressivStates[EXP_EYEBROW] = 0
expressivStates[EXP_FURROW] = 0
expressivStates[EXP_SMILE] = 0
expressivStates[EXP_CLENCH] = 0
expressivStates[EXP_SMIRK_LEFT] = 0
expressivStates[EXP_SMIRK_RIGHT] = 0
expressivStates[EXP_LAUGH] = 0
upperFaceAction = ES_ExpressivGetUpperFaceAction(eState)
upperFacePower = ES_ExpressivGetUpperFaceActionPower(eState)
lowerFaceAction = ES_ExpressivGetLowerFaceAction(eState)
lowerFacePower = ES_ExpressivGetLowerFaceActionPower(eState)
expressivStates[upperFaceAction] = upperFacePower
expressivStates[lowerFaceAction] = lowerFacePower
print >>f, expressivStates[EXP_EYEBROW], ", ",
print >>f, expressivStates[EXP_FURROW], ", ",
print >>f, expressivStates[EXP_SMILE], ", ",
print >>f, expressivStates[EXP_CLENCH], ", ",
print >>f, expressivStates[EXP_SMIRK_LEFT], ", ",
print >>f, expressivStates[EXP_SMIRK_RIGHT], ", ",
print >>f, expressivStates[EXP_LAUGH], ", ",
# Affectiv Suite results
print >>f, ES_AffectivGetExcitementShortTermScore(eState), ", ",
print >>f, ES_AffectivGetExcitementLongTermScore(eState), ", ",
print >>f, ES_AffectivGetEngagementBoredomScore(eState), ", ",
# Cognitiv Suite results
print >>f, ES_CognitivGetCurrentAction(eState), ", ",
print >>f, ES_CognitivGetCurrentActionPower(eState)
#-------------------------------------------------------------------------------
EXP_EYEBROW = 0x0020 # eyebrow
EXP_FURROW = 0x0040 # furrow
EXP_SMILE = 0x0080 # smile
EXP_CLENCH = 0x0100 # clench
EXP_SMIRK_LEFT = 0x0400 # smirk left
EXP_SMIRK_RIGHT = 0x0800 # smirk right
EXP_LAUGH = 0x0200 # laugh
#------------------------------------------------------------------------------
header = ['Time', 'UserID', 'Wireless Signal Status', 'Blink', 'Wink Left',
'Wink Right', 'Look Left', 'Look Right', 'Eyebrow', 'Furrow',
'Smile', 'Clench', 'Smirk Left', 'Smirk Right', 'Laugh',
'Short Term Excitement', 'Long Term Excitement',
'Engagement/Boredom', 'Cognitiv Action', 'Cognitiv Power']
#------------------------------------------------------------------------------
USER_ID = 0
def Setup(params, verb, deb):
GLOBAL_PARAMS = params
VERBOSITY = verb
DEBUG = deb
if libEDK.EE_EngineConnect("Emotiv Systems-5") != 0:
raise MGHeadSetError("Emotive Engine start up failed.")
USER_ID = c_uint(params['userID'])
STOP_COLLECTION = False
EMO_DATA = []
RAW_DATA = []
def EmoCollector():
eEvent = EE_EmoEngineEventCreate()
eState = EE_EmoStateCreate()
while not STOP_COLLECTION:
time.sleep(.1)
state = EE_EngineGetNextEvent(eEvent)
EE_EmoEngineEventGetUserId(eEvent, pointer(userID))
if eventType == EE_EmoStateUpdated:
EE_EmoEngineEventGetEmoState(eEvent, eState)
timestamp = dateutil.parser.parse(ES_GetTimeFromStart(eState))
if VERBOSITY or DEBUG:
logging.Info("%10.3f New EmoState from user %d ...\r" % (timestamp, USER_ID)
if DEBUG:
logEmoState(USER_ID, eState)
EMO_DATA.append({'userID':USER_ID, 'eState':eState, 'timestamp':timestamp})
elif state != 0x600:
print "Internal error in Emotive Engine"
#special keys
#avi fill this in
def RawCollector():
while not STOP_COLLECTION:
time.sleep(.0078125) #may need to change to nanosleep
raw = SomeFuncGetRawData()
timestamp = datetime.datetime.now()
if VERBOSITY or DEBUG:
pass #log extra data
if DEBUG:
pass #log extra data
RAW_Data.append({'userID':USER_ID, 'raw':raw, 'timestamp':timestamp})
def StartLog():
t = threading.Thread(target=EmoCollector)
t = threading.Thread(target=RawCollector)
t.start()
def SaveRawData():
data = {}
with open(GLOBAL_PARAMS['infoFile'], "r") as data_file:
data = json.load(data_file)
data['RAW_EMO_DATA'] = RAW_DATA
data['RAW_EMO_STATE_DATA'] = EMO_DATA
with open(GLOBAL_PARAMS['infoFile'], 'w') as data_file:
json.dump(data, data_file)
def StopLog():
STOP_COLLECTION = True
time.sleep(.1)
SaveRawData()
return (EMO_DATA, RAW_DATA)