Open ghost opened 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
Steps to reproduce
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':