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)
        message = r.recognize_google(audio)
        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
Nov 27, 2018 in Python by Shobha
• 170 points

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:

answered Nov 27, 2018 by Omkar
• 67,380 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.


Here you can have a look at the code.
answered Nov 27, 2018 by Nitesh
• 2,920 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
• 4,480 points
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
0 votes
1 answer
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
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,380 points
+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,380 points

edited Aug 7, 2018 by Omkar 35 views