Speech (audio file, wav) to Text - getting timed out

+1 vote

I am trying to read a wav file of size 24.4MB, 9 mins 33  secs. using a simple python speech_recogniton code, i am getting below mentioned error. any suggestions would be great help.

Error: Could not request results from google Speech Recognition service; recognition connection failed: [Errno 32] Broken pipe

def readAudioFile(audio_file):
    r = sr.Recognizer()
    file = sr.AudioFile(audio_file)
    with file as source:

        audio = r.record(source)
        type(audio)
    try:
        message = r.recognize_google(audio)
        print(message)
        print("Data extracted from phone conversation:\n" + message)
    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))

parser = argparse.ArgumentParser()
parser.add_argument("--file", "-f", type=str, required=True)
args = parser.parse_args()
audio_file = args.file
print(audio_file)
readAudioFile(audio_file)
Nov 27, 2018 in Python by Shobha
• 170 points
187 views

I have a similar code and I get this error:

Could not request results from Google Speech Recognition service; recognition request failed: Bad Gateway

2 answers to this question.

+2 votes
Best answer
I think the error you are getting is because the file you are using crosses the API limit. Please check the limits of the API from the below mentioned link and see if the audio you are using is crossing the limit:

https://cloud.google.com/speech-to-text/quotas
answered Nov 27, 2018 by Omkar
• 67,120 points

selected Nov 30, 2018 by Omkar
Yes this is the problem, as i tried using it with smaller files and it worked fine.
thanks for the reply. ,as mentioned above, the code is working fine if the audio length is small. I am new to python, is there any example of how to do it with uri?

Hey @Shobha. To use uri, you have to first upload your file to Google Cloud Storage. You can refer the following link to know how to do this: https://www.youtube.com/watch?v=kdRuDaDyyBo

Then in you code, instead of the audio file path, use the uri pointing to your audio file in Google Cloud Storage. 

audio_file = gs://bucket-name/path_to_audio_file
Thank you so much for your reply :)
@Shobha Did it work for you?
You're Welcome :-) Shobha
0 votes
@Omkar @Shobha

I tried doing it for a 2 minute audio file. It worked fine. I am not storing it in a text file i am simply printing the text.

The file is 3 MB.

https://www.edureka.co/community/31551/error-speech-to-text-codec-cannot-decode-the-bytes-position

Here you can have a look at the code.
answered Nov 27, 2018 by Nitesh
• 2,870 points

Related Questions In Python

0 votes
0 answers

how to print string to text file?

can you give an example? READ MORE

Apr 3 in Python by Waseem
• 3,440 points
13 views
0 votes
1 answer

How to read data from a text file using Python?

Refer to the below example where the ...READ MORE

answered May 12 in Python by Sushma
17 views
0 votes
0 answers
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,710 points
1,318 views
0 votes
2 answers

Error: Speech to Text Codec cannot decode the bytes in position

import speech_recognition as sr r = sr.Recognizer() audio ='C\Users\Desktop\audiofile1.wav' with ...READ MORE

answered Nov 27, 2018 in Python by Nabarupa Das
93 views
0 votes
2 answers
0 votes
1 answer
+1 vote
2 answers

Appending data to a file

First open the file that you want ...READ MORE

answered Jul 23, 2018 in Python by Omkar
• 67,120 points
26 views
+1 vote
1 answer

Appending data to a file

To append a file without overwriting, open ...READ MORE

answered Aug 7, 2018 in Python by Omkar
• 67,120 points

edited Aug 7, 2018 by Omkar 29 views