我正在使用Google Cloud API进行语音文本转换,我想添加一些上下文短语。根据google文档,语音上下文还具有一个我无法设置的boost字段。
我更改了导入的Speech_v1p1beta1而不是Speech_v1,并进行了如下编码:
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code=first_lang,
#alternative_language_codes=[second_lang],
audio_channel_count=1,
enable_separate_recognition_per_channel=False,
#metadata=metadata,
max_alternatives=5,
# enable_word_confidence=True,
speech_contexts=[{'phrases':['sfs'],'boost':15}]
# speech_contexts=[speech.SpeechContext(
# phrases=['life insurance'],boost=15
# )
# ]
)
我遇到以下错误:
File "convert-wav-beta.py", line 165, in <module>
transcript , confidence = google_transcribe(audio_file_name)
File "convert-wav-beta.py", line 106, in google_transcribe
speech_contexts=[{'phrases':['sfs'],'boost':15}]
File "/env/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 528, in init
copy.add(**val)
File "/env/lib/python3.5/site-packages/google/protobuf/internal/containers.py", line 376, in add
new_element = self._message_descriptor._concrete_class(**kwargs)
File "/env/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 509, in init
field = _GetFieldByName(message_descriptor, field_name)
File "/env/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 573, in _GetFieldByName
(message_descriptor.name, field_name))
ValueError: Protocol message SpeechContext has no "boost" field.```
答案 0 :(得分:0)
我通过更新系统中的beta库解决了此问题。我是通过Google从github代码中复制代码来做到这一点的。
答案 1 :(得分:0)
似乎您正在使用软件包“ speech”的RecognitionConfig,而不是“ speech_v1p1beta1 ”。如果使用beta功能,则类型和枚举也应从speech_v1p1beta1包中导入。您的导入语句应类似于:
from google.cloud import speech_v1p1beta1
from google.cloud.speech_v1p1beta1 import enums
from google.cloud.speech_v1p1beta1 import types