【问题标题】:Voice Recognition in Python not working?Python中的语音识别不起作用?
【发布时间】:2018-07-16 03:52:13
【问题描述】:
import speech_recognition as sr


# obtain audio from the microphone


r = sr.Recognizer()


with sr.Microphone() as source:
    print("Listening...")

audio = r.listen(source)

BING_KEY = ""  #API KEY HERE

try:
    print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))


except sr.UnknownValueError:
    print("Microsoft Bing Voice Recognition could not understand audio")


except sr.RequestError as e:

   print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))

########################################################################################################

voiceLine = r.recognize_bing(audio, key=BING_KEY)

if r.recognize_bing(audio, key=BING_KEY) == "Hello":
    print("big nerd")

#### if voiceLine == "Hello":
####   print("big nerd")
问题似乎在于语音在“if”语句中的行为不像普通字符串......(我对python很陌生,所以请放轻松:c)我也知道缩进不在正确的地方,我不知道如何使用这个网站,kms。

【问题讨论】:

  • 案件可能很重要,应该是你好,而不是你好
  • 您能否更详细地解释一下,您的问题究竟是什么?是什么让你从r.recognize_bing() 回来?

标签: python api speech-recognition voice-recognition voice


【解决方案1】:
I tried this and it worked:

    faudio = r.recognize_bing(audio, key=BING_KEY)

    if faudio.strip() == "whatever":
        do something...

【讨论】:

  • 我试图计算响应的 len(),在我的情况下,我使用 Google Cloud Speech,我注意到返回字符串上有一堆空格,所以只做 strip(),应该工作。
【解决方案2】:

如果您使用外接或蓝牙麦克风,您可能需要安装额外的 python 包:

!pip install pyaudio

然后在你的代码中导入包而不做任何改变,但是你可以在你的电脑中查看支持的麦克风列表

import pyaudio
# check the list of microphones
sr.Microphone.list_microphone_names()

有关语音识别的完整教程,值得查看此网站: https://realpython.com/python-speech-recognition/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多