Emotiv EEG Cortex 2 API的综合线程化、异步Python客户端
项目描述
Emotiv Cortex2 Python客户端
作者: methylDragon
图片来源: Emotiv
简介
Emotiv EEG Cortex 2 API的非官方Python客户端。
特点是通过异步WebSocket进行整个JSON-RPC API通信,以提高速度。
Cortex 2应用程序用于托管WebSocket Web服务器网关接口,该接口接收JSON请求并返回JSON数据。
此Python客户端被设计为该API的包装客户端。它使用单独的异步线程流式传输多会话传感器数据!
API参考: https://emotiv.gitbook.io/cortex-api/
PyPI链接: https://pypi.ac.cn/manage/project/cortex2
支持我的努力!
要求
- Python 3.6或更高版本
安装
pip install cortex2
示例用法
请记住,通过 注册 来设置您的客户端ID和密钥。
同时确保您已启动EmotivApp,因为它托管WebSocket服务器!(您可能需要使用Windows或Mac。)
如果客户端无法连接,您可能需要使用客户端重新启动脚本。
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:
pass
您也可以通过明确指定ID来连接!
client.connect_headset(headset_id="EPOCPLUS-3B9AXXXX")
client.create_session(headset_id="EPOCPLUS-3B9AXXXX")
您可以做更多的事情!整个API都得到涵盖,并且一切通常都以相同的方式进行。
其他显著功能
- 将WebSocket API包装在辅助函数中,以执行请求ID检查、异步消息处理和某些基本数据清理。
- 线程用于尝试允许订阅多个会话和多个耳机。
- 线程有一个辅助函数,可以自动分割数据流,这样您可以独立地处理流,而不是将它们全部作为一个对象处理。
- 当调用相关方法时,将触发自动同步辅助方法以检测现有的会话和耳机。(例如,当尝试更新、连接或断开耳机时,客户端将自动查询和更新已看到的耳机字典。)
显著的类属性
名称 | 类型 | 描述 | 示例 |
---|---|---|---|
debug | bool | 调试标志。如果设置,则会减慢数据订阅速率。 | |
client_id | str | 客户端 ID | |
client_secret | str | 客户端密钥 | |
cortex_token | str | Cortex 令牌 | |
approved | bool | 如果访问被授权则为 True | |
authorized | bool | 如果已颁发 cortex 令牌则为 True | |
headsets | OrderedDict() | 已看到的耳机对象字典的 OrderedDict | |
connected_headsets | OrderedDict() | OrderedDict of connected headset object dicts | |
sessions | OrderedDict() | 已看到的会话对象字典的 OrderedDict | |
data_deque_size | int | 每个订阅者数据缓冲区的最大大小 | |
data_streams | dict | 按会话键的传感器数据流 | {session_id: {data_stream_deques_types: [data]}, ...} |
subscribed_streams | dict | 按会话键的已订阅数据流名称和描述 | {session_id: {stream_names: info}, ...} |
subscriber_spinning | bool | 跟踪订阅者线程是否正在运行 | |
subscriber_messages_handled | int | 统计已处理的订阅者消息数量 | |
subscriber_reading | threading.Event() | 事件标志,用于暂停订阅者而不会终止它 |
类方法
类辅助函数
send_authed_request(method=None, params={}, request=None)
send_request(method=None, params={}, request=None)
set_client_id(client_id)
set_client_secret(client_secret)
_verify_key(key, response, field=None)
)_exit_handler(_signo, _stack_frame)
连接到 Cortex API
get_cortex_info()
身份验证
-
get_user_login()
-
request_access()
-
has_access_right()
-
authorize()
-
generate_new_token()
-
get_user_information()
-
get_license_info()
-
辅助函数
authenticate()
耳机
-
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)
-
辅助函数
sync_headsets(response=None, query=True)
_get_headset_type(headset_id)
会话
-
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)
-
辅助函数
sync_sessions(response=None, query=True)
数据订阅
-
subscribe(streams, session_id_idx=0, session_id=None)
-
unsubscribe(streams, session_id_idx=0, session_id=None)
-
辅助函数
receive_data()
spin_subscriber()
stop_subscriber()
pause_subscriber()
resume_subscriber()
_subscriber_thread()
_create_data_streams(session_id)
_delete_data_streams(session_id)
_split_and_update_data_streams(data_sample)
记录
create_record(title, session_id_idx=0, session_id=None, description=None, subject_name=None, tags=None)
stop_record(title, session_id_idx=0, session_id=None)
update_record(record_id, description=None, tags=None)
delete_record(record_ids)
export_record(record_ids, folder, format, stream_types, version=None)
export_edf(record_ids, folder, stream_types)
export_csv_v1(record_ids, folder, stream_types)
export_csv_v2(record_ids, folder, stream_types)
query_records(query, order_by={'startDatetime': "DESC"}, limit=0, offset=0, include_markers=False)
get_record_infos(record_ids)
标记
inject_marker(time, value, label, session_id_idx=0, session_id=None, port=None, extras=None)
update_marker(标记_id, 时间, 会话_id_idx=0, 会话_id=None, 附加信息=None)
BCI
查询配置文件()
获取当前配置文件(耳机_id_idx=0, 耳机_id=None)
设置配置文件(状态, 配置文件, 耳机_id_idx=0, 耳机_id=None, 新配置文件名=None)
创建配置文件(配置文件)
加载配置文件(配置文件, 耳机_id_idx=0, 耳机_id=None)
卸载配置文件(配置文件, 耳机_id_idx=0, 耳机_id=None)
保存配置文件(配置文件)
重命名配置文件(配置文件, 耳机_id_idx=0, 耳机_id=None, 新配置文件名=None)
删除配置文件(配置文件)
加载访客配置文件(耳机_id_idx=0, 耳机_id=None)
获取检测信息(检测)
获取心理命令信息()
获取面部表情信息()
训练(检测, 状态, 动作, 会话_id_idx=0, 会话_id=None)
开始训练(检测, 动作, 会话_id_idx=0, 会话_id=None)
接受训练(检测, 动作, 会话_id_idx=0, 会话_id=None)
拒绝训练(检测, 动作, 会话_id_idx=0, 会话_id=None)
重置训练(检测, 动作, 会话_id_idx=0, 会话_id=None)
擦除训练(检测, 动作, 会话_id_idx=0, 会话_id=None)
高级BCI
获取训练签名动作(检测, 配置文件=None, 会话=None)
获取训练时间(检测, 会话_id_idx=0, 会话_id=None)
面部表情签名类型(状态, 配置文件=None, 会话=None, 签名=None)
获取面部表情签名类型(配置文件=None, 会话=None)
设置面部表情签名类型(配置文件=None, 会话=None, 签名=None)
面部表情阈值(状态, 动作, 配置文件=None, 会话=None, 值=None)
获取面部表情阈值(动作, 配置文件=None, 会话=None)
设置面部表情阈值(动作, 配置文件=None, 会话=None, 值=None)
心理命令活动动作(状态, 配置文件=None, 会话=None, 动作=[])
获取心理命令活动动作(配置文件=None, 会话=None)
设置心理命令活动动作(配置文件=None, 会话=None, 动作=[])
心理命令大脑映射(配置文件=None, 会话=None)
心理命令获取技能评级(配置文件=None, 会话=None, 动作=None)
心理命令训练阈值(配置文件=None, 会话=None)
心理命令动作等级(状态, 配置文件=None, 会话=None, 等级=None)
获取心理命令动作等级(配置文件=None, 会话=None)
设置心理命令动作等级(配置文件=None, 会话=None, 等级=None)
心理命令动作灵敏度(状态, 配置文件=None, 会话=None, 值=[])
获取心理命令动作灵敏度(状态, 配置文件=None, 会话=None)
设置心理命令动作灵敏度(状态, 配置文件=None, 会话=None, 值=[])
EmotivCortex2Client 更新日志
v1.0.1 - 2019_07_09
更改
- 修复子模块导入错误
v1.0.0 - 2019_07_09
新增
- 初始发布
- 带有测试异步性的WebsocketClient
- 带有完整API的EmotivCortex2Client
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分布
构建分布
cortex2-1.0.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7ec8d211815ab355ee79d5a64a4850f97ec442f6282c1402c889a667cb642570 |
|
MD5 | 7487d8a8ba7aff32edd2b61aead1ea86 |
|
BLAKE2b-256 | de44e3c12cd8fba058d659a36fe68378ca6b166fdd4b2329f690a556300df0ce |