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,950 points
600 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

+1 vote
1 answer

(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

You are getting this error because you ...READ MORE

answered Dec 31, 2019 in Python by Kalgi
• 51,890 points
6,387 views
0 votes
1 answer

What's the right way to concatenate files and appending the New Line character in Python? Text or binary mode?

It seems like you're using the wrong ...READ MORE

answered Nov 13, 2018 in Python by Anirudh
• 2,080 points

edited Dec 14, 2018 by Anirudh 227 views
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, 2019 in Python by anonymous
3,593 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, 2019 in Python by Mohammad
• 3,230 points
334 views
+1 vote
1 answer
0 votes
3 answers

Python error "NameError: name 'sr' is not defined"

NameError: name 'xx' is not defined Python knows ...READ MORE

answered Mar 19 in Python by rahul
• 360 points
21,470 views
0 votes
1 answer

Need help with Python Text-to-Speech usage

Hi. Just before I begin my answer I ...READ MORE

answered Jan 16, 2019 in Python by Nymeria
• 3,540 points
113 views
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,058 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
• 69,040 points
1,286 views