| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
Author: methylDragon
Image sources: Emotiv
Unofficial Python client for the Emotiv EEG Cortex 2 API.
Features the entire JSON-RPC API communicated via asynchronous websockets for speed.
The Cortex 2 app is used to host a websocket web server gateway interface that takes JSON requests and returns JSON data.
This Python client is designed to be a wrapper client for said API. It streams multi-session sensor data using a separate asynchronous thread!
API Reference: https://emotiv.gitbook.io/cortex-api/
PyPi Link: https://pypi.org/project/cortex2/
pip install cortex2Remember to set up your client ID and secret by registering
Also ensure you've started the EmotivApp, since it hosts the websocket server! (You'll probably have to use Windows or Mac.)
If the client fails to connect, you might have to restart the script using the client.
from cortex2 import EmotivCortex2Client
url = "wss://localhost:6868"
# Remember to start the Emotiv App before you start!
# Start client with authentication
client = EmotivCortex2Client(url,
client_id='CLIENT_ID',
client_secret="CLIENT_SECRET",
check_response=True,
authenticate=True,
debug=False)
# Test API connection by using the request access method
client.request_access()
# Explicit call to Authenticate (approve and get Cortex Token)
client.authenticate()
# Connect to headset, connect to the first one found, and start a session for it
client.query_headsets()
client.connect_headset(0)
client.create_session(0)
# Subscribe to the motion and mental command streams
# Spins up a separate subscription thread
client.subscribe(streams=["mot", "com"])
# Test message handling speed
a = client.subscriber_messages_handled
time.sleep(5)
b = client.subscriber_messages_handled
print((b - a) / 5)
# Grab a single instance of data
print(client.receive_data())
# Continously grab data, while making requests periodically
while True:
counter += 1
# time.sleep(0.1)
if counter % 5000 == 0:
print(client.request_access())
# Try stopping the subscriber thread
if counter == 50000:
client.stop_subscriber()
break
try:
# Check the latest data point from the motion stream, from the first session
print(list(client.data_streams.values())[0]['mot'][0])
except:
passYou can also connect by explicitly stating IDs!
client.connect_headset(headset_id="EPOCPLUS-3B9AXXXX")
client.create_session(headset_id="EPOCPLUS-3B9AXXXX")You can do a lot more! The entire API is covered, and everything generally works the same way.
| Name | Type | Description | Example |
|---|---|---|---|
| debug | bool | Debug flag. Slows down data subscription rate if set. | |
| client_id | str | Client ID | |
| client_secret | str | Client Secret | |
| cortex_token | str | Cortex Token | |
| approved | bool | True if access is granted | |
| authorized | bool | True if cortex token is issued | |
| headsets | OrderedDict() | OrderedDict of headset object dicts seen | |
| connected_headsets | OrderedDict() | OrderedDict of connected headset object dicts | |
| sessions | OrderedDict() | OrderedDict of seen session object dicts | |
| data_deque_size | int | Maximum size of each subscriber data buffer | |
| data_streams | dict | Sensor data streams keyed by session | {session_id: {data_stream_deques_types: [data]}, ...} |
| subscribed_streams | dict | Data stream names and descriptions that are subscribed, keyed by session | {session_id: {stream_names: info}, ...} |
| subscriber_spinning | bool | Tracks whether the subscriber thread is running | |
| subscriber_messages_handled | int | Counts how many subscriber messages have been handled | |
| subscriber_reading | threading.Event() | Event flag to pause the subscriber without killing it |
get_user_login()
request_access()
has_access_right()
authorize()
generate_new_token()
get_user_information()
get_license_info()
Helpers
query_headsets(id=None, sync=True)
control_device(command, headset_id=None, mappings=None)
refresh_headsets()
connect_headset(headset_id_idx=0, headset_id=None, mappings=None)
disconnect_headset(headset_id)
update_headset(settings, headset_id_idx=0, headset_id=None)
maximise_headset(headset_id_idx=0, headset_id=None)
maximise_headset_no_motion(headset_id_idx=0, headset_id=None)
Helpers
create_session(headset_id_idx=0, headset_id=None)
create_activated_session(headset_id_idx=0, headset_id=None)
update_session(status, session_id_idx=0, session_id=None)
activate_session(session_id_idx=0, session_id=None)
close_session(session_id_idx=0, session_id=None)
query_sessions(sync=True)
Helpers
subscribe(streams, session_id_idx=0, session_id=None)
unsubscribe(streams, session_id_idx=0, session_id=None)
| Back | FazBrowse Home | New Git URL |