简体   繁体   English

urlopen,Google语音API

[英]urlopen, Google speech API

I want to use Google's Speech API using the Python code below and a wav file (or an audio file in some other format for that matter). 我想使用下面的Python代码和wav文件(或其他某种格式的音频文件)使用Google的Speech API。 I get a broken pipe error at the moment, which I don't know how to fix. 目前,我收到了一个折断的管道错误,我不知道该如何解决。 Have been reading a bit about changing headers here , but feel I would need some guidance there if this is the way forward. 在这里已经阅读了一些有关更改标题的内容,但是如果这是前进的方向,我觉得我需要一些指导。 Don't know if this is actually supposed to work, using the Google Web Speech API Demo : 使用Google Web Speech API演示不知道这是否真的可行:

My code: 我的代码:

#!/usr/bin/python
import sys
import urllib.request
import urllib.parse
import json
import scipy.io.wavfile

try:
    filename = sys.argv[1]
except IndexError:
    print('Usage: transcribe.py <file>')
    sys.exit(1)

rate, data = scipy.io.wavfile.read(filename)

url = 'https://www.google.com/intl/en/chrome/demos/speech.html'
headers = {'Content-type': 'audio/wav; rate=16000'}
# Possibly use this somehow later on...
# user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
# values = {'name' : 'Michael Foord',
          'location' : 'Northampton',
          'language' : 'Python' }

req = urllib.request.Request(url, data, headers)

try:
    ret = urllib.request.urlopen(req)
except urllib.error.URLError as e:
    print(e.reason)
    sys.exit(1)

resp = ret.read()
text = json.loads(resp)['hypotheses'][0]['utterance']
print(text)

The url you use is not correct API url, the url for v1 speech API is https://www.google.com/speech-api/v1/recognize , however, it is turned down for quite some time already. 您使用的网址不是正确的API网址,v1语音API的网址为https://www.google.com/speech-api/v1/recognize ,但是,它已经被拒绝了一段时间。 See for details 查看详情

Google speech Api v1 not working? Google Speech Api v1无法正常工作吗?

You might want to use streaming API v2 with Google, but those requires API key, see for details https://github.com/gillesdemey/google-speech-v2 您可能希望将流式API v2与Google一起使用,但是它们需要API密钥,有关详细信息,请参见https://github.com/gillesdemey/google-speech-v2

Overall, I recommend you to use existing wrapper instead, it will hide all API complexity. 总的来说,我建议您改用现有的包装器,这样可以掩盖所有API的复杂性。 This wrapper should be good: 这个包装器应该很好:

https://pypi.python.org/pypi/SpeechRecognition/ https://pypi.python.org/pypi/SpeechRecognition/

You still need an API key from google. 您仍然需要Google提供的API密钥。

Alternatively you can use other API endpoint, like Project Oxford from Microsoft. 或者,您可以使用其他API端点,例如Microsoft的Project Oxford。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM