程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python realizes voice to text (Baidu interface)

編輯:Python
import json
import base64
import os
import requests
RATE = "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"
# Spell it url
url ="%sgrant_type=%s&client_id=%s&client_secret=%s"%(server,grant_type,client_id,client_secret)
# obtain token
res = requests.post(url)
token = json.loads(res.text)["access_token"]
return token
def 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={
"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 by wav File path

Secret key and Api key Parameters obtained for Baidu interface


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved