1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from plyer import stt
Builder.load_string('''
#:import stt plyer.stt
<SpeechInterface>:
orientation: 'vertical'
Label:
size_hint_y: None
height: sp(40)
text: 'Is supported: %s' % stt.exist()
Label:
size_hint_y: None
height: sp(40)
text: 'Possible Matches'
TextInput:
id: results
hint_text: 'results (auto stop)'
TextInput:
id: partial
hint_text: 'partial results (manual stop)'
TextInput:
id: errors
hint_text: 'errors'
Button:
id: start_button
text: 'Start Listening'
on_release: root.start_listening()
''')
class SpeechInterface(BoxLayout):
'''Root Widget.'''
def start_listening(self):
if stt.listening:
self.stop_listening()
return
start_button = self.ids.start_button
start_button.text = 'Stop'
self.ids.results.text = ''
self.ids.partial.text = ''
stt.start()
Clock.schedule_interval(self.check_state, 1 / 5)
def stop_listening(self):
start_button = self.ids.start_button
start_button.text = 'Start Listening'
stt.stop()
self.update()
Clock.unschedule(self.check_state)
def check_state(self, dt):
# if the recognizer service stops, change UI
if not stt.listening:
self.stop_listening()
def update(self):
self.ids.partial.text = '\n'.join(stt.partial_results)
self.ids.results.text = '\n'.join(stt.results)
class SpeechApp(App):
def build(self):
return SpeechInterface()
def on_pause(self):
return True
if __name__ == "__main__":
SpeechApp().run()
|