当我尝试使用以下配置调用Google Cloud Speech to Text Api进行长时间运行识别时:
config = dict(
languageCode='de',
maxAlternatives=1,
enableWordTimeOffsets=True,
enableAutomaticPunctuation=True,
model='default',
encoding='ENCODING_UNSPECIFIED'
)
我收到这个错误
收到的JSON有效负载无效。 'config'处的未知名称“encoding”:Proto字段不重复,无法启动列表
怎么解决?
能否请您提供更多信息......比如您在项目的这一部分使用哪种语言和图书馆版本?
假设您使用的是Python,您可以在这里找到另一种将Google Cloud Speech连接到Text Api的官方方式:https://cloud.google.com/speech-to-text/docs/basics
我以前的方式是使用googleapiclient
python包以及JSON数据结构而不是字典数据。
import base64
import googleapiclient.discovery
with open(speech_file, 'rb') as speech:
# Base64 encode the binary audio file for inclusion in the JSON
# request.
speech_content = base64.b64encode(speech.read())
# Construct the request
service = googleapiclient.discovery.build('speech', 'v1')
service_request = service.speech().recognize(
body={
"config": {
"encoding": "LINEAR16", # raw 16-bit signed LE samples
"sampleRateHertz": 16000, # 16 khz
"languageCode": "en-US", # a BCP-47 language tag
},
"audio": {
"content": speech_content
}
})
如果您不知道如何安装python包,请参阅此官方文章:https://packaging.python.org/tutorials/installing-packages/#id13
有关长期运行的请求,请参阅:qazxsw poi
在这种情况下,配置JSON结构将是:
https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/longrunningrecognize
RecognitionConfig是这种类型的JSON对象:
{
"config": {
object(RecognitionConfig)
},
"audio": {
object(RecognitionAudio)
}
}
而RecognitionAudio就是这样的:
{
"encoding": enum(AudioEncoding),
"sampleRateHertz": number,
"languageCode": string,
"maxAlternatives": number,
"profanityFilter": boolean,
"speechContexts": [
{
object(SpeechContext)
}
],
"enableWordTimeOffsets": boolean
}
对于LongRunning识别,您也可以参考以下链接:{
// Union field audio_source can be only one of the following:
"content": string,
"uri": string
// End of list of possible types for union field audio_source.
}
它展示了如何使用Python包qazxsw poi来处理长时间运行的请求,这只是通过在Phyton类中使用以下方法:
https://developers.google.com/resources/api-libraries/documentation/speech/v1/java/latest/com/google/api/services/speech/v1/Speech.SpeechOperations.html