2016-08-28 3 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" 

내가 해낸 다음 두 번째가 제공하는 동안

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