json.dumps ValueError: Extra data unable to dump json outputs
json.dumps ValueError: Extra data unable to dump json outputs
我正在尝试使用语音向 watson api 发送文本,但是当我设置 interim_results = True
时,我收到了值错误。请帮助我:)
with open(join(dirname(__file__), './audio-file.wav'), 'rb') as audio_file:
print(json.dumps(speech_to_text.recognize(
audio_file, content_type='audio/wav', timestamps=True, interim_results =True, word_confidence=True), indent=2))
错误输出:
然而,当我设置 interim_results = False
时,我得到了正常工作的输出。
interim_results = False
时的输出:
我认为原因与多个 json 输出有关,但我不知道如何解决,因为这是 json.dumps:) 所以我无法参考 json.loads 值错误案例来解决这个案例。
如果 interim_results=true,服务会发回多个 JSON blob,期望您在它们到达时单独解析它们。例如,如果您想显示近乎实时的转录,这将很有用。
如果您只是进行一次性转录并且不需要近乎实时地显示文本,我建议您将 interim_results 设置为 false。
您 可以 围绕 }\s*{
拆分结果(其中一个 JSON blob 结束,下一个开始),然后将每个单独的块解析为JSON(如有必要,恢复 }
和 {
),但它不会给您带来任何好处,因为完整的最终结果已经存在。
或者,如果您进行 need/want 近乎实时的更新,WebSocket 接口会使这更容易一些,因为每个 JSON 块到达它自己的消息 - 查看 https://github.com/watson-developer-cloud/speech-to-text-websockets-python一个例子。
我正在尝试使用语音向 watson api 发送文本,但是当我设置 interim_results = True
时,我收到了值错误。请帮助我:)
with open(join(dirname(__file__), './audio-file.wav'), 'rb') as audio_file:
print(json.dumps(speech_to_text.recognize(
audio_file, content_type='audio/wav', timestamps=True, interim_results =True, word_confidence=True), indent=2))
错误输出:
然而,当我设置 interim_results = False
时,我得到了正常工作的输出。
interim_results = False
时的输出:
我认为原因与多个 json 输出有关,但我不知道如何解决,因为这是 json.dumps:) 所以我无法参考 json.loads 值错误案例来解决这个案例。
如果 interim_results=true,服务会发回多个 JSON blob,期望您在它们到达时单独解析它们。例如,如果您想显示近乎实时的转录,这将很有用。
如果您只是进行一次性转录并且不需要近乎实时地显示文本,我建议您将 interim_results 设置为 false。
您 可以 围绕 }\s*{
拆分结果(其中一个 JSON blob 结束,下一个开始),然后将每个单独的块解析为JSON(如有必要,恢复 }
和 {
),但它不会给您带来任何好处,因为完整的最终结果已经存在。
或者,如果您进行 need/want 近乎实时的更新,WebSocket 接口会使这更容易一些,因为每个 JSON 块到达它自己的消息 - 查看 https://github.com/watson-developer-cloud/speech-to-text-websockets-python一个例子。