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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
'''
Speech to Text
==============
.. versionadded:: 1.4.0
Speech Recognition facade.
In order to check that your device supports voice recognition use method
`exist`.
Variable `language` indicates which language will be used to match words from
voice.
Use `start` to start voice recognition immediately and `stop` to stop.
.. note::
Needed permissions for Android: `RECORD_AUDIO` (and `INTERNET` if you want
online voice recognition API to be used)
.. note::
On Android platform, after execute `start` method you can hear BEEP!
Mute sound in order to disable it.
.. note::
For Android implementation to work there has to be an application with
`android.speech.RecognitionService` implementation present in the system.
Mostly it's `com.google.android.googlequicksearchbox` or "Google"
application (the search bar with the launcher widget).
Offline Speech Recognition on Android
-------------------------------------
Requires any application that provides an
`android.speech.RecognitionService` implementation to the other apps. One of
such applications is on a lot of devices preinstalled Google (quick search
box).
The API prefers offline recognition, but should be able to switch to online
alternative in case you don't have a language package installed (`INTERNET`
permission necessary).
You can enable offline speech recognition this way (Android 8.1):
* open the `Settings` app
* choose `Language & Input` / `Language & Keyboard` (Samsung might include it
in the `General` category)
* choose `On-Screen keyboard` or `Voice search`
* choose `Google Keyboard`
* choose `Offline Speech recognition`
* download language package if you don't have one already
Simple Examples
---------------
To start listening::
>>> from plyer import stt
>>> stt.start()
To retrieve partial results while listening::
>>> assert stt.listening
>>> print(stt.partial_results)
To stop listening::
>>> stt.stop()
To retrieve results after the listening stopped::
>>> print(stt.results)
'''
class STT:
'''
Speech to text facade.
'''
_language = 'en-US'
'''
Default language in which platform will try to recognize voice.
In order to change language pick one from list by using
`supported_languages` method.
'''
_supported_languages = [
'en-US',
'pl-PL'
]
results = []
'''
List of sentences found while listening. It may consist of many similar
and possible sentences that was recognition program.
'''
errors = []
'''
List of errors found while listening.
'''
partial_results = []
'''
List of results found while the listener is still being active.
'''
prefer_offline = True
'''
Preference whether to use offline language package necessary for
each platform dependant implementation or online API.
'''
listening = False
'''
Current state of listening.
'''
@property
def supported_languages(self):
'''
Return list of supported languages used in recognition.
'''
return self._supported_languages
@property
def language(self):
'''
Return current language.
'''
return self._language
@language.setter
def language(self, lang):
'''
Set current language.
Value can not be set if it's not supported. See `supported_languages`
to get what language you can set.
.. note::
We obviously can't check each language, therefore if you find
that a specific language is available to you and the only limitation
is our check for the internally defined `supported_languages`, feel
free to open a pull request for adding your language to the list.
'''
if lang in self.supported_languages:
self._language = lang
# public methods
def start(self):
'''
Start listening.
'''
self.results = []
self.partial_results = []
self._start()
self.listening = True
def stop(self):
'''
Stop listening.
'''
self._stop()
self.listening = False
def exist(self):
'''
Returns a boolean for speech recognition availability.
'''
return self._exist()
# private methods
def _start(self):
raise NotImplementedError
def _stop(self):
raise NotImplementedError
def _exist(self):
raise NotImplementedError
|