2016-03-01 76 views
0

我想使用Google的Speech API,使用下面的Python代碼和一個wav文件(或者其他格式的音頻文件)。我現在收到了一個壞的管道錯誤,我不知道該如何解決。已經閱讀了一些關於改變標題here,但覺得如果這是前進的方向,我需要一些指導。不知道這其實應該工作,使用Google Web Speech API Demourlopen,Google語音API

我的代碼:

#!/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) 

回答

1

您使用的URL是不正確的API的網址,網址爲V1的語音API是https://www.google.com/speech-api/v1/recognize,但是它已經被拒絕了很長一段時間了。詳情請參閱

Google speech Api v1 not working?

您可能需要使用流與谷歌API V2,但那些需要API密鑰,詳見https://github.com/gillesdemey/google-speech-v2

總體來說,我推薦你使用現有的包裝相反,它將隱藏所有的API複雜性。此包裝應該是不錯的:

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

你仍然需要從谷歌的API密鑰。

或者,您可以使用其他API端點,例如Microsoft的Project Oxford。

+0

好的,謝謝!似乎Google語音不再有API密鑰。我會研究其他的選擇,比如Mozilla的Web Speech API和Project Oxford! – Ingrid