【问题标题】:IBM Watson Speech To Text using Python Requests Broken PipeIBM Watson Speech To Text 使用 Python 请求 Broken Pipe
【发布时间】:2016-08-28 11:15:55
【问题描述】:

我正在尝试制作一个 python 脚本来访问 IBM Speech-To-Text。我试图在他们的网站上创建一个与 cURL 示例等效的命令:

curl -X POST -u <username>:<password>
--header "Content-Type: audio/flac"
--header "Transfer-Encoding: chunked"
--data-binary @<path>0001.flac 
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true&timestamps=true&max_alternatives=3"

我想出了以下几点:

headers = {
    'Content-Type': 'audio/flac',
    'Transfer-Encoding': 'chunked',
}
auth = (USERNAME, PASSWORD)
data = open(audio_name, 'br')
r = requests.post('https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true&timestamps=true&max_alternatives=3'
                  , headers=headers, data=data, auth=auth)

第一个命令从终端执行得很好,而第二个命令给了我:

requests.exceptions.ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))

我该如何解决这个问题?

【问题讨论】:

    标签: python-3.x curl python-requests ibm-watson


    【解决方案1】:

    删除'Transfer-Encoding': 'chunked' 并将br 更改为rbrequests 库将设置正确的标题。

    另一方面,我建议您使用 WDC Python SDK,这将使您的生活更轻松。

    使用pip安装它:

    pip install watson-developer-cloud
    

    然后:

    import json
    from os.path import join, dirname
    from watson_developer_cloud import SpeechToTextV1
    
    
    speech_to_text = SpeechToTextV1(
        username='YOUR SERVICE USERNAME',
        password='YOUR SERVICE PASSWORD',
    )
    
    
    with open(join(dirname(__file__), './0001.flac'), 'rb') as audio_file:
        print(json.dumps(speech_to_text.recognize(
              audio_file, content_type='audio/wav', timestamps=True,
              continuous=True, timestamps=True, max_alternatives=3
             ), indent=2))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-28
      • 2019-04-02
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多