代码拉取完成,页面将自动刷新
# -*- coding: UTF-8 -*-
# Python 2.x引入httplib模块
# import httplib
# Python 3.x引入http.client模块
import http.client
import json
#https://help.aliyun.com/document_detail/187161.html
def process(request, audioFile) :
# 读取音频文件
with open(audioFile, mode = 'rb') as f:
audioContent = f.read()
host = 'nls-gateway.cn-shanghai.aliyuncs.com'
# 设置HTTP请求头部
httpHeaders = {
'Content-Length': len(audioContent)
}
# Python 2.x使用httplib
# conn = httplib.HTTPConnection(host)
# Python 3.x使用http.client
conn = 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 = '您的appkey'
token = '您的token'
from constant import appKey,token
# 服务请求地址
url = 'https://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/FlashRecognizer'
# 音频文件,下载地址:https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav
audioFile = 'nls-sample-16k.wav'
audioFile = 'getvoice.mp3'
format = audioFile.split('.')[-1]
sampleRate = 16000
print(type(sampleRate))
enablePunctuationPrediction = True
enableInverseTextNormalization = True
enableVoiceDetection = False
# 设置RESTful请求参数
request = url + '?appkey=' + appKey
request = request + '&token=' + token
request = request + '&format=' + format
request = request + '&sample_rate=' + str(sampleRate)
print('Request: ' + request)
process(request, audioFile)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。