SpeechRecognition hot-word not working properly

311 views Asked by At

I am a beginner I don't know much about speech recognition and hotwords.

Till now I have been using SpeechRecognition module but its not so accurate as:

  1. I have to wait for the program to access my mic
  2. Its not accurate even tough I use ambient_noise

This is what I have tried till now

    try:
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source, duration=2)
            listening_audio = r.listen(source, phrase_time_limit=3)
            try:
                command = r.recognize_google(listening_audio, language='en-in')
                self.Command.emit(command)
            except sr.UnknownValueError:
                print('')
            except Exception as e:
                print('First error:', str(e))
    except Exception as e:
        print("Second error:", str(e))

I want help for my program to work as expected or if anyone can give me excess to the Snowboy website. Either of them would help me achive my goal.

1

There are 1 answers

1
The Pilot Dude On

Try using the pyttsx3 module with speech_recognition

import speech_recognition as sr
import pyttsx3

engine = pyttsx3.init('sapi5')

voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

rate = engine.getProperty('rate')
engine.setProperty('rate', rate-70)

volume = engine.getProperty('volume')
engine.setProperty('volume', 1)

i = 0
n = 0

def speak(audio):
    print(audio)
    engine.say(audio)
    engine.runAndWait()

while (i<1):
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.adjust_for_ambient_noise(source)
        n=(n+1)
        print("Waiting For Voice Input...")
        audio = r.listen(source)
                                                   # interprete audio (Google Speech Recognition)
    try:
        s = (r.recognize_google(audio))
        message = (s.lower())
        print(message)
        
    except sr.UnknownValueError:
        print("Retry")
    except sr.RequestError as e:
        print("Could not request results$; {0}".format(e))
        try:
            speak("Sir Bad Signal")
        except:
            print("Sir Bad Signal")

This should work and should be better than your code. Try it out.