日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python調用百度語音識別api

系統 1703 0

最近在處理語音檢索相關的事。
其中用到語音識別,調用的是訊飛與百度的api,前者使用js是實現,后者用python3實現(因為自己使用python)

環境:

python3.5
centos 7

流程

整個百度語音識別rest api 使用分為三部分:

1 (申請操作)創建應用,獲取應用的 API Key 以及 Secret Key。

2 (程序實現)通過已知的 應用的 API Key 以及 Secret Key, 發送post 請求到 https://openapi.baidu.com/oauth/2.0/token 獲取 token

3 (程序實現) 通過上步驟獲取的 token,通過post, 發送相關的 語音信息 到 http://vop.baidu.com/server_api ,獲取識別結果.

以上過程參考百度語音開發文檔,或者網上的資料。

python實現

程序整體如下:

            
import requests
import json
import uuid
import base64

def get_token():
 url = "https://openapi.baidu.com/oauth/2.0/token"
 grant_type = "client_credentials"
 api_key = "NzGBYD0jPFDqVT8VHRYa****"  # 自己申請的應用
 secret_key = "8439155b9db2040b4acd13b0c*****" # 自己申請的應用
 data = {'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key}
 r = requests.post(url, data=data)
 token = json.loads(r.text).get("access_token")
 return token


def recognize(sig, rate, token):
 url = "http://vop.baidu.com/server_api"
 speech_length = len(sig)
 speech = base64.b64encode(sig).decode("utf-8")
 mac_address = uuid.UUID(int=uuid.getnode()).hex[-12:]
 rate = rate
 data = {
 "format": "wav",
 "lan": "zh",
 "token": token,
 "len": speech_length,
 "rate": rate,
 "speech": speech,
 "cuid": mac_address,
 "channel": 1,
 }
 data_length = len(json.dumps(data).encode("utf-8"))
 headers = {"Content-Type": "application/json",
 "Content-Length": data_length}
 r = requests.post(url, data=json.dumps(data), headers=headers)
 print(r.text)


filename = "two.wav"

signal = open(filename, "rb").read()
rate = 8000

token = get_token()
recognize(signal, rate, token)


          

同時,獲取語音信息可以通過:

            
import scipy.io.wavfile
filename = "two.wav"
rate, signal = scipy.io.wavfile.read(filename=filename)

          

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 大姚县| 合作市| 阿拉尔市| 祁连县| 曲阳县| 年辖:市辖区| 若尔盖县| 伊通| 夏河县| 华亭县| 理塘县| 娄底市| 诸城市| 灯塔市| 瑞丽市| 渑池县| 长丰县| 三江| 祁阳县| 宁武县| 苍梧县| 五寨县| 江山市| 凤庆县| 宜兴市| 宜良县| 兰溪市| 万宁市| 习水县| 报价| 沁水县| 屯昌县| 临沂市| 宜都市| 什邡市| 高陵县| 松阳县| 拜城县| 义乌市| 黄骅市| 临汾市|