2

I have an audio file, which I want to convert to text using Google Speech Recognition. But I'm facing the below issue.

Code :

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))

Error I'm facing:

 ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
SiHa
  • 7,830
  • 13
  • 34
  • 43
Anagha
  • 3,073
  • 8
  • 25
  • 43

1 Answers1

0

The link below show a similar question, I hope the answers can helps:

python: [Errno 10054] An existing connection was forcibly closed by the remote host

I had work on a similar project without using an API key, and I tried to match it and make it more suitable with your code:

 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))
Mejo
  • 41
  • 5