【问题标题】:Transcribing sound files to text in python and google speech api在 python 和谷歌语音 api 中将声音文件转录为文本
【发布时间】:2013-10-30 20:28:43
【问题描述】:

我有一堆 wav 文件。我制作了一个简单的脚本来将它们转换为 flac,这样我就可以将它与谷歌语音 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 秒,还是一样。

我不认为谷歌已经发布了 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

产生了和上面一样的问题。

【问题讨论】:

    标签: python speech-to-text


    【解决方案1】:

    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 文件,也许我们可以进一步诊断。

    【讨论】:

    • 这是一个好点,我尝试了那个代码,但我得到了同样的破管道:(这部分不接受代码,检查顶部)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多