使用 Kivy 应用程序调用 MS Azure 语音识别
Calling MS Azure speech recognition with Kivy app
我正在使用 Kivy 2.0.0 构建一个 Python 应用程序。我想时不时地访问 Kivy 的循环并调用语音识别。语音识别在您按下一个按钮后开始,在按下另一个按钮后结束。然后,这种语音识别将其结果存储到一个变量(result)中,并将其显示在屏幕上的一个标签上,其文本定义为term_text.
我写了这段代码,我预计它会起作用,但它不会。你能帮帮我吗?
这是我得到的错误...
文件“字符串”,第 22 行,在模块中
类型错误:listen() 缺少 1 个必需的位置参数:'dt'
import azure.cognitiveservices.speech as speechsdk
from kivy.app import App
from kivy.core import text
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.clock import Clock
speech_key, service_region = "", "uksouth"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_config.speech_recognition_language="en-US"
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
Builder.load_string("""
<w1>
BoxLayout:
orientation: "vertical"
size: root.width, root.height
spacing: 50
padding: 50
Label:
id: label1
text: root.term_text
bold: True
size_hint: (3, 3)
pos_hint: {"center_x": 0.5}
Button:
id: button1
text: "Listen to me now!"
background_normal: 'none'
background_color: (0, 64/255, 77/255)
on_press: root.listen()
Button:
id: button2
text: "Stop listening to me"
background_normal: 'none'
background_color: (0, 64/255, 77/255)
on_press: root.stop()
""")
result = ""
def stop_cb(evt):
print('CLOSING on {}'.format(evt))
speech_recognizer.stop_continuous_recognition()
global done
done = True
def collectResult(evt):
global result
result += evt.result.text
class w1(Widget):
term_text = StringProperty()
evt = " "
def __init__(self, **kwargs):
super(w1, self).__init__(**kwargs)
self.term_text = "terminal"
Clock.schedule_interval(self.listen, 2)
def listen(self, dt):
self.term_text = "Listening..."
speech_recognizer.recognized.connect(lambda evt: collectResult(evt))
speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED:
{}'.format(evt)))
speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED
{}'.format(evt)))
speech_recognizer.canceled.connect(lambda evt: print('CANCELED {}'.format(evt)))
speech_recognizer.session_stopped.connect(stop_cb)
speech_recognizer.canceled.connect(stop_cb)
speech_recognizer.start_continuous_recognition()
def stop(self):
stop_cb()
self.term_text = " {} ".format(result)
class DaApp(App):
def build(self):
return w1()
if __name__ == '__main__':
DaApp().run()
如错误所述:
listen() missing 1 required positional argument: 'dt'
所以你只需要提供一个dt
参数。尝试更改:
on_press: root.listen()
至:
on_press: root.listen(0)
我正在使用 Kivy 2.0.0 构建一个 Python 应用程序。我想时不时地访问 Kivy 的循环并调用语音识别。语音识别在您按下一个按钮后开始,在按下另一个按钮后结束。然后,这种语音识别将其结果存储到一个变量(result)中,并将其显示在屏幕上的一个标签上,其文本定义为term_text.
我写了这段代码,我预计它会起作用,但它不会。你能帮帮我吗?
这是我得到的错误...
文件“字符串”,第 22 行,在模块中 类型错误:listen() 缺少 1 个必需的位置参数:'dt'
import azure.cognitiveservices.speech as speechsdk
from kivy.app import App
from kivy.core import text
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.clock import Clock
speech_key, service_region = "", "uksouth"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_config.speech_recognition_language="en-US"
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
Builder.load_string("""
<w1>
BoxLayout:
orientation: "vertical"
size: root.width, root.height
spacing: 50
padding: 50
Label:
id: label1
text: root.term_text
bold: True
size_hint: (3, 3)
pos_hint: {"center_x": 0.5}
Button:
id: button1
text: "Listen to me now!"
background_normal: 'none'
background_color: (0, 64/255, 77/255)
on_press: root.listen()
Button:
id: button2
text: "Stop listening to me"
background_normal: 'none'
background_color: (0, 64/255, 77/255)
on_press: root.stop()
""")
result = ""
def stop_cb(evt):
print('CLOSING on {}'.format(evt))
speech_recognizer.stop_continuous_recognition()
global done
done = True
def collectResult(evt):
global result
result += evt.result.text
class w1(Widget):
term_text = StringProperty()
evt = " "
def __init__(self, **kwargs):
super(w1, self).__init__(**kwargs)
self.term_text = "terminal"
Clock.schedule_interval(self.listen, 2)
def listen(self, dt):
self.term_text = "Listening..."
speech_recognizer.recognized.connect(lambda evt: collectResult(evt))
speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED:
{}'.format(evt)))
speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED
{}'.format(evt)))
speech_recognizer.canceled.connect(lambda evt: print('CANCELED {}'.format(evt)))
speech_recognizer.session_stopped.connect(stop_cb)
speech_recognizer.canceled.connect(stop_cb)
speech_recognizer.start_continuous_recognition()
def stop(self):
stop_cb()
self.term_text = " {} ".format(result)
class DaApp(App):
def build(self):
return w1()
if __name__ == '__main__':
DaApp().run()
如错误所述:
listen() missing 1 required positional argument: 'dt'
所以你只需要提供一个dt
参数。尝试更改:
on_press: root.listen()
至:
on_press: root.listen(0)