最近公司在對接阿里云語音識別(一句話識別),阿里云文檔提供的并不是很詳細(xì)坪圾,python demo不全讼油,修改后可直接執(zhí)行杰赛,如有問題可簡書聯(lián)系我,大家一起討論矮台。
import hashlib
import hmac
import base64
import datetime
import ssl
import requests
class HttpProxy:
"""
Http工具類乏屯,封裝了鑒權(quán)
"""
def __init__(self, ak_id, ak_secret, model):
self.__ak_id = ak_id
self.__ak_secret = ak_secret
# 必填
self.__version = "2.0"
self.__model = model
# 音頻文件長度
self.f_len = None
def __current_gmt_time(self):
return datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")
def __md5_base64(self, body):
hash = hashlib.md5()
hash.update(body)
return base64.b64encode(hash.digest()).decode('utf-8')
def __sha1_base64(self, str_to_sign, secret):
hmacsha1 = hmac.new(secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha1)
return base64.b64encode(hmacsha1.digest()).decode('utf-8')
def send_request(self, body, audio_format, sample_rate, url):
gmtnow = self.__current_gmt_time()
temp_body = self.__md5_base64(body)
body_md5 = self.__md5_base64(temp_body.encode('utf8'))
content_type = "audio/" + audio_format + ";samplerate=" + str(sample_rate)
str_to_sign = "POST" + "\n" + "application/json" + "\n" + body_md5 + "\n" + content_type + "\n" + gmtnow
signature = self.__sha1_base64(str_to_sign, self.__ak_secret)
auth_header = "Dataplus " + self.__ak_id + ":" + signature
ssl._create_default_https_context = ssl._create_unverified_context
url = url + "model=" + self.__model + "&" + "version=" + self.__version
headers = {
"Accept": "application/json",
"Content-Type": content_type,
"Date": gmtnow,
"Authorization": auth_header,
"Content-Length": str(self.f_len)
}
r = requests.post(url, data=body, headers=headers)
return r.json().get("result")
def read_file(self, file_path):
file = open(file_path, 'rb')
sound_wav_rb = file.read()
file.close()
# 這里要轉(zhuǎn)換成字節(jié)數(shù)組
temp_byte = bytearray(sound_wav_rb)
print(temp_byte)
return temp_byte
if __name__ == '__main__':
# chat可以根據(jù)自己的情景修改,這里阿里云文檔已經(jīng)標(biāo)明
http_proxy = HttpProxy("這里修改成你自己的ak_id", "這里修改成你自己的ak_secret", "chat")
# 讀取文檔
sound_wav_rb = http_proxy.read_file("/Users/macbook/Desktop/test.speex")
# 這里的參數(shù)詳見文檔
response = http_proxy.send_request(sound_wav_rb, "speex", 16000, "https://nlsapi.aliyun.com/recognize?")
print(response)
此腳本可直接執(zhí)行瘦赫,也可修改后對接前端進(jìn)行語音識別辰晕,希望對大家有用~