日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python使用百度语音识别API注意事项

發(fā)布時(shí)間:2023/12/20 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python使用百度语音识别API注意事项 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.



代碼如下:

# -*- coding:utf-8 -*- #http://blog.csdn.net/happen23/article/details/45821697 #百度語音識(shí)別API的使用樣例(python實(shí)現(xiàn)) #encoding=utf-8import wave import urllib, urllib2, pycurl import base64 import json ## get access token by api key & secret keydef get_token():apiKey = "xjlSpsvUgGF8a9ltNOtREoTr"secretKey = "a95ca71b81854b526e7eb04ae8f51d23"auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;res = urllib2.urlopen(auth_url)json_data = res.read()return json.loads(json_data)['access_token']def dump_res(buf):print buf## post audio to server def use_cloud(token):fp = wave.open('8k.wav', 'rb')nf = fp.getnframes()f_len = nf * 2audio_data = fp.readframes(nf)cuid = "xxxxxxxxxx" #my xiaomi phone MACsrv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + tokenhttp_header = ['Content-Type: audio/pcm; rate=8000','Content-Length: %d' % f_len]c = pycurl.Curl()c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode#c.setopt(c.RETURNTRANSFER, 1)c.setopt(c.HTTPHEADER, http_header) #must be list, not dictc.setopt(c.POST, 1)c.setopt(c.CONNECTTIMEOUT, 30)c.setopt(c.TIMEOUT, 30)c.setopt(c.WRITEFUNCTION, dump_res)c.setopt(c.POSTFIELDS, audio_data)c.setopt(c.POSTFIELDSIZE, f_len)c.perform() #pycurl.perform() has no return valif __name__ == "__main__":token = get_token()use_cloud(token)



以上代碼是不能直接跑的,一般錄音得到的音頻文件,該代碼下運(yùn)行不了,目前只知道百度提供的音頻文件可以識(shí)別,百度提供的音頻文件下載鏈接如下:

http://yuyin.baidu.com/docs/asr/54

在上面這個(gè)鏈接的頁(yè)面中,往下拖,可以得到下載鏈接如下:
http://speech-doc.gz.bcebos.com/rest-api-asr/public_audio/public.zip


然后運(yùn)行結(jié)果:

{"corpus_no":"6485972281376050071","err_msg":"success.","err_no":0,"result":["北京科技館,"],"sn":"651708407021510133099"}


注意事項(xiàng):

百度的語音識(shí)別和語音合成用的是相同的

appid、API key和Secret Key,所以使用相同的token即可

獲取以上三個(gè)字段的教程:

http://jingyan.baidu.com/article/f3e34a12df0cddf5eb65359f.html

后記:


下面的代碼可以運(yùn)行任意自己錄制的音頻文件,注意,運(yùn)行前必須apt-get install ffmpeg

另外,rate改成了16000,不然會(huì)識(shí)別不準(zhǔn),不過,也沒有經(jīng)過大量測(cè)試,不知道識(shí)別準(zhǔn)確的情況還會(huì)不會(huì)出現(xiàn)。

# -*- coding:utf-8 -*- #http://blog.csdn.net/happen23/article/details/45821697 #百度語音識(shí)別API的使用樣例(python實(shí)現(xiàn)) #encoding=utf-8import wave import urllib, urllib2, pycurl import base64 import subprocess import json ## get access token by api key & secret keydef get_token():apiKey = "xjlSpsvUgGF8a9ltNOtREoTr"secretKey = "a95ca71b81854b526e7eb04ae8f51d23"auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;res = urllib2.urlopen(auth_url)json_data = res.read()return json.loads(json_data)['access_token']def dump_res(buf):print buf## post audio to server def use_cloud(token):subprocess.call(['ffmpeg', '-i', 'tian.mp3', 'tian.wav'])#這句代碼的意思是在終端中執(zhí)行[]中的命令。所以執(zhí)行的前提是apt-get install ffmpegfp = wave.open('tian.wav', 'rb')nf = fp.getnframes()f_len = nf * 2audio_data = fp.readframes(nf)cuid = "xxxxxxxxxx" #my xiaomi phone MACsrv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + tokenhttp_header = ['Content-Type: audio/pcm; rate=16000','Content-Length: %d' % f_len]c = pycurl.Curl()c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode#c.setopt(c.RETURNTRANSFER, 1)c.setopt(c.HTTPHEADER, http_header) #must be list, not dictc.setopt(c.POST, 1)c.setopt(c.CONNECTTIMEOUT, 30)c.setopt(c.TIMEOUT, 30)c.setopt(c.WRITEFUNCTION, dump_res)c.setopt(c.POSTFIELDS, audio_data)c.setopt(c.POSTFIELDSIZE, f_len)c.perform() #pycurl.perform() has no return valif __name__ == "__main__":token = get_token()use_cloud(token)所謂的離線資源下載,其實(shí)仍然是本地向服務(wù)器請(qǐng)求,效率上是無法提高的。


創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python使用百度语音识别API注意事项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。