700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 调用阿里云语音转文本

调用阿里云语音转文本

时间:2022-12-03 14:44:02

相关推荐

调用阿里云语音转文本

文章目录

一、accessKeyId和accessKeySecret申请二、appKey申请三、调用接口代码

一、accessKeyId和accessKeySecret申请

二、appKey申请

创建项目既有

三、调用接口代码

安装包

pip install aliyun-python-sdk-core==2.13.3

# -*- coding: utf8 -*-import jsonimport timefrom aliyunsdkcore.acs_exception.exceptions import ClientExceptionfrom aliyunsdkcore.acs_exception.exceptions import ServerExceptionfrom aliyunsdkcore.client import AcsClientfrom aliyunsdkcore.request import CommonRequestdef fileTrans(akId, akSecret, appKey, fileLink) :# 地域ID,固定值。REGION_ID = "cn-shanghai"PRODUCT = "nls-filetrans"DOMAIN = "-"API_VERSION = "-08-17"POST_REQUEST_ACTION = "SubmitTask"GET_REQUEST_ACTION = "GetTaskResult"# 请求参数KEY_APP_KEY = "appkey"KEY_FILE_LINK = "file_link"KEY_VERSION = "version"KEY_ENABLE_WORDS = "enable_words"# 是否开启智能分轨KEY_AUTO_SPLIT = "auto_split"# 响应参数KEY_TASK = "Task"KEY_TASK_ID = "TaskId"KEY_STATUS_TEXT = "StatusText"KEY_RESULT = "Result"# 状态值STATUS_SUCCESS = "SUCCESS"STATUS_RUNNING = "RUNNING"STATUS_QUEUEING = "QUEUEING"# 创建AcsClient实例client = AcsClient(akId, akSecret, REGION_ID)# 提交录音文件识别请求postRequest = CommonRequest()postRequest.set_domain(DOMAIN)postRequest.set_version(API_VERSION)postRequest.set_product(PRODUCT)postRequest.set_action_name(POST_REQUEST_ACTION)postRequest.set_method('POST')# 新接入请使用4.0版本,已接入(默认2.0)如需维持现状,请注释掉该参数设置。# 设置是否输出词信息,默认为false,开启时需要设置version为4.0。task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False}# 开启智能分轨,如果开启智能分轨,task中设置KEY_AUTO_SPLIT为True。# task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True}task = json.dumps(task)print(task)postRequest.add_body_params(KEY_TASK, task)taskId = ""try :postResponse = client.do_action_with_exception(postRequest)postResponse = json.loads(postResponse)print (postResponse)statusText = postResponse[KEY_STATUS_TEXT]if statusText == STATUS_SUCCESS :print ("录音文件识别请求成功响应!")taskId = postResponse[KEY_TASK_ID]else :print ("录音文件识别请求失败!")returnexcept ServerException as e:print (e)except ClientException as e:print (e)# 创建CommonRequest,设置任务ID。getRequest = CommonRequest()getRequest.set_domain(DOMAIN)getRequest.set_version(API_VERSION)getRequest.set_product(PRODUCT)getRequest.set_action_name(GET_REQUEST_ACTION)getRequest.set_method('GET')getRequest.add_query_param(KEY_TASK_ID, taskId)# 提交录音文件识别结果查询请求# 以轮询的方式进行识别结果的查询,直到服务端返回的状态描述符为"SUCCESS"、"SUCCESS_WITH_NO_VALID_FRAGMENT",# 或者为错误描述,则结束轮询。statusText = ""while True :try :getResponse = client.do_action_with_exception(getRequest)getResponse = json.loads(getResponse)print (getResponse)statusText = getResponse[KEY_STATUS_TEXT]if statusText == STATUS_RUNNING or statusText == STATUS_QUEUEING :# 继续轮询time.sleep(10)else :# 退出轮询breakexcept ServerException as e:print (e)except ClientException as e:print (e)if statusText == STATUS_SUCCESS :print ("录音文件识别成功!")else :print ("录音文件识别失败!")return getResponseaccessKeyId = "您的AccessKey Id"accessKeySecret = "您的AccessKey Secret"appKey = "您的appkey"fileLink = "/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav"# 执行录音文件识别r = fileTrans(accessKeyId, accessKeySecret, appKey, fileLink)print(r)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。