0

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.

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
JashSmith
  • 211
  • 1
  • 15

1 Answers1

0

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.

The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
  • Thanks for your response. It works but I if there is a song being played in the background then it listens for always or even if I say the word it listens or another 2 seconds which I want to omit out. I know the problem is in the module but is there a way to solve it? – JashSmith Jan 06 '21 at 12:42