qpython3中的离线语音识别

Offline Speech Recognition in qpython3

我一直在尝试制作一个使用 sl4a.Android.recognizeSpeech 函数的 qpython 程序。该功能在线运行良好。

在我的 phone 设置中,我打开并下载了离线语音识别,google 现在离线工作正常,但 python 语音根本不起作用,要求我每次重试。

示例代码:

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()
droid.recognizeSpeech("foo", None, None)

returns 索引号为 1 的识别语音数组。所以如果你想访问它,你必须输入

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

实际上 none 以上对我有用。所以我这样解决了:

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

结果变量存储从用户那里识别出的语音

示例:

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