python|python实现语音转文字(百度接口)

import json import base64 import os import requestsRATE = "16000" FORMAT = "wav" CUID="wate_play" DEV_PID="1536"def get_token(): server = "https://openapi.baidu.com/oauth/2.0/token?" grant_type = "client_credentials" #API Key client_id = "vyYLov63W6x33nIPvwVdLLsX" #Secret Key client_secret = "EzgQoFF9xp62SeGCXCBaD8FjWNxvl9kZ"#拼url url ="%sgrant_type=%s&client_id=%s&client_secret=%s"%(server,grant_type,client_id,client_secret) #获取token res = requests.post(url) token = json.loads(res.text)["access_token"]return tokendef get_word(token): wavPath = './static/yuyin/record.wav' with open(wavPath, "rb") as f: speech = base64.b64encode(f.read()).decode('utf8') size = os.path.getsize(wavPath) headers = { 'Content-Type' : 'application/json'} url = "https://vop.baidu.com/server_api" data=https://www.it610.com/article/{"format":FORMAT, "rate":RATE, "dev_pid":DEV_PID, "speech":speech, "cuid":CUID, "len":size, "channel":1, "token":token, } try: req = requests.post(url,json.dumps(data),headers) result = json.loads(req.text) ret=result["result"][0] except : ret = None return ret[:-1]

wavPath 为wav文件路径
【python|python实现语音转文字(百度接口)】Secret key 和Api key为百度接口获取的参数

    推荐阅读