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.45k stars 2.4k forks source link

AttributeError: 'module' object has no attribute 'listen' #426

Open ghost opened 5 years ago

ghost commented 5 years ago

Steps to reproduce

  1. (How do you make the issue happen? Does it happen every time you try it?) Run Code. Located at bottom.

Expected behaviour

(What did you expect to happen?) To not show error, and be listening for my voice. Actual behaviour

(What happened instead? How is it different from what you expected?) It gives me this error - AttributeError: 'module' object has no attribute 'listen'

System information

My system is Windows 10 x64.

My Python version is 2.7 (You can check this by running python -V.)

Using Pycharm Community Edition 2019

My SpeechRecognition library version is 3.8.1.

My PyAudio library version is 0.2.11

My working microphones are: (0, u'Microsoft Sound Mapper - Input') (1, u'Microphone (Realtek Audio)') (2, u'Microsoft Sound Mapper - Output') (3, u'Speakers / Headphones (Realtek ')

Code used - import speech_recognition as sr;print(sr.version) import pyttsx3 import os

speech = sr

try: engine = pyttsx3.init() except ImportError: print('Requested Driver Not Found') except RuntimeError: print('Driver fails to start')

voices = engine.getProperty('voices')

for voice in voices: print(voice.id) engine.setProperty('voice','HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0') rate = engine.getProperty('rate') engine.setProperty('rate', rate)

def speak_text_cmd(cmd): engine.say(cmd) engine.runAndWait()

def read_voice_cmd(): voice_text = '' print('Listening...') with sr.Microphone(device_index=1) as source: audio = speech.listen(source) try: voice_text = speech.recognize_google(audio, key='GOOGLE_SPEECH_RECOGNITION_API_KEY') except sr.UnknownValueError: pass except sr.Request as e: print ('Network Error') return voice_text

if name == 'main':

speak_text_cmd('TEST')

while True:

    voice_note = read_voice_cmd()
    print('cmd : ()'.format(voice_note))

    if 'hello' in voice_note:
        speak_text_cmd('Hello Boss. How can I help you?')
        continue
    elif 'open' in voice_note:
        os.system('explorer C:\\ ()'.format(voice_note.replace('Open ', '')))
        continue
    elif 'bye' in voice_note:
        speak_text_cmd('Bye Boss. Shutting Down')
        exit()
colinjlacy commented 5 years ago

The listen method lives in the Recognizer class, not in the module. So to fix this you'd create a new instance of Recognizer, probably as your speech variable:

speech = sr.Recognizer()

After that the call to listen should work. I have a working example here: https://github.com/colinjlacy/vocalist/blob/master/vocalist/listener.py