python error AttributeError NoneType object has no attribute read

0 votes

I ahve the following python code:

import speech_recognition as sr

recognizer=sr.Recognizer()
mic=sr.Microphone()
print('Heyyyaa')
audio=recognizer.listen(mic)

try:
    text=recognizer.recognize_google(language='it')
    print('heyyya '+text)
except sr.UnknownValueError:
    print('Errore 1')
except sr.RequestError:
    print('Errore 2')

I get the following error:

Traceback (most recent call last):
  File "C:/Users/Freddy/PycharmProjects/SpeechRecognition/SpeechToText.py", line 26, in <module>
    audio=recognizer.listen(mic)
  File "C:\Python34\lib\site-packages\speech_recognition\__init__.py", line 293, in listen
    buffer = source.stream.read(source.CHUNK)
AttributeError: 'NoneType' object has no attribute 'read'
Oct 3, 2019 in Python by Hannah
• 18,570 points
11,477 views

1 answer to this question.

0 votes

The microphone needs to be entered using a with statement. Try this code. it should work:

import speech_recognition as sr

r=sr.Recognizer()
with sr.Microphone() as mic:
    print('heyyaa')
    audio=r.listen(mic)

try:
    text=r.recognize_google(language='it')
    print('heyya '+text)
except sr.UnknownValueError:
    print('Errore 1')
except sr.RequestError:
    print('Errore 2')


Hope this helps!!

If you need to learn more about Python, It's recommended to join Python Online Training today.

Thanks!

answered Oct 3, 2019 by Jack

Related Questions In Python

0 votes
1 answer
0 votes
2 answers

Python Pandas error: AttributeError: 'DataFrame' object has no attribute 'rows'

Try this: data=pd.read_csv('/your file name', delim_whitespace=Tru ...READ MORE

answered Dec 10, 2020 in Python by anonymous
• 82,820 points
80,463 views
0 votes
1 answer

Python error "AttributeError: 'str' object has no attribute 'casefold'"

Check your python version. Casefold is possible ...READ MORE

answered Jul 4, 2019 in Python by Yesha
3,940 views
0 votes
1 answer

Python error "AttributeError: '_Screen' object has no attribute 'onkeypress'"

Instead of  wn.onkeypress(go_up, "w") Try wn.onkey(go_uo, " ...READ MORE

answered Jul 5, 2019 in Python by Pooja
2,350 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 2,411 views
0 votes
0 answers
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,810 views
0 votes
1 answer

Python error "AttributeError: '_Screen' object has no attribute 'mainloop'" python module turtle

Hey @Nagya, replace  wn.mainlopp() with turtle.mainloop() ...READ MORE

answered Jun 19, 2019 in Python by Faiza
3,520 views
+1 vote
2 answers

Python error "AttributeError: 'Turtle' object has no attribute 'Shape'"

Hey @Nagya, replace python.Shape("Square") with the following: python.shape("square") Python is case ...READ MORE

answered Jun 19, 2019 in Python by Faiza
16,005 views