百度語音識別--示例
阿新 • • 發佈:2019-01-04
終於找到能跑通的,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])