1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| from urllib import request import json, base64, uuid, os import wave import pycurl import io
bda_app_id = "XXXXXXXXX" bda_api_key = "XXXXXXXXX" bda_secret_key = "XXXXXXXXX"
bda_access_token = "" bda_expires_in = "" ret_text = ""
def get_mac_address(): return uuid.UUID(int=uuid.getnode()).hex[-12:]
def get_access_token(): url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=ZrjLfF5Rh7pOL66gaOmDGnXn&client_secret=16bac9645093ca2632ebb81015ff7544"
req = request.Request(url, method="POST") resp = request.urlopen(req) data = resp.read().decode('utf-8') json_data = json.loads(data)
global bda_access_token bda_access_token = json_data['access_token']
return bda_access_token
CHUNK = 1024 def get_wav_data(wav_path): if wav_path is None or len(wav_path) == 0: return None
wav_file = wave.open(wav_path, 'rb') nframes = wav_file.getnframes() audio_data = wav_file.readframes(nframes)
return audio_data, nframes
def dump_res(buf): resp_json = json.loads(buf.decode('utf-8')) ret = resp_json['result']
global ret_text ret_text = ret[0]
print(buf)
def wav_to_text(wav_path): if wav_path is None or len(wav_path) == 0: return None
if len(bda_access_token) == 0: get_access_token() if len(bda_access_token) == 0: return None
data, f_len = get_wav_data(wav_path)
url = 'http://vop.baidu.com/server_api?cuid=' + get_mac_address() + '&token=' + bda_access_token http_header = [ 'Content-Type: audio/pcm; rate=8000', 'Content-Length: %d' % f_len ]
c = pycurl.Curl() c.setopt(pycurl.URL, str(url)) c.setopt(c.HTTPHEADER, http_header) c.setopt(c.POST, 1) c.setopt(c.CONNECTTIMEOUT, 30) c.setopt(c.TIMEOUT, 30) c.setopt(c.WRITEFUNCTION, dump_res) c.setopt(c.POSTFIELDS, data) c.setopt(c.POSTFIELDSIZE, f_len) c.perform()
return ret_text
|