SmooK
SmooK

Reputation: 37

Python Speech Recognition: AttributeError: module 'speech_recognition' has no attribute 'Recognizer'

I know there are several threads of my question, but no answer of it helped me and I tried anything I can think of and saw here.

The error code im getting:

Traceback (most recent call last):
  File "/home/pi/Documents/SB/sp_recog.py", line 4, in <module>
    import speech_recognition as sr
  File "/home/pi/Documents/SB/speech_recognition.py", line 59, in <module>
    data = recordAudio()
  File "/home/pi/Documents/SB/speech_recognition.py" line 20, in recordAudio
    r = sr.Recognizer()
AttributeError: module 'speech_recognition' has no attribute 'Recognizer'

And the Python Script im using:

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr
import vlc
import time
import os
from time import ctime
from gtts import gTTS

def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='de')
    tts.save("audio.mp3")
    os.system("mpg321 audio.mp3")

def recordAudio():
    # Record Audio
    r = sr.Recognizer()
    p = vlc.MediaPlayer("PRIVATE")
    with p.play() as source:
        print("Say something!")
        audio = r.listen(source)

    # Speech recognition using Google Speech Recognition
    data = ""
    try:
        # Uses the default API key
        # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        data = r.recognize_google(audio)
        print("You said: " + data)
    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; {0}".format(e))

    return data


def PRIVATE(data):
    if "PRIVATE" in data:
        speak("PRIVATE")

    if "PRIVATE" in data:
        speak(ctime())

    if "PRIVATE" in data:
        data = data.split(" ")
        location = data[2]
        speak("PRIVATE")


# initialization
time.sleep(2)
speak("PRIVATE")
while 1:
    data = recordAudio()
    PRIVATE(data)

I changed some of the code because I don't want it to be read. I copied the script from a site and modified it so I can work with it.

Things I tried:

  • Changing the filename
  • Compiling the script to a executable via PYInstaller
  • Reinstall Speech Recognizer
  • Reinstall PyAudio
  • Reinstall Python_VLC (while I don't think the error is because of that library, It's worth a atleast.)

I would appreciate any help and I hope if the issue will be resolved that other people with the same problem benefit from this thread, thank you.

Upvotes: 0

Views: 1182

Answers (1)

Lakshay Sharma
Lakshay Sharma

Reputation: 877

It seems like you have a file /home/pi/Documents/SB/speech_recognition.py, and therefore it's looking for Recognizer() in your file (as opposed to the actual module speech_recognition). Try renaming your speech_recognition.py file to something else.

Upvotes: 3

Related Questions