Speech-to-text¶
For the Audio Read test step a speech-to-text-service can be configured. Each service can define multiple different speech recognition profiles. By default ecu.test supports speech recognition using the SAPI of Windows. It is possible to connect or implement additional speech-to-text services using UserPyModules.
User defined speech recognition¶
The speech recognition uses a speech-to-text service to extract a spoken text from an audio file to compare it with a reference text. A Python script needs to fulfill several requirements to be available as a valid speech recognizer.
The script needs to be located in the
UserPyModulsfolder of the workspace.On the top level of the script, you must specify the variable MODULE_TYPE = ‘SpeechToTextService’.
The file needs to contain a class “SpeechToTextService”.
The class needs to implement the methods __init__, GetProfiles, RecognizeText, GetFileExtensions and GetServiceId.
There must not be two services with the same service ID.
Profiles within the service can be identified uniquely by their name and language.
Newly added services are available after refreshing the user libraries. This can be done in ecu.test in the menu “Extras”.
Minimal SpeechToTextService Example¶
This is an example of a user defined speech output.
MODULE_TYPE = 'SpeechToTextService'
class SpeechToTextService:
"""
Test SpeechToTextService that returns static strings
"""
def __init__(self):
self._profiles = {
'Default EN': 'en',
'Default DE': 'de',
'FancyAlgorithm DE': 'de',
}
def GetProfiles(self) -> list[tuple[str, str]] | list[tuple[str, str, str]]:
"""
Returns list of available profiles as tuple (name, language) or
(name, language, description). The name has to be unique.
"""
return [(key, value) for key, value in self._profiles.items()]
def RecognizeText(self, filePath: str, profileName: str, language: str) -> str:
if profileName not in self._profiles.keys():
raise RuntimeError(f'Profile {profileName} not found. Available profiles: '
f'{self.GetProfiles()}')
if profileName == 'Default EN':
return 'I\'m afraid I can\'t do that, Dave.'
if profileName == 'Default DE':
return 'Das kann ich nicht tun, Dave.'
if profileName == 'FancyAlgorithm DE':
return 'Gehirne der Serie 9000 sind die besten Computer, die jemals gebaut worden ' \
'sind. Kein Computer der Serie 9000 hat jemals einen Fehler gemacht oder ' \
'unklare Informationen gegeben. Wir alle sind hundertprozentig zuverlässig ' \
'und narrensicher - wir irren uns nie.'
return ''
@staticmethod
def GetFileExtensions() -> list[str]:
"""
Supported extensions for audio files.
"""
return ['wav', 'flac', 'mp3', 'ogg']
@staticmethod
def GetServiceId() -> str:
"""
Service name as to be displayed in the voice selection.
"""
return 'FakeSTT'
Explanation of available methods¶
Class “SpeechToTextService”
This class must be implemented by the Python script.
__init__
This method needs to be implemented without arguments, but may be empty.
GetProfiles
This method returns a list of recognition profiles that are available for the speech-to-text service. Each profile is listed as a tuple (name, RFC 5646 language code, optional: description). The profiles will be listed in the test configuration in the format “<service id>: <profile name>[, <description>] (<language>)”.
RecognizeText
This method recognizes spoken text in an audio file and returns it as string.
for the audio file with path filePath
the selected profile profileName of the module
the language language of the selected profile.
GetFileExtensions
Returns supported file extensions resp. audio formats of the speech-to-text service as list of strings, e.g. [‘wav’, ‘flac’, ‘mp3’, ‘ogg’].
GetServiceId
Unique identifier of the speech-to-text service.