Uberi / speech_recognition

Speech recognition module for Python, supporting several engines and APIs, online and offline.
https://pypi.python.org/pypi/SpeechRecognition/
BSD 3-Clause "New" or "Revised" License
8.44k stars 2.4k forks source link

AttributeError: 'unicode' object has no attribute 'read' #109

Closed ritwickdsouza closed 8 years ago

ritwickdsouza commented 8 years ago

Hi, When i try to run the audio_trasnscribe.py it gives me the error AttributeError: 'unicode' object has no attribute 'read' on line number 7.

Uberi commented 8 years ago

@RitwickDSouza,

Please post the full code of the audio_transcribe.py example you are running. It sounds like you've modified the code, but we can't help you without knowing what those modifications are.

Also, it would be very helpful if you posted your Python version and SpeechRecognition library version. If you are not using SpeechRecognition 3.4.3, I highly recommend upgrading.

ritwickdsouza commented 8 years ago
def textParse(audio):
    try:
        result = r.recognize_google(audio)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition Service".format(e))
    return result

def getPhrase():
    with sr.Microphone() as source:
        audio = r.listen(source)
    return textParse(audio)

def getClip(clip_name):
    with sr.AudioFile(path.join(path.dirname(path.realpath(__file__)), clip_name)) as source:
        audio = r.record(source)
    return textParse(audio)

import speech_recognition as sr
from os import path

r = sr.Recognizer()
try:
    phrase = getClip('english.wav')
    print phrase
except UnboundLocalError:
    print 'Unable to hear the `word'

Python Version : 2.7.11 VoiceRecognition Version : 3.4.3

Can't seem to figure what is wrong. the function getPhrase() works but getClip does not work

Uberi commented 8 years ago

That exact code is working fine for me. Did you modify the audio file english.wav? If so, please post it here.

Can you try the following code, and post the full output (that means everything that the program output, not just one line)?

def textParse(audio):
    return r.recognize_google(audio)

def getPhrase():
    with sr.Microphone() as source:
        audio = r.listen(source)
    return textParse(audio)

def getClip(clip_name):
    with sr.AudioFile(path.join(path.dirname(path.realpath(__file__)), clip_name)) as source:
        audio = r.record(source)
    return textParse(audio)

import speech_recognition as sr
from os import path

r = sr.Recognizer()
try:
    phrase = getClip('english.wav')
    print phrase
except UnboundLocalError:
    print 'Unable to hear the `word'
ritwickdsouza commented 8 years ago
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Users\Ritwick DSouza\Desktop\Wrappup\microphone_recognition.py in <module>()
     17 r = sr.Recognizer()
     18 try:
---> 19     phrase = getClip('english.wav')
     20     print phrase
     21 except UnboundLocalError:

C:\Users\Ritwick DSouza\Desktop\Wrappup\microphone_recognition.py in getClip(clip_name)
      8 
      9 def getClip(clip_name):
---> 10     with sr.AudioFile(path.join(path.dirname(path.realpath(__file__)), clip_name)) as source:
     11         audio = r.record(source)
     12     return textParse(audio)

C:\Users\Ritwick DSouza\AppData\Local\Enthought\Canopy\User\lib\site-packages\speech_recognition\__init__.pyc in __init__(self, filename_or_fileobject)
    160 
    161     def __init__(self, filename_or_fileobject):
--> 162         assert isinstance(filename_or_fileobject, str) or filename_or_fileobject.read, "Given audio file must be a filename string or a file-like object"
    163         self.filename_or_fileobject = filename_or_fileobject
    164         self.stream = None

AttributeError: 'unicode' object has no attribute 'read'
Uberi commented 8 years ago

@RitwickDSouza,

I found the problem, and fixed it in b011c9aa555dc6d1ff03897e6ac4ca66915d13c8. Expect to see it in the next release!

ritwickdsouza commented 8 years ago
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Users\Ritwick DSouza\Desktop\Wrappup\microphone_recognition.py in <module>()
     17 r = sr.Recognizer()
     18 try:
---> 19     phrase = getClip('english.wav')
     20     print phrase
     21 except UnboundLocalError:

C:\Users\Ritwick DSouza\Desktop\Wrappup\microphone_recognition.py in getClip(clip_name)
      8 
      9 def getClip(clip_name):
---> 10     with sr.AudioFile(path.join(path.dirname(path.realpath(__file__)), clip_name)) as source:
     11         audio = r.record(source)
     12     return textParse(audio)

C:\Users\Ritwick DSouza\AppData\Local\Enthought\Canopy\User\lib\site-packages\speech_recognition\__init__.pyc in __init__(self, filename_or_fileobject)
    160 
    161     def __init__(self, filename_or_fileobject):
--> 162         if str is bytes: # Python 2 - if a file path is specified, it must either be a `str` instance or a `unicode` instance
    163             assert isinstance(filename_or_fileobject, (str, unicode)) or hasattr(filename_or_fileobject, "read"), "Given audio file must be a filename string or a file-like object"
    164         else: # Python 3 - if a file path is specified, it must be a `str` instance

AttributeError: 'unicode' object has no attribute 'read' 

Still getting this error :/

Uberi commented 8 years ago

The new version has not been released yet; it seems like you patched the source, but didn't delete the .pyc bytecode caches.

Uberi commented 8 years ago

This has been fixed in 3.4.4. Thanks for reporting!

arivatibm commented 7 years ago

still having this issue on the latest release