将Curl转换为Python|IBM STT



我正在尝试将下面的curl命令转换为python

curl -X POST -u "apikey:my_key" --header "Content-Type:" --data-binary @testaudio1.wav "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?speaker_labels=true"

curl命令是一个有效的命令,但我无法获得与python对应的命令。

我尝试了以下代码

import json, sys
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1
wav_file = sys.argv[1]
speech_to_text = SpeechToTextV1(
iam_apikey='my_api',
url='https://gateway-lon.watsonplatform.net/speech-to-text/api/v1'
)
with open(wav_file, 'rb') as audio:
result = speech_to_text.recognize(audio, content_type='audio/wav', timestamps=True,
word_confidence=True, speaker_labels=True)

但它似乎抛出了一个错误AppDataLocalContinuumAnaconda3libsocket.py", line 732, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed

使用requests模块。

例如:

import requests
with open(wav_file, 'rb') as audio:
response = requests.post("https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize?speaker_labels=true",
headers={'apikey': 'my_key'},
data=audio))

最新更新