Google 云 - 收到无效的 JSON 负载。 'config' 处的未知名称 "encoding":原型字段不重复,无法启动列表
Google Cloud - Invalid JSON payload received. Unknown name "encoding" at 'config': Proto field is not repeating, cannot start list
当我尝试使用以下配置调用 Google Cloud Speech to Text Api 进行长运行 识别时:
config = dict(
languageCode='de',
maxAlternatives=1,
enableWordTimeOffsets=True,
enableAutomaticPunctuation=True,
model='default',
encoding='ENCODING_UNSPECIFIED'
)
我收到这个错误
Invalid JSON payload received. Unknown name "encoding" at 'config': Proto field is not repeating, cannot start list
如何解决?
能否请您提供更多信息...例如您在这部分项目中使用的是哪种语言和库版本?
假设您正在使用 Python,您可以找到另一种连接到 Google Cloud 的官方方式语音转文本 Api 此处:https://cloud.google.com/speech-to-text/docs/basics
我习惯的做法是使用 googleapiclient
phyton 包和 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
LongRunning请求请参考:
https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/longrunningrecognize
本例中的配置 JSON 结构为:
{
"config": {
object(RecognitionConfig)
},
"audio": {
object(RecognitionAudio)
}
}
其中 RecognitionConfig 是一个 JSON 类型的对象:
{
"encoding": enum(AudioEncoding),
"sampleRateHertz": number,
"languageCode": string,
"maxAlternatives": number,
"profanityFilter": boolean,
"speechContexts": [
{
object(SpeechContext)
}
],
"enableWordTimeOffsets": boolean
}
而RecognitionAudio是这样的:
{
// 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.
}
LongRunning的识别,也可以参考这个link:
https://developers.google.com/resources/api-libraries/documentation/speech/v1/java/latest/com/google/api/services/speech/v1/Speech.SpeechOperations.html
它展示了如何使用 Phyton 包 googleapiclient.discovery
来处理长 运行 请求,只需在你的 Phyton class 中使用以下方法即可:
...
service_request = service.speech().longrunningrecognize(
body= {
"config": {
"encoding": "FLAC",
"languageCode": "en-US",
"enableWordTimeOffsets": True
},
"audio": {
"uri": str('gs://speech-clips/'+self.audio_fqid)
}
}
)
...
当我尝试使用以下配置调用 Google Cloud Speech to Text Api 进行长运行 识别时:
config = dict(
languageCode='de',
maxAlternatives=1,
enableWordTimeOffsets=True,
enableAutomaticPunctuation=True,
model='default',
encoding='ENCODING_UNSPECIFIED'
)
我收到这个错误
Invalid JSON payload received. Unknown name "encoding" at 'config': Proto field is not repeating, cannot start list
如何解决?
能否请您提供更多信息...例如您在这部分项目中使用的是哪种语言和库版本?
假设您正在使用 Python,您可以找到另一种连接到 Google Cloud 的官方方式语音转文本 Api 此处:https://cloud.google.com/speech-to-text/docs/basics
我习惯的做法是使用 googleapiclient
phyton 包和 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
LongRunning请求请参考: https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/longrunningrecognize
本例中的配置 JSON 结构为:
{
"config": {
object(RecognitionConfig)
},
"audio": {
object(RecognitionAudio)
}
}
其中 RecognitionConfig 是一个 JSON 类型的对象:
{
"encoding": enum(AudioEncoding),
"sampleRateHertz": number,
"languageCode": string,
"maxAlternatives": number,
"profanityFilter": boolean,
"speechContexts": [
{
object(SpeechContext)
}
],
"enableWordTimeOffsets": boolean
}
而RecognitionAudio是这样的:
{
// 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.
}
LongRunning的识别,也可以参考这个link: https://developers.google.com/resources/api-libraries/documentation/speech/v1/java/latest/com/google/api/services/speech/v1/Speech.SpeechOperations.html
它展示了如何使用 Phyton 包 googleapiclient.discovery
来处理长 运行 请求,只需在你的 Phyton class 中使用以下方法即可:
...
service_request = service.speech().longrunningrecognize(
body= {
"config": {
"encoding": "FLAC",
"languageCode": "en-US",
"enableWordTimeOffsets": True
},
"audio": {
"uri": str('gs://speech-clips/'+self.audio_fqid)
}
}
)
...