[英]Transcribing sound files to text in python and google speech api
我在WAV中有一堆文件。 我做了一個簡單的腳本,將它們轉換為flac,因此可以將其與Google Speech API結合使用。 這是python代碼:
import urllib2
url = "https://www.google.com/speech-api/v1/recognize?client=chromium&lang=en-US"
audio = open('somefile.flac','rb').read()
headers={'Content-Type': 'audio/x-flac; rate=16000', 'User-Agent':'Mozilla/5.0'}
request = urllib2.Request(url, data=audio, headers=headers)
response = urllib2.urlopen(request)
print response.read()
但是我收到此錯誤:
Traceback (most recent call last):
File "transcribe.py", line 7, in <module>
response = urllib2.urlopen(request)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 392, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 370, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1194, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1161, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 32] Broken pipe>
起初我以為是因為文件太大。 但是我記錄了自己5秒鍾,但仍然如此。
我認為Google ha還沒有發布該api,因此很難理解為什么它會失敗。
是否還有其他可以在Python或Node中使用的其他出色的語音轉文本api?
-----根據請求編輯我的嘗試:
import json
import requests
url = 'https://www.google.com/speech-api/v1/recognize?client=chromium&lang=en-US'
data = {'file': open('file.flac', 'rb')}
headers = {'Content-Type': 'audio/x-flac; rate=16000', 'User-Agent':'Mozilla/5.0'}
r = requests.post(url, data=data, headers=headers)
# r = requests.post(url, files=data, headers=headers) ## does not work either
# r = requests.post(url, data=open('file.flac', 'rb').read(), headers=headers) ## does not work either
print r.text
產生與上述相同的問題。
該API接受HTTP POST請求。 您在此處使用HTTP GET請求。 可以通過將代碼中的URI直接加載到瀏覽器中來確認:
HTTP method GET is not supported by this URL
Error 405
另外,我建議使用requests
python庫。 參見http://www.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
最后,似乎API僅接受長達15秒的段。 也許您的錯誤是文件太大? 如果您可以上傳flac文件示例,也許我們可以進一步診斷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.