【问题标题】:How to resolve the error 400 using Watson speech to text api如何使用 Watson Speech to Text api 解决错误 400
【发布时间】:2021-06-16 17:31:04
【问题描述】:

我正在尝试将音频(mp3、m4a 或 flac)导入此代码,以便访问 Watson API 并获取脚本。 我尝试过使用不同的音频文件,从视频中提取或直接由 Windows Recorder 录制。它们都具有接近 1 到 12MB 的不同大小。 但总是在下面返回这个错误。我没有在其他网站上找到类似问题的答案。

pip install ibm_watson
apikey = 'xxxx'
url = 'yyyy'

from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import subprocess
import os

authenticator = IAMAuthenticator(apikey)
stt = SpeechToTextV1(authenticator = authenticator)
stt.set_service_url(url)

f= "file_path"
res = stt.recognize(audio=f, content_type='audio/m4a', model='en-US_NarrowbandModel', continuous=True, inactivity_timeout=360).get_result()

ApiException                              Traceback (most recent call last)
<ipython-input-24-cfbd4e46f426> in <module>()
      3 f= "file_path"
      4 res = stt.recognize(audio=f, content_type='audio/m4a', model='en-US_NarrowbandModel', continuous=True,
----> 5                     inactivity_timeout=360).get_result()

1 frames
/usr/local/lib/python3.7/dist-packages/ibm_cloud_sdk_core/base_service.py in send(self, request, **kwargs)
    300                                         status_code=response.status_code)
    301 
--> 302             raise ApiException(response.status_code, http_response=response)
    303         except requests.exceptions.SSLError:
    304             logging.exception(self.ERROR_MSG_DISABLE_SSL)

ApiException: Error: Stream was 9 bytes but needs to be at least 100 bytes., Code: 400 , X-global-transaction-id: 927c7d31-c030-4d71-8998-aa544b1ae111

【问题讨论】:

  • 错误显示Stream was 9 byteslen("file_path") 给出9。可能它需要audio=open(f, 'rb').read() 而不是audio=f

标签: python ibm-watson speech-to-text


【解决方案1】:

我无法测试它,但错误显示Stream was 9 byteslen("file_path") 给出9

可能需要

audio=open(f, 'rb').read() 

而不是audio=f


编辑:

recognize() 的文档显示了使用示例

with open('file_path', 'rb') as audio_file:

    speech_to_text.recognize(audio=audio_file, ...)

所以这意味着你可能需要

audio=open(f, 'rb')

没有.read()


文档中的完整示例:

import json
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator('{apikey}')
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url('{url}')

with open(join(dirname(__file__), './.', 'audio-file2.flac'),
               'rb') as audio_file:
    speech_recognition_results = speech_to_text.recognize(
        audio=audio_file,
        content_type='audio/flac',
        word_alternatives_threshold=0.9,
        keywords=['colorado', 'tornado', 'tornadoes'],
        keywords_threshold=0.5
    ).get_result()
print(json.dumps(speech_recognition_results, indent=2))

您必须单击链接recognize() 并向下滚动才能在文档中查看它。

【讨论】:

    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多