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

0 votes

This is my program i used.

import speech_recognition as sr
r = sr.Recognizer()
audio ='C\Users\Desktop\audiofile1.wav'
with sr.AudioFile(audio) as source:
    print("Audio File")
    audio = r.record(source)
    print("Captured successfully")
try:
    text = r.recognize_google(audio)
    print (text)
except Exception as exp:
    print (exp)

This is the error I am getting:

Can someone help?

Nov 27, 2018 in Python by Nitesh
• 2,910 points
110 views

Your file location string must be written as follows:

audio ='C\\Users\\Desktop\\audiofile1.wav'
Thank you @Shuvodip Ghosh it worked fine.

2 answers to this question.

+3 votes
Best answer
import speech_recognition as sr
r = sr.Recognizer()
audio ='C\Users\Desktop\audiofile1.wav'
with sr.AudioFile(audio) as source:
    print("Audio File")
    audio = r.record(source)
    print("Captured successfully")
try:
    text = r.recognize_google(audio)
    print (text)
except Exception as exp:
    print (exp)

The error is caused by the 3rd line use \\ instead of \ otherwise it will show you a unicode error.

I changed your program's 3rd line.

import speech_recognition as sr
r = sr.Recognizer()​
audio ='C\\Users\\Desktop\\audiofile1.wav'
with sr.AudioFile(audio) as source:
    print("Audio File")
    audio = r.record(source)
    print("Captured successfully")
try:
    text = r.recognize_google(audio)
    print (text)
except Exception as exp:
    print (exp)

Run the file it will work fine.

answered Nov 27, 2018 by Nabarupa Das

selected Nov 30, 2018 by Omkar
–3 votes
import speech_recognition as sr
r = sr.Recognizer()
audio ='C\Users\Desktop\audiofile1.wav'
with sr.AudioFile(audio) as source:
    print("Audio File")
    audio = r.record(source)
    print("Captured successfully")
try:
    text = r.recognize_google(audio)
    print (text)
except Exception as exp:
    print (exp)

The above file should be in the location of your program. Else it wont be recognised.

Try placing it in the location of your program.

answered Nov 27, 2018 by Jino

This is not the right solution. The file need not necessarily be in the same location. The error is because of single back-slash (\). When single back-slash is used, it is considered as a Uni-code character. You have to use escape character (use double back-slash)

Related Questions In Python

0 votes
1 answer
0 votes
2 answers

Python Error "UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128)"

import csv import sys reload(sys) sys.setdefaultencoding('utf8') data = [["a", "b", u'\xe9']] with ...READ MORE

answered Jun 28 in Python by anonymous
135 views
0 votes
1 answer

How to resolve the error while importing cv2 from openCV in python?

I would recommend you go with anaconda ...READ MORE

answered Jul 31 in Python by Mohammad
• 3,060 points
26 views
0 votes
2 answers
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 177 views
+1 vote
2 answers

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

I think the error you are getting ...READ MORE

answered Nov 27, 2018 in Python by Omkar
• 67,290 points
232 views