如何在使用Recognition_google()时解决“ API不可用”问题

时间:2019-04-14 02:54:29

标签: python-3.x speech-recognition

我想用SpeechRecognition包编写一个简单的“猜单词”游戏。但是,当我尝试使用Recognition_google()识别单词时,它总是显示“ API无效”或“远程主机强行关闭了现有连接”。如何解决该问题?

# adjust the recognizer sensitivity to ambient noise and record audio
# from the microphone
with microphone as source:
    recognizer.adjust_for_ambient_noise(source)
    audio = recognizer.listen(source, timeout=7)

# set up the response object
response = {
    "success": True,
    "error": None,
    "transcription": None
}

# try recognizing the speech in the recording
# if a RequestError or UnknownValueError exception is caught,
#     update the response object accordingly
try:
    response["transcription"] = recognizer.recognize_google(audio)
except sr.RequestError:
    # API was unreachable or unresponsive
    response["success"] = False
    response["error"] = "API unavailable"
except sr.UnknownValueError:
    # speech was unintelligible
    response["error"] = "Unable to recognize speech"

return response

当我尝试识别该单词时,它会显示“ API无效”或“远程主机强制关闭现有连接”。

0 个答案:

没有答案