python之使用snowboy离线语音唤醒
python之使用snowboy離線語音喚醒
介紹
snowboy 是一個開源的、輕量級語音喚醒引擎,比較好用。
訓練語音模型網(wǎng)址:
https://snowboy.hahack.com/
環(huán)境
開發(fā)環(huán)境為: ubuntu 21.04
sudo apt-get install alsa-base sudo apt-get install alsa-utils sudo apt-get install libasound2-dev sudo apt-get install pulseaudio sudo apt-get install swig sudo apt-get install libatlas-base-dev sudo apt-get install pyaudio sudo apt-get install soxswig安裝不成功:
采用:swig安裝
pyaudio 開始播放音樂
sudo aplay test.wav獲取snowboy
使用
git clone https://github.com/Kitt-AI/snowboy.git或
git clone https://gitee.com/william_william/snowboy.git測試使用
首先使用命令
cd ../../swig/Python3 && make獲取對應該系統(tǒng)的_snowboydetect.so,不是一個系統(tǒng)的直接copy會報錯的。
打開snowboydecoder.py 文件,將代碼 from * import snowboydetect 改為 import snowboydetect 即可。
進入目錄 snowboy/examples/Python3 并運行以下命令:
python3 demo.py resources/models/snowboy.umdl創(chuàng)建一個demo
首先創(chuàng)建一個demo文件夾,
然后把resources文件夾、demo.py 、 snowboydecoder.py 、snowboydetect.py
_snowboydetect.so (_snowboydetect.so 在swig/Python3的目錄下) 復制到該文件夾,還有把訓練的模型也復制其中。
在該文件夾下運行以下命令:
python3 demo.py xxx.umdlIntegration
#!/bin/python3 # 直接 喚醒 并且執(zhí)行某個函數(shù) import snowboydecoder import sys import signal import os import subprocess as sub# 執(zhí)行 bash 類 # pip3 install subprocess class Run():# 初始化 參數(shù);args 為數(shù)組 ['python3','xx.py']def __init__(self,args,shell=True,encoding="utf-8"):self.args = argsself.shell = shellself.encoding =encoding# 處理 args 為一個字符串 def Handle(self):arg = ''for item in self.args:arg += item +' 'return arg# 執(zhí)行 命令行def run(self):res = self.Handle()res = sub.run(res,shell=self.shell,encoding=self.encoding)# 方便以后對其進行操作return res # 第二種: 不使用demo,直接對demo進行再封裝;只需要填寫model的文件名即可 class Rundev():def __init__(self,model,sensitivity=0.5,sleep_time=0.03):# 外置參數(shù)self.model = modelself.sensitivity = sensitivityself.sleep_time = sleep_time#內(nèi)置參數(shù) self.interrupted = Falsedef interrupt_callback(self):return self.interrupteddef signal_handler(self,signal, frame):self.interrupted = True def run(self):print('正在監(jiān)聽中.........','按 Ctrl+C 停止運行')# capture SIGINT signal, e.g., Ctrl+Csignal.signal(signal.SIGINT, self.signal_handler)detector = snowboydecoder.HotwordDetector(self.model, sensitivity =self.sensitivity)# main loopdetector.start(detected_callback=snowboydecoder.play_audio_file,interrupt_check=self.interrupt_callback,sleep_time=self.sleep_time)# 使終止detector.terminate()# 測試 if __name__ == "__main__":# os.getcwd()獲取當前工作路徑args = ['python3',os.getcwd()+"/python/snowBoyDemo/demo.py",os.getcwd()+"/python/snowBoyDemo/xiaoai.pmdl"]# dev = Run(args=args)# dev.run()dev = Rundev(os.getcwd()+"/python/snowBoyDemo/xiaoai.pmdl")dev.run()使用第二個類的可以把demo.py刪除了。
成功后:
如果想添加一些喚醒后的操作,打開snowboydecoder.py,第208行:
可以修改這里 這里是被喚醒之后運行的方法#small state machine to handle recording of phrase after keywordif state == "PASSIVE":if status > 0: #key word foundself.recordedData = []self.recordedData.append(data)silentCount = 0recordingCount = 0message = "Keyword " + str(status) + " detected at time: "message += time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))logger.info(message)print("執(zhí)行一個測試")callback = detected_callback[status-1]if callback is not None:callback()if audio_recorder_callback is not None:state = "ACTIVE"continue訓練語音模型
https://snowboy.hahack.com/
總結(jié)
以上是生活随笔為你收集整理的python之使用snowboy离线语音唤醒的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦幻西游python验证成语_GitHu
- 下一篇: 零知识证明从0到1,ZK简介