讯飞音乐识别python实现
一:进入讯飞听写平台,找到语音识别的歌曲识别
二:点击文档,选择demo文件
三:将下载的文档直接粘贴在pycharm上
四:给定的demo是根据url实现音乐识别的,具有局限性,我想实现的是在前端页面传输文件进行识别。
#_*_encoding:utf-8_*_
import requests
import base64
import json
import time
import hashlib
class demo:
def __init__(self):
self.url = "http://webqbh.xfyun.cn/v1/service/v1/qbh"
self.AUDIO_URL = AUDIO_URL
def music_iat(self):
curtime = str(int(time.time()))
print(curtime)
# 使用audio_url传输音频数据时,http request body须为空。
# 直接把音频二进制数据写入到Http Request Body时,不需要设置audio_url参数
param = {
"audio_url":self.AUDIO_URL
}
base64_param = base64.urlsafe_b64encode(json.dumps(param).encode('utf-8'))
tt = str(base64_param, 'utf-8')
m2 = hashlib.md5()
m2.update((apikey+ curtime+ tt).encode('utf-8'))
checksum = m2.hexdigest()
header = {
"X-CurTime": curtime,
"X-Param": base64_param,
"X-Appid": appid,
"X-CheckSum":checksum,
}
res = requests.post(self.url,headers= header)
result = res.content
print(result.decode("utf-8"))
if __name__ == "__main__":
# 在控制台获取appid等信息
appid = ""
apikey = ""
# 音频url地址
AUDIO_URL = "https://xfyun-doc.cn-bj.ufileos.com/1537253485018707/qlzw2.wav"
demo = demo()
demo.music_iat()
需要四个参数,分别是appid,apikey,file(前端传输文件),url(音乐识别api接口地址)
将music_iat下的代码更换为下面展示的
def music_iat(self):
# 获取文件名
filename = self.file.filename
# 文件写入磁盘
self.file.save(filename)
# 将评测文件写入到Http Request 体中
with open(filename, 'rb') as f:
file_content = f.read()
base64_audio = base64.b64encode(file_content)
data = {'audio': base64_audio # 音频数据
}
param = json.dumps({"aue": "raw", # 音频编码 raw(支持pcm、wav格式)
})
curtime = str(int(time.time()))
paramBase64 = str(base64.b64encode(param.encode('utf-8')), 'utf-8')
m2 = hashlib.md5()
m2.update((self.apikey + curtime + paramBase64).encode('utf-8'))
checksum = m2.hexdigest()
header = {
"X-CurTime": curtime,
"X-Param": paramBase64,
"X-Appid": self.appid,
"X-CheckSum": checksum,
}
res = requests.post(self.url, headers=header, data=data)
result = json.loads(res.content)['data']
return result
主函数
from qbh_python3_demo import demo # 导入 speech_eval_api_helper.py中调用讯飞开放平台接口的语音评测模块
from flask import Flask, render_template, request # 导入flask web框架依赖的模块
appid = ""
apikey = ""
url = "http://webqbh.xfyun.cn/v1/service/v1/qbh"
app = Flask(__name__) # 创建程序实例
@app.route('/', methods=['GET', 'POST'])
def speech_evaluation():
if request.method == 'GET':
return render_template('music.html', result={})
file = request.files.get('file') # 3、获取文件
print(file)
if not file:
return render_template('music.html', result={})
seclass = demo(url, appid, apikey,file) # 语音评测接口调用模块
result = seclass.music_iat() # 获取语音评测结果及结果解析
print(len(result))
return render_template('music.html', result=result)
if __name__ == '__main__':
app.run(debug=True)
前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button class="record-btn">开始录音</button>
<form action="/" method="POST" id="upload" enctype="multipart/form-data">
<input type="file" name="file" id="pic" accept=".wav" class="buttons1" required>
<input type="submit" value="开始评测" onclick="uploadPic()" class="buttons2">
</form>
<div><h3>检测结果</h3>
{% for i in range(result['song']|length) %}
<h1>{{result['song'][i]}}</h1>
{% endfor %}
</div>
<div style="width:48%;float:left"><h2>语音评测结果</h2>
<div style="border:1px solid #ddd;min-height: 400px;padding: 8px;">
<table width="600" border="6" cellpadding="10">
<tr>
<th align="center">序号</th>
<th align="center">歌曲</th>
<th align="center">歌手</th>
</tr>
{% for i in range(result|length) %}
<tr>
<td align="center">{{loop.index}}</td>
<td align="center">{{result[i]['song']}}</td>
<td align="center">{{result[i]['singer']}}</td>
</tr>
{% endfor %}
</table>
</div>
<div style="clear:both;"></div>
</div>
</body>
</html>
执行主函数,效果如下