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
• 3,080 points
2,405 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 28, 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 28, 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
3 answers

(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
• 52,360 points
98,173 views
0 votes
1 answer

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

This error occurs because you are using ...READ MORE

answered Sep 9, 2020 in Python by Gitika
• 65,910 points
13,608 views
0 votes
0 answers
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 1,197 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
13,221 views
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, 2020 in Python by rahul
• 360 points
41,253 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,560 points
710 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,007 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,210 points
4,971 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP