【问题标题】:Speech recognition for Python 3.4 thats easy to work?Python 3.4 的语音识别好用吗?
【发布时间】:2015-11-26 09:32:10
【问题描述】:

我希望获得一个简单有效的语音识别。我一直在speech_recognition上看这个,当我执行代码时出现以下错误

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

try:
    print("You said " + r.recognize(audio))    
except LookupError:                            




print("You said " + r.recognize(audio))    # recognize speech using Google       Speech Recognition
AttributeError: 'Recognizer' object has no attribute 'recognize'

    print("Could not understand audio")

这是从他们网页上的示例中复制的

【问题讨论】:

    标签: python python-3.x speech-recognition google-speech-api


    【解决方案1】:

    我也遇到了同样的问题。问题是我没有设置最低阈值水平。所以我添加了这段代码。

    import speech_recognition as sr
    r = sr.Recognizer()
    m = sr.Microphone()
    #set threhold level
    with m as source: r.adjust_for_ambient_noise(source)
    print("Set minimum energy threshold to {}".format(r.energy_threshold))
    # obtain audio from the microphone
    
    with sr.Microphone() as source:
        print("Say something!")
        audio = r.listen(source)
    
    print(r.recognize_google(audio))
    

    现在完美运行!!!

    【讨论】:

      【解决方案2】:

      我让它工作了。

      import speech_recognition as sr
      
      # obtain audio from the microphone
      r = sr.Recognizer()
      with sr.Microphone() as source:
          print("Say something!")
          audio = r.listen(source)
      
      print(r.recognize_google(audio))
      

      【讨论】:

        【解决方案3】:

        您使用什么版本的 SpeechRecognition 库?

        您可以通过以下方式进行检查:

        import speech_recognition as sr
        print sr.__version__
        

        如果您使用的是最新版本 (SpeechRecognition 3.1.3),则应使用 recognize_google() 方法而不是 recognize(),以使用 Google API。

        至于最新的文档,你可以查一下here,还有一些有用的examples

        【讨论】:

          【解决方案4】:

          我获取了一个实际执行但无法打印出我说的话的代码

          注意:此示例需要 PyAudio,因为它使用 Microphone 类

          import speech_recognition as sr
          import pyaudio
          
          # obtain audio from the microphone
          r = sr.Recognizer()
          with sr.Microphone() as source:
              print("Say something!")
              audio = r.listen(source)
          
          # recognize speech using Google Speech Recognition
          try:
              # for testing purposes, we're just using the default API key
              # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
              # instead of `r.recognize_google(audio)`
              print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
          except sr.UnknownValueError:
              print("Google Speech Recognition could not understand audio")
          except sr.RequestError as e:
              print("Could not request results from Google Speech Recognition service; {0}".format(e))
          
          # recognize speech using Wit.ai
          WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
          try:
              print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
          except sr.UnknownValueError:
              print("Wit.ai could not understand audio")
          except sr.RequestError as e:
              print("Could not request results from Wit.ai service; {0}".format(e))
          
          # recognize speech using IBM Speech to Text
          IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
          IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
          try:
              print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
          except sr.UnknownValueError:
              print("IBM Speech to Text could not understand audio")
          except sr.RequestError as e:
              print("Could not request results from IBM Speech to Text service; {0}".format(e))
          
          # recognize speech using AT&T Speech to Text
          ATT_APP_KEY = "INSERT AT&T SPEECH TO TEXT APP KEY HERE" # AT&T Speech to Text app keys are 32-character lowercase alphanumeric strings
          ATT_APP_SECRET = "INSERT AT&T SPEECH TO TEXT APP SECRET HERE" # AT&T Speech to Text app secrets are 32-character lowercase alphanumeric strings
          try:
              print("AT&T Speech to Text thinks you said " + r.recognize_att(audio, app_key=ATT_APP_KEY, app_secret=ATT_APP_SECRET))
          except sr.UnknownValueError:
              print("AT&T Speech to Text could not understand audio")
          except sr.RequestError as e:
              print("Could not request results from AT&T Speech to Text service; {0}".format(e))
          

          我读到以下内容

          说点什么! 谷歌语音识别认为你打招呼 无法从 Wit.ai 服务请求结果;识别请求失败:错误请求 无法从 IBM Speech to Text 服务请求结果;识别请求失败:未经授权 无法从 AT&T Speech to Text 服务请求结果;凭据请求失败:未经授权

          【讨论】:

            猜你喜欢
            • 2011-07-24
            • 2018-10-23
            • 2021-07-15
            • 1970-01-01
            • 1970-01-01
            • 2018-02-23
            • 2021-07-04
            • 2021-11-21
            • 1970-01-01
            相关资源
            最近更新 更多