语音转文本

对于 Audio Read test step(音频读取测试步骤),可以配置语音转文本服务。每个服务都可以定义多个不同的语音识别配置文件。 默认情况下,ecu.test 通过 Windows 的 SAPI 支持语音识别。 还可以使用 UserPyModules 连接或实现其他语音转文本服务。

用户自定义语音识别

语音识别使用语音转文本服务,从音频文件中提取口述文本,并与参考文本进行比较。 Python 脚本必须满足以下要求,才能作为有效的语音识别器使用。

  • 脚本必须位于 Workspace(工作区)的 UserPyModuls 文件夹中。

  • 必须在脚本顶层指定变量 MODULE_TYPE = ‘SpeechToTextService’。

  • 文件中必须包含类 “SpeechToTextService”。

  • 该类必须实现 __init__、GetProfiles、RecognizeText、GetFileExtensions 和 GetServiceId 方法。

  • 不得存在两个服务使用相同的 service ID。

  • 服务中的配置文件可通过名称和语言唯一标识。

新增服务在刷新用户库后即可使用。可在 ecu.test 的 “Extras” 菜单中执行此操作。

最小 SpeechToTextService 示例

这是一个用户自定义语音识别示例。

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'

可用方法说明

类 “SpeechToTextService”

Python 脚本必须实现该类。

__init__

该方法必须实现,且不带参数,但可以为空。

GetProfiles

该方法返回语音转文本服务可用的识别配置文件列表。每个配置文件表示为元组 (name, RFC 5646 language code, optional: description)。 这些配置文件会在测试配置中按 “<service id>: <profile name>[, <description>] (<language>)” 的格式列出。

RecognizeText

该方法识别音频文件中的口述文本,并将其作为字符串返回。

  • 针对路径为 filePath 的音频文件。

  • 使用模块中选定的配置文件 profileName。

  • 使用所选配置文件的语言 language。

GetFileExtensions

以字符串列表的形式返回语音转文本服务支持的文件扩展名或音频格式,例如 [‘wav’, ‘flac’, ‘mp3’, ‘ogg’]。

GetServiceId

语音转文本服务的唯一标识符。