備忘錄_20160105(定位)
修改
回首頁
程式 2023-01-20 10:00:13 1674180013 100
語音辨識 speech / voice recognition。pocketsphinx。cmusphinx。python。 part II
語音辨識 speech / voice recognition。pocketsphinx。cmusphinx。python。 part II
import os
from pocketsphinx import LiveSpeech, get_model_path # pip install pocketsphinx
from playsound import playsound # pip install playsound
# 最簡短範例,會出現許多英文字,有的對,有的不對
print('example 0')
for phrase in LiveSpeech():
print(phrase)
if str(phrase).find('but')!=-1:
break
# 引用檔案,目前是引用英文的資源檔案
print('example 1')
speech1 = LiveSpeech(
sampling_rate=16000, # optional
hmm=get_model_path('C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\pocketsphinx\\model\\en-us\\en-us'),
dic=get_model_path('C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\pocketsphinx\\model\\en-us\\cmudict-en-us.dict'),
lm=get_model_path('C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\pocketsphinx\\model\\en-us\\en-us.lm.bin'),
)
for phrase in speech1:
print(phrase)
if str(phrase).find('but')!=-1:
break
# 單一關鍵詞的判斷率最高
print('example 2')
speech2 = LiveSpeech(
keyphrase='hi siri',
kws_threshold=1e-20, # 若設定為1,代表程式百分百認定一定沒錯;若設定為 1e-50,則一點點模糊的聲音,也可能會被認定為符合關鍵詞。
)
for phrase in speech2:
print(phrase)
break
# 可以自行設定關鍵詞的檔案
print('example 3')
speech3 = LiveSpeech(
hmm=get_model_path('C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\pocketsphinx\\model\\en-us\\en-us'),
dic=get_model_path('C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\pocketsphinx\\model\\en-us\\cmudict-en-us.dict'),
kws=get_model_path('d:\\speech3.kws'), #若有設定 kws,則 lm 就不能同時設定,會引發衝突。
)
for phrase in speech3:
print(phrase)
if str(phrase).find('ok google')!=-1:
playsound('example3.mp3') # 似乎要放在同一資料夾的樣子
break
'''
橫線底下是 speech3.kws 的內容,可用兩條斜線包夾 threshold。注意關鍵詞與斜線中間不要包含空格,不然回傳的關鍵詞也會包含空格。
關鍵詞要判斷的精確一點,最好音節多一點,準確率會高一點。
------------------------
hello world/5e-20/
happy ending/1e-20/
hi jarvis/1e-20/
ok google/1e-20/
hi siri/1e-20/
'''