2016-08-28 14 views
0

IBM Speech-To-TextにアクセスするためのPythonスクリプトを作成しようとしています。IBM Watson Speech To Pythonを使用してテキストに壊れたパイプを要求する

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" 

と私は、次のを思い付いた:私は自分のサイト上での例をカールする同等のコマンドを作ってみまし秒1を与えながら、端末から細かい実行

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')) 

どうすればこの問題を解決できますか?

答えて

2

'Transfer-Encoding': 'chunked'を削除し、brrbに変更してください。 requestsライブラリは正しいヘッダーを設定します。

一方、あなたの人生を楽にする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)) 
関連する問題