用于管理语音命令生命周期的python库。

command-lifecycle的Python项目详细描述


语音命令生命周期

code-climate-imagecircle-ci-imagecodecov-image

python库来管理语音命令的生命周期。使用Alexa语音服务非常有用。


安装

pip install command_lifecycle

尾迹探测器

wakeword是一个特定的词,它触发代码进入动作。它允许您的代码在发出特定单词之前处于空闲状态。

音频生命周期使用snowboy来确定是否发出了wakeword。需要先安装库。

编译完Snowboy后,将编译后的snowboy文件夹复制到项目的顶层。默认情况下,文件夹结构应为:

.
├── ...
├── snowboy
|   ├── snowboy-detect-swig.cc
|   ├── snowboydetect.py
|   └── resources
|       ├── alexa.umdl
|       └── common.res
└── ...

如果默认结构不适合您的需要,可以自定义wakeword detector

用法

您应该通过重复调用lifecycle.extend_audio(some_audio_bytes),向生命周期发送稳定的音频流。如果发出了诸如“alexa”(默认)或“ok,google”之类的wakeword,则调用handle_command_startedhandle_command_finised然后在wakeword后面的命令音频完成后调用。

麦克风音频

importpyaudioimportcommand_lifecycleclassAudioLifecycle(command_lifecycle.BaseAudioLifecycle):defhandle_command_started(self,wakeword_name):super().handle_command_started(wakeword_name)print(f'The audio contained {wakeword_name}!')defhandle_command_finised(self):super().handle_command_finised()print('The command has finished')lifecycle=AudioLifecycle()p=pyaudio.PyAudio()stream=p.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True)try:print('listening. Start by saying "Alexa". Press CTRL + C to exit.')whileTrue:lifecycle.extend_audio(stream.read(1024))finally:stream.stop_stream()stream.close()p.terminate()

文件音频

importwaveimportcommand_lifecycleclassAudioLifecycle(command_lifecycle.BaseAudioLifecycle):defhandle_command_started(self,wakeword_name):super().handle_command_started(wakeword_name)print(f'The audio contained {wakeword_name}!')lifecycle=AudioLifecycle()withwave.open('./tests/resources/alexa_what_time_is_it.wav','rb')asf:whilef.tell()<f.getnframes():lifecycle.extend_audio(f.readframes(1024))print('The command has finished')

与Alexa一起使用

command_lifecycle对于与语音服务交互很有用。生命周期一直等到发出wakeword,然后开始将音频命令流式传输到语音服务(使用Alexa Voice Service Client),然后对响应执行一些有用的操作:

fromavs_client.avs_client.clientimportAlexaVoiceServiceClientimportpyaudioimportcommand_lifecycleclassAudioLifecycle(command_lifecycle.BaseAudioLifecycle):alexa_client=AlexaVoiceServiceClient(client_id='my-client-id'secret='my-secret',refresh_token='my-refresh-token',)def__init__(self):self.alexa_client.connect()super().__init__()defhandle_command_started(self,wakeword_name):super().handle_command_started(wakeword_name)audio_file=command_lifecycle.to_audio_file()fordirectiveinself.alexa_client.send_audio_file(audio_file):# do something with the AVS audio response, e.g., play it.lifecycle=AudioLifecycle()p=pyaudio.PyAudio()stream=p.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True)try:print('listening. Start by saying "Alexa". Press CTRL + C to exit.')whileTrue:lifecycle.extend_audio(stream.read(1024))finally:stream.stop_stream()stream.close()p.terminate()

定制

唤醒字

默认的wakeword是“alexa”。这可以通过对command_lifecycle.wakeword.SnowboyWakewordDetector

fromcommand_lifecycleimportwakewordclassMySnowboyWakewordDetector(wakeword.SnowboyWakewordDetector):decoder_models=[{'name':'CUSTOM','model':b'path/to/custom-wakeword-model.umdl''sensitivity':b'0.5',}]classAudioLifecycle(lifecycle.BaseAudioLifecycle):audio_detector_class=MySnowboyWakewordDetectordefhandle_command_started(self,wakeword_name):super().handle_command_started(wakeword_name)print(f'The audio contained the {wakeword_name}!')defhandle_command_finised(self):super().handle_command_finised()print('The command has finished')lifecycle=AudioLifecycle()# now load the audio into lifecycle

有关创建自定义wakeword模型的步骤,请参见Snowboy docs

多个wakewords

可能需要针对不同的尾波触发不同的行为。为此,请在decoder_models

中使用多个项目
fromcommand_lifecycleimportwakewordclassMyMultipleWakewordDetector(wakeword.SnowboyWakewordDetector):GOOGLE='GOOGLE'decoder_models=wakeword.SnowboyWakewordDetector.decoder_models+[{'name':GOOGLE,'model':b'path/to/okgoogle.umdl','sensitivity':b'0.5',}]classAudioLifecycle(lifecycle.BaseAudioLifecycle):audio_detector_class=MyMultipleWakewordDetectordefhandle_command_started(self,wakeword_name):ifwakeword_name==self.audio_detector.ALEXA:print('Alexa standing by')elifwakeword_name==self.audio_detector.GOOGLE:print('Google at your service')super().handle_command_started(wakeword_name)

您可以从here下载wakewords。

尾迹探测器

Snowboy是默认的wakeword检测器。通过对command_lifecycle.wakeword.BaseWakewordDetector进行子分类并将wakeword_detector_class设置为自定义类,可以使用其他尾迹词检测器:

importwavefromcommand_lifecycleimportlifecycle,wakewordclassMyCustomWakewordDetector(wakeword.BaseWakewordDetector):import_error_message='Cannot import wakeword library!'wakeword_library_import_path='path.to.wakeword.Library'defwas_wakeword_uttered(self,buffer):# use the library to check if the audio in the buffer has the wakeword.# not `buffer.get()` returns the audio inside the buffer....defis_talking(self,buffer):# use the library to check if the audio in the buffer has audible words# not `buffer.get()` returns the audio inside the buffer....classAudioLifecycle(lifecycle.BaseAudioLifecycle):audio_detector_class=MyCustomWakewordDetector...lifecycle=AudioLifecycle()# now load the audio into lifecycle

处理输入数据

支持三种输入数据格式:

ConverterNotes
^{}default Input data is already wav bytes.
^{}Input data is list of integers.
^{}Input data is list of floats generated by a web browser.

通过设置生命周期的audio_converter_class


from command_lifecycle.helpers import WebAudioToWavConverter

class AudioLifecycle(lifecycle.BaseAudioLifecycle):
    audio_converter_class = WebAudioToWavConverter

期望更慢或更快的命令

发出音频命令的人可能需要花点时间收集他们的想法,然后才能完成命令。这种沉默可以解释为命令结束,导致过早地调用handle_command_finised

为了避免这种情况,生命周期允许在命令的生命周期超时之前在命令中保持一些静默。这种沉默可能发生在命令的开头或中间。注意,这样做的一个副作用是,在该人停止说话和调用handle_command_finised之间会有一个停顿。

要更改此默认行为,可以更改timeout_manager_class。可用的超时管理器是:

Timeout managerNotes
^{}Allows one second of silence.
^{}default Allows 2 seconds of silence.
^{}Allows three seconds of silence.

要使自定义超时管理器创建一个子类command_lifecycle.timeout.BaseTimeoutManager

importwavefromcommand_lifecycleimporttimeout,wakewordclassMyCustomTimeoutManager(timeout.BaseTimeoutManager):allowed_silent_seconds=4classAudioLifecycle(lifecycle.BaseAudioLifecycle):timeout_manager_class=MyCustomTimeoutManager

单元测试

要运行单元测试,请调用以下命令:

make test_requirements
make test

版本控制

我们使用SemVer进行版本控制。有关可用的版本,请参见PyPI

其他项目

这个库由alexa-browser-client使用,它允许您从浏览器与alexa对话。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaGWT:何时使用Lazydemelement?   Java中跟踪消失线程的多线程处理   java Springboot未能配置数据源:“url”,但我没有使用数据库   java为按钮生成随机位置   math Java:包含二项式系数计算的表达式   java通过AsyncTask传递参数   从路径错误创建java文件   高流量网站的性能播放框架、Java、Apache、PostgreSQL、JPA和Hibernate   java将4D矢量转换为长矢量   arraylist Java循环在没有任何错误的情况下终止   java正在制作一个计算器应用程序,希望在第二个片段中更新历史,但无法完成   java将信息从IntentService发送到Activity   java如何在游戏中插入大量实体!工作   javascript如何在ScriptEngineforJava中从数学中获得准确的结果?