1

I'm trying to create an application like Alexa for the computer called "Emma" using Python. By using Speech Recognition module it'll use a microphone as a source to listen to the user. it works fine but after answering or doing some stuff like searching it'll freeze and doesn't work anymore.

I thought that maybe speech recognition has some limited time for using but after searching I've found nothing about it. Now I just don't know it's because of speech recognition or some other modules like GTTS (Google Text To Speech).

Here is the link to my repository if you need to see the whole code: https://github.com/sina1mhi/emma_virtual_assistant

Please let me know your ways to solve the problem.

Here is the part of speech recognition code:

def record_audio(ask=False, lang="en-US"):
    with sr.Microphone() as source:  # microphone as source
        print("Emma: I'm listening")
        if ask:
            speak(ask)
        time.sleep(1)
        audio = r.listen(source)  # listen for the audio via source
        voice_data = ''
        try:
            voice_data = r.recognize_google(
                audio, language=lang)  # convert audio to text
        except sr.UnknownValueError:  # error: recognizer does not understand
            speak("I did'nt get that")
            exit()
        except sr.RequestError:
            # error: recognizer is not connected
            speak('Sorry, the service is down')
            exit()
        print(f">> {voice_data.lower()}")  # print what user said
        return voice_data.lower()
  • What have you tried so far? – Jamie Feb 11 '20 at 14:46
  • @Jamie actually I don't know what to do at all. I'm new to programming BTW. I've tried to improve the speed of respond by putting it inside a while loop and use continue statement but nothing works. – Sina Mohammadi Feb 11 '20 at 14:56
  • Sorry, I should've been more clear. How do you know it's freezing? At which line in the code do things stop working? Are you getting any errors? – Jamie Feb 11 '20 at 14:59
  • @Jamie No I'm not getting any errors and when I run the app in the terminal after doing some stuff it just stops, like an infinite loop you know, But there's no infinite loop at all. I'm 100% sure. – Sina Mohammadi Feb 11 '20 at 15:05
  • Have you tried adding print statements at checkpoints in your code to see where it's getting stopped? Or you could try using the debugging tools that come with most IDEs. – Jamie Feb 11 '20 at 15:06
  • @Jamie I've tried the debugging tool a couple of times and didn't find the bug. I'm gonna add some print statements and let you know the result. Thanks for the idea. – Sina Mohammadi Feb 11 '20 at 15:11
  • @Jamie I've just found out that instead of using recognizer's listen method I should use record method and set the duration argument. – Sina Mohammadi Feb 11 '20 at 20:44

1 Answers1

0

After reading Speech Recognition Library Reference I found out that instead of using the recognizer's listen method I should use record method and set the duration argument.


Here's the code:

def record_audio(ask=False, lang="en-US"):
    # Change the sample_rate to 16000 good quality and better recognition
    # higher sample rate means slower app.
    with sr.Microphone(sample_rate=12000) as source:  # microphone as source
        print("Emma: I'm listening")
        audio = r.record(source, duration=5)  # listen for the audio via source
        print("Emma: Microphone turned off, processing...")
        voice_data = ''
        try:
            voice_data = r.recognize_google(
                audio, language=lang)  # convert audio to text
        except sr.UnknownValueError:  # error: recognizer does not understand
            speak("I did'nt get that")
            exit()
        except sr.RequestError:
            # error: recognizer is not connected
            speak('Sorry, the service is down')
            exit()
        print(f">> {voice_data.lower()}")  # print what user said
        return voice_data.lower()