如何使用Watson语音到文本api解决错误400

2024-05-29 00:01:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将音频(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

Tags: fromimportauthenticatorurlresponsestatuscodewatson
1条回答
网友
1楼 · 发布于 2024-05-29 00:01:41

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

可能需要

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

而不是audio=f


编辑:

{a1}的文档显示了使用

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()并向下滚动才能在文档中看到它

enter image description here

相关问题 更多 >

    热门问题