如何使用 Watson speech to text api 解决错误 400
How to resolve the error 400 using Watson speech to text api
我正在尝试将音频(mp3、m4a 或 flac)导入此代码,以便访问 Watson API 并获取文字记录。
我尝试过不同的音频文件,从视频中提取或直接由 windows 录音机录制。所有这些都具有接近 1 到 12MB 的不同大小。
但是总是return下面这个错误。我在其他类似问题的网站上没有找到答案。
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 bytes
并且 len("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))
您必须单击 link recognize() 并向下滚动才能在文档中看到它。
我正在尝试将音频(mp3、m4a 或 flac)导入此代码,以便访问 Watson API 并获取文字记录。 我尝试过不同的音频文件,从视频中提取或直接由 windows 录音机录制。所有这些都具有接近 1 到 12MB 的不同大小。 但是总是return下面这个错误。我在其他类似问题的网站上没有找到答案。
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 bytes
并且 len("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))
您必须单击 link recognize() 并向下滚动才能在文档中看到它。