0

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.

2
  • Not sure what you're trying to do, but a quick look at the doc and it doesn't seem that that's a method in the library pypi.org/project/SpeechRecognition Commented Aug 1, 2018 at 23:56
  • @zacharybys Which method are you talking about? Recognizer() Is a method, isn't it? (fairly new to python) Commented Aug 2, 2018 at 0:00

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe I am that stupid, thank you so much. At first it was named speech_recognition.py, but after I googled the error I just copied the script and renamed the file, but the old file still was there. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.