Sina Mohammadi
Sina Mohammadi

Reputation: 21

Python google speech recognition module doesn't work after a while

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()

Upvotes: 1

Views: 1757

Answers (1)

Sina Mohammadi
Sina Mohammadi

Reputation: 21

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()

Upvotes: 0

Related Questions