【发布时间】:2019-06-28 06:04:27
【问题描述】:
语音转文本后如何触发命令?现在我有文本,但无法在 python 中使用 if 条件比较它,所以我可以执行我需要的任务。我在树莓派中使用 pocketsphinx 进行语音识别。
import os
from pocketsphinx import LiveSpeech, get_model_path
model_path = get_model_path()
speech = LiveSpeech(
verbose=False,
sampling_rate=16000,
buffer_size=2048,
no_search=False,
full_utt=False,
hmm=os.path.join(model_path, 'en-us'),
lm=os.path.join(model_path, 'en-us.lm.bin'),
dic=os.path.join(model_path, 'cmudict-en-us.dict')
)
for phrase in speech:
print(phrase)
if phrase == "HOME"
print (OK)
代码没有给出任何错误并且工作正常;我说它打印在屏幕上,即代码工作到最后 3 行 [print (phrase)] 并给出预期结果,但最后 2 行没有执行所需的任务,但没有给出错误
【问题讨论】:
-
它到底打印了什么?代码看起来是正确的,除了 OK(它是变量还是你的意思是“OK”?),也许你必须考虑大小写(
phrase.lower() == 'home')或检查不完全相等('home' in phrase.lower(),phrase.lower().startswith('home'))跨度> -
感谢您的考虑;我附上了结果,我考虑了单词“HOME”的情况,而 OK 我的意思是单词“OK”而不是变量
-
查看文档我已经看到什么短语不是字符串。将
print(phrase)更改为print(phrase, type(phrase), dir(phrase))并显示结果,它可能会提示如何比较它或从中获取字符串 -
非常感谢您的回答;没错,它不是字符串;当我打印 (type(phrase)) 时,它显示 [
] 你能指导我如何比较类和字符串吗?由于我是编程菜鸟,当我输入 [dir(phrase)] 时,它会显示复杂目录的数量。
标签: python speech-recognition pocketsphinx