无法通过语音自适应增强来提高转录准确性

时间:2020-12-23 15:36:38

标签: python google-cloud-platform google-speech-api

我正在使用 SpeechRecognition Python 库来执行 Speech to Text 操作。我正在使用 <table> <tr> <td>Row 1, Column 1</td> <td>Row 1, Column 2</td> <td>Row 1, Column 3</td> </tr> <tr> <td>Row 2, Column 1</td> <td>Row 2, Column 2</td> <td>Row 2, Column 3</td> </tr> <tr> <td>Row 3, <button>Click me</button> Column 1</td> <td>Row 3, Column 2</td> <td>Row 3, Column 3</td> </tr> </table> 函数来使用 Google Cloud Speech-to-Text API。

这是我的代码:

recognize_google_cloud

函数 import speech_recognition as sr; import json; j = ''; with open('key.json', 'r') as f: j = f.read().replace('\n', ''); js = json.loads(j); r = sr.Recognizer(); mic = sr.Microphone(); with candide as source: audio = r.record(source); print(r.recognize_google_cloud(audio, language='fr-FR', preferred_phrases=['pistoles', 'disait'], credentials_json=j)); 将麦克风捕获的数据发送到 Google API,并在一组备选方案中选择给定语音的最可能转录。 如本 page of the documentation 中所述,参数 recognize_google_cloud 用于选择包含所列单词的其他替代词。

可以使用 speech adaptation boost 改进这些结果。由于 SpeechRecognition 库的版本不允许我们指定 boost 值,我用硬编码的 boost 值更新了 preferered_phrases 文件:

speech_recognition/__init__.py

不幸的是,当我执行我的代码时,出现以下错误:

        if preferred_phrases is not None:
            speech_config["speechContexts"] = {"phrases": preferred_phrases, "boost": 19}

我的请求有错误吗?

1 个答案:

答案 0 :(得分:1)

我了解到您正在修改 SpeechRecognition libraryspeech_recognition/__init__.py 文件,以便在您的请求中包含“boost”参数。

在查看此文件时,我注意到它使用了 API 的 'v1' version;但是,“boost”参数仅在 ‘v1p1beta1’ version

因此,您可以在代码中进行的另一个调整如下:

`speech_service = build ("speech","v1p1beta1", credentials = api_credentials, cache_discovery = False)`

通过此修改,您应该不会再看到 BadRequest 错误。

同时,请考虑该库是内部使用 Google Speech-to-text API 的第三方库。因此,如果此库不能满足您当前的所有需求,另一种替代方法可以直接使用 Speech-to-text API Python Client library 创建您自己的实现。