Offline Speech Recognition in qpython3

*爱你&永不变心* 提交于 2019-12-24 16:14:36

问题


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

回答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]



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

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


来源:https://stackoverflow.com/questions/35683016/offline-speech-recognition-in-qpython3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!