Emotiv / cortex-example

Example with Cortex V2/V3 API
https://emotiv.com
MIT License
202 stars 115 forks source link

KeyError while loading commands from Emotiv insight. #39

Closed Rakesh420 closed 5 years ago

Rakesh420 commented 5 years ago

I am trying to access the getDetectionInfo to read the mental commands but I am having a problem after that. I am new to programming and I need help on how to solve this.

{"id":411114,"jsonrpc":"2.0","result":{"actions":["push"],"message":"Set MentalCommand active actions successfully"}} {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649902.0082} 1 neutral {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649902.2596} 2 neutral {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649902.5101} 3 neutral {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649902.7606} 4 neutral {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649903.011} 5 neutral {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649903.2607} 6 neutral {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649903.5115} 7 neutral {'com': ['neutral', 0], 'sid': '81cd0e6e-e530-4552-a77c-14e9e7d1ada8', 'time': 1567649903.7623} Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Rakesh Chandrakant/.spyder-py3/eegrover.py', wdir='C:/Users/Rakesh Chandrakant/.spyder-py3')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Rakesh Chandrakant/.spyder-py3/eegrover.py", line 223, in thought = json.loads(ws.recv())["com"][0]

KeyError: 'com'

bchaperon commented 5 years ago

Hello @Rakesh420 I am not a Python expert but it seems that this error comes from your source code, not from Cortex.

At this line: thought = json.loads(ws.recv())["com"][0] You try to get the key "com" from the json object but this object doesn't contain this key. So Python returns an error. First you should load the json and then test if the key exist or not. If the key "com" doesn't exist then this json object is not a mental command.