python-3.x 谷歌语音识别的ConnectionResetError

6l7fqoea  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(231)

我有一个音频文件,我想使用Google语音识别将其转换为文本。但我遇到了以下问题。
代码:

from os import path
AUDIO_FILE = path.join(path.dirname(path.realpath('C:\\Users\\anagha\\Documents\\Python Scripts')),"Python Scripts\\res1.wav")

r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
    audio = r.record(source)

try:
    print("Google Speech Recognition thinks you said " + r.recognize_google(audio,key="My_API_key"))
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))

我面临的错误:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
hjzp0vay

hjzp0vay1#

下面的链接显示了一个类似的问题,我希望答案可以帮助:
python: [Errno 10054] An existing connection was forcibly closed by the remote host
我曾在一个类似的项目中工作过,但没有使用API密钥,我尝试匹配它,使其更适合您的代码:

from gtts import gTTS
    import speech_recognition as sr
    
    r = sr.Recognizer()
    audioFile = sr.AudioFile(r'C:\\Users\\anagha\\Documents\\Python Scripts\\res1.wav')
    
    with audioFile as source:
      r.adjust_for_ambient_noise(source, duration=0)
      audioData = r.record(source, duration=None, offset=None)
    
    try:
        print("Google Speech Recognition thinks you said " + r.recognize_google(audio_data = audioData, language = 'en-US'))
    
    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))

相关问题