1、登录阿里云免费试用网站开通免费试用功能,按提示完成身份认证
/product/nls/freetrial?spm=5176.12061034.2743742960.3.5b787403LTwEYt
2、创建项目
创建完成后可看到创建的项目:
3、获取AccessToken
访问令牌(Access Token)是调用智能语音交互服务的凭证
默认进入总览,单击页面右上角点击获取AccessToken。
方便快速对接SDK,测试语音服务,该Token仅供测试使用。
4、调用服务
# -*- coding: utf-8 -*-import http.clientimport jsondef process(request, token, audioFile) :# 读取音频文件with open(audioFile, mode = 'rb') as f:audioContent = f.read()host = 'nls--'# 设置HTTP请求头部httpHeaders = {'X-NLS-Token': token,'Content-type': 'application/octet-stream','Content-Length': len(audioContent)}# Python 2.x使用httplib# conn = httplib.HTTPConnection(host)# Python 3.x使用http.clientconn = http.client.HTTPConnection(host)conn.request(method='POST', url=request, body=audioContent, headers=httpHeaders)response = conn.getresponse()print('Response status and response reason:')print(response.status ,response.reason)body = response.read()try:print('Recognize response is:')body = json.loads(body)print(body)status = body['status']if status == 20000000 :result = body['result']print('Recognize result: ' + result)else :print('Recognizer failed!')except ValueError:print('The response is not json format string')conn.close()appKey = 'your appKey'token = 'your token'# 服务请求地址url = 'http://nls--/stream/v1/asr'# 音频文件audioFile = 'test.wav'format = 'pcm'sampleRate = 16000enablePunctuationPrediction = TrueenableInverseTextNormalization = TrueenableVoiceDetection = False# 设置RESTful请求参数request = url + '?appkey=' + appKeyrequest = request + '&format=' + formatrequest = request + '&sample_rate=' + str(sampleRate)if enablePunctuationPrediction :request = request + '&enable_punctuation_prediction=' + 'true'if enableInverseTextNormalization :request = request + '&enable_inverse_text_normalization=' + 'true'if enableVoiceDetection :request = request + '&enable_voice_detection=' + 'true'print('Request: ' + request)process(request, token, audioFile)