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。
答案 0 :(得分:0)
I tried this and it worked:
faudio = r.recognize_bing(audio, key=BING_KEY)
if faudio.strip() == "whatever":
do something...
答案 1 :(得分:0)
如果您使用外部或蓝牙麦克风,则可能需要安装额外的python软件包:
!pip install pyaudio
然后在不更改任何内容的情况下将包导入到您的代码中,但是您可以检查PC中支持的麦克风列表
import pyaudio
# check the list of microphones
sr.Microphone.list_microphone_names()
有关语音识别的完整教程,值得检查以下网站: https://realpython.com/python-speech-recognition/