Skip to content Skip to sidebar Skip to footer

Offline Speech Recognition In Qpython3

I have been trying to make a qpython program that uses sl4a.Android.recognizeSpeech function. The functionality works fine online. In my phone settings, I turned on and downloaded

Solution 1:

droid.recognizeSpeech("foo", None, None)

returns an Array with the recognized Speech in Index number 1. So if you want to access it, you have to type

return droid.recognizeSpeech("foo", None, None)[1]

Solution 2:

Actually none of the above worked for me. So I solved that this way:

x, result, error = droid.recognizeSpeech("Speak")

The result variable stores the speech recognized from the user

Example:

import sl4a
import time

droid = sl4a.Android()

defSpeak(talk):
   try:
     droid.ttsSpeak(talk)
     while droid.ttsIsSpeaking()[1] == True:
           time.sleep(2)
   except:
     droid.ttsSpeak("nothing to say")

deflisten():
   global result,error
   time.sleep(1)
   x, result, error = droid.recognizeSpeech("Speak")

whileTrue:
   try:
     listen()
   except:
     print(error)

   try:
     iflen(str(result)) > 0:
        print(result)
        if result == "how old are you":
           Speak("I'm 1 year old")
        elif result isNone:
           breakelse:
           Speak("I heard " + result)
   except Exception as e:
     print(e)
     break

Post a Comment for "Offline Speech Recognition In Qpython3"