廢話多了,現在看看Google的猥瑣API之STT!(屬於speech-api v1的recognize)
http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN
不過使用不是那么簡單的哦……
如何調用
要使用這個API,你得先有一些心理素質:面對倒霉的FLAC格式,因為這個API只支持FLAC格式的數據,杯具啊。
不過你找到了我的博客,那么你是幸福的,因為我已經為你准備了不少好東西了。(順便說一句,如果要轉載記得清楚地標注“來自http://blog.laobubu.net”,我信任你。)
現在看看如何請求數據:
- 【URL】http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN
- 【方式】POST
- 【請求頭】Content-Type:audio/x-flac; rate=16000
- 【POST】flac文件的數據
- 【URL里可選參數】&maxresults=返回結果數
如果你人品大爆發,你成功了,可以得到類似這個的結果
{"status":0,"id":"54e1babccaa58682ffbb02ceb42aa47c-1", "hypotheses":[{"utterance":"測試程序","confidence":0.8556527}]}或者你的請求URL里設置了maxresults(比如 http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=10 )你就會得到:
{"status":0,"id":"fbf23a887b9ac2bfb630aa40dd1a776c-1","hypotheses": [ {"utterance":"歡迎訪問過的網址","confidence":0.7275984}, {"utterance":"歡迎訪問我的網站"}, {"utterance":"歡迎訪問我的網址"}, {"utterance":"歡迎訪問過的網站"}, {"utterance":"白雲訪問過的網址"}, {"utterance":"懷孕訪問過的網址"}, {"utterance":"歡迎訪問有的網址"}, {"utterance":"歡迎訪問過的網址哦"}, {"utterance":"白雲訪問我的網站"}, {"utterance":"懷孕訪問我的網站"} ] }不過不簡單哦
實例
Python代碼
#By laobubu.net import urllib2 FILE='1.flac' #這里假設在當前文件夾下有一個叫1.flac的文件被識別 url = 'http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN' audio=open(FILE,'rb').read() headers = {'Content-Type' : 'audio/x-flac; rate=16000'} req = urllib2.Request(url, audio, headers) response = urllib2.urlopen(req) print response.read().decode('UTF-8')
PHP代碼
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=10"); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('1.flac')); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/x-flac; rate=16000")); $data = curl_exec($ch); curl_close($ch); if ($data=json_decode($data,true)) { echo "<ul>"; foreach($data['hypotheses'] as $i) echo "<li>".$i['utterance']."</li>"; echo "</ul>"; } else { echo "<i>識別出錯</i>"; }
頭疼:FLAC文件格式
這里我長話短說,google的flac也有限制,22050 Hz+201kbps還正常,如果比這個大多了就不對了。
不過還有最要命的問題,怎么生成FLAC文件?
沒關系,這里有救星:flac.exe包下載
http://datastorage.laobubu.net/FLAC.zip
具體使用方法是【flac.exe 文件.wav】這樣在命令行調用即可,測試得知:不支持MP3等格式,但是支持基本的wav格式。
至於其他環境下(如linux里)也有對應的flac下載。我沒有怎么用過linux就不說啥了。
好了,你可以去制造你的app了,哈哈。
來自laobubu實驗室自動轉換API
為了方便,我寫了一個python程序,放在Google App Engine上面,只要提交一個音頻文件的URL(支持MP3、wav等常見格式),且文件不大,就可以得到結果了哦。
API入口: http://laobubumf.appspot.com/stt/
備用入口: http://glab.laobubu.net/stt/