1

我正在做一个项目,让计算机在我发出命令时回复它。我刚开始,但是当我运行代码时,它没有以我期望的方式响应。这是我的代码:

from pocketsphinx import LiveSpeech
import os
for phrase in LiveSpeech(): print(phrase)
if phrase == 'oh' :
    os.system('espeak' ' "hi"')

运行此代码时出现错误。这是错误:

Traceback (most recent call last):
  File "xxt", line 4, in <module>
    print(phrase)
  File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 93, in __str__
    return self.hypothesis()
  File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 126, in hypothesis
    hyp = self.hyp()
  File "/Library/Python/2.7/site-packages/pocketsphinx/pocketsphinx.py", line 359, in hyp
    return _pocketsphinx.Decoder_hyp(self)
  File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 225, in stop
    raise StopIteration
StopIteration

谢谢,阿迪亚

4

1 回答 1

1

这是因为 if 语句不在循环中,循环体是 print 语句

for phrase in LiveSpeech():
    print(phrase)
    if phrase == 'oh' :
        os.system('espeak' ' "hi"')

这就是它应该的样子

于 2017-08-15T07:19:18.953 回答