【问题标题】:AttributeError: 'Recognizer' object has no attribute 'recognize'AttributeError:“识别器”对象没有“识别”属性
【发布时间】:2021-11-04 20:19:35
【问题描述】:

我正在尝试 Python(3.5 版)中的语音识别模块,但遇到以下错误:

'AttributeError: 'Recognizer' 对象没有属性'recognize''

这是我使用的代码:

import pyaudio
import speech_recognition as sr

r = sr.Recognizer()
r.energy_threshold=4000

with sr.Microphone() as source:
    audio = r.listen(source)

try:
    print("Speech was:" + r.recognize(audio))
except LookupError:
    print('Speech not understood')

我在 Stack Overflow 本身上找到了这段代码,并从给定 here 的语音识别模块中阅读了文档,但是由于某种原因它没有在我的系统中执行。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    根据documentation Recognizer 类没有方法recognize 这就是你得到的错误的含义,它来自

    print("Speech was:" + r.recognize(audio))
    

    这里你试图调用一个不存在的方法,你必须使用类提供的几个recognize_*方法之一,比如recognize_googlerecognize_witrecognize_ibm或@987654329 @

    【讨论】:

      【解决方案2】:

      方法已从recognize 更改为recognize_google。您的代码应该可以使用此更改

      import pyaudio
      import speech_recognition as sr
      
      r=sr.Recognizer()
      r.energy_threshold=4000
      
      with sr.Microphone() as source:
         audio=r.listen(source)
      
      try:
         print("Speech was:" + r.recognize_google(audio))
      except LookupError:
         print('Speech not understood')
      

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        您必须将r.recognize(audio) 更改为r.recognize_google(audio) 并以这种方式通过命令行运行它:python3x path_your_file.py

        【讨论】:

          猜你喜欢
          • 2016-06-05
          • 2018-12-13
          • 1970-01-01
          • 2021-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多