Ryzokuken
Ryzokuken

Reputation: 40

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 offline speech recognition and google now works fine offline, but the python speech does not work at all, asking me to try again every single time.

Sample Code:

import sl4a 
import time

droid = sl4a.Android()

def speak(text):
    droid.ttsSpeak(text)
    while droid.ttsIsSpeaking()[1] == True:
        time.sleep(1)

def listen():
    return droid.recognizeSpeech('Speak Now',None,None)

def login():
    speak('Passphrase, please')
    try:
        phrase = listen().result.lower()
    except:
        phrase = droid.dialogGetPassword('Passphrase').result
    print(phrase)
    if phrase == 'pork chops':
        speak('Welcome')
    else:
        speak('Access Denied')
        exit(0)

login()

Upvotes: 1

Views: 7607

Answers (2)

XploitsR
XploitsR

Reputation: 81

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

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

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

while True:
   try:
     listen()
   except:
     print(error)

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

Upvotes: 0

hoppla1232
hoppla1232

Reputation: 26

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]

Upvotes: 1

Related Questions