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,940 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,350 points
100,967 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,770 points
14,038 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,090 points

edited Dec 14, 2018 by Anirudh 1,666 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,883 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
42,577 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
1,030 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,434 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,220 points
5,829 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