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.3k stars 2.39k forks source link

speech_recognition/examples/audio_transcribe.py not working on Python 2 #201

Closed tonio-lora closed 7 years ago

tonio-lora commented 7 years ago

Steps to reproduce

Hi, I'm trying the speech_recognition/examples/audio_transcribe.py example on Python 2.7 and I get this error:

C:\Python27\python.exe C:/Users/anlo/PycharmProjects/textanalyticsdemo/speech_recognition.py Traceback (most recent call last): File "C:/Users/anlo/PycharmProjects/textanalyticsdemo/speech_recognition.py", line 1, in import speech_recognition as sr File "C:\Users\anlo\PycharmProjects\textanalyticsdemo\speech_recognition.py", line 11, in r = sr.Recognizer() AttributeError: 'module' object has no attribute 'Recognizer'

Process finished with exit code 1

The code I'm using is exactly the same that is listed on GitHub. I just changed the location of the audio file. These are the first lines:


# obtain path to "english.wav" in the same folder as this script
from os import path

AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "C:\\Users\\anlo\\PycharmProjects\\textanalyticsdemo\\english.wav")
# AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
# AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")

# use the audio file as the audio source
r = sr.Recognizer()

Expected behaviour

I was hoping the program will process the audio file using the Bing Speech recognition API (I entered the service key on the code) and print the text returned by the Bing Speech service

Actual behaviour

I got the error above.

(If the library threw an exception, paste the full stack trace here)

System information

My system is Windows 10 X64

My Python version is 2.7.11. My Pip version is 9.1.

My SpeechRecognition library version is 3.6.0. (You can check this by running python -c "import speech_recognition as sr;print(sr.__version__)".)

Uberi commented 7 years ago

Hi @tonio-lora,

The issue is that your file is named speech_recognition.py. When you do import speech_recognition as sr in Python 2, it's interpreted as an implicit relative import, which basically means your file is importing itself.

It's a weird issue; I did the same thing a few years at a BattleHack 2015, and it took hours to figure out at the time! In Python 3, implicit relative importing is gone, so this can't happen.

The solution is to not name your file speech_recognition.py.