百度語音識別--示例

終於找到能跑通的,copy下~~



import requests
import json
import os
import base64


#設置應用信息
baidu_server = "https://openapi.baidu.com/oauth/2.0/token?"
grant_type = "client_credentials"
client_id = "*****" #填寫API Key
client_secret = "****" #填寫Secret Key

#合成請求token的URL
url = baidu_server+"grant_type="+grant_type+"&client_id="+client_id+"&client_secret="+client_secret
print("url:",url)

#獲取token
res = requests.post(url)
print(res.text)
token = json.loads(res.text)["access_token"]
print(token)
#24.b891f76f5d48c0b9587f72e43b726817.2592000.1524124117.282335-10958516

#設置格式
RATE = "16000"
FORMAT = "wav"
CUID="wate_play"
DEV_PID="1536"

#以字節格式讀取文件之後進行編碼
with open(r'16k.wav', "rb") as f:
    speech = base64.b64encode(f.read()).decode('utf8')
size = os.path.getsize(r'16k.wav')
headers = { 'Content-Type' : 'application/json'}
url = "https://vop.baidu.com/server_api"
data={

        "format":FORMAT,
        "rate":RATE,
        "dev_pid":DEV_PID,
        "speech":speech,
        "cuid":CUID,
        "len":size,
        "channel":1,
        "token":token,
    }

req = requests.post(url,json.dumps(data),headers)
result = json.loads(req.text)
print(result["result"][0])






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章