智能语音识别
实验目标:
调用wenet与百度智能云进行语音识别,并且使用自定义训练集进行训练。
实验任务
调用wenet的解码器,对测试语音进行识别,输出中文语句;
调用百度智能云的api进行语音识别;
尝试构建或下载自定义语音数据集,使用wenet框架进行训练
wenet
配置相关环境
-
安装必要库
pip install wenet pip install wave
-
下载模型文件
chs
模型文件下载链接
https://github.com/wenet-e2e/wenet/releases/download/v2.0.1/chs.tar.gz
代码部分
import wave
import wenetruntime as wenet
test_wav = "test.wav" #选择语音文件
with wave.open(test_wav, 'rb') as fin:
assert fin.getnchannels() == 1
wav = fin.readframes(fin.getnframes())
#模型文件夹
model_dir = "chs"
decoder = wenet.decoder(model_dir=model_dir, lang='chs')
# the wav is 24k, 16bits, and decode every 0.5 seconds
interval = int(0.5 * 24000) * 2
for i in range(0, len(wav), interval):
last = false if i + interval < len(wav) else true
chunk_wav = wav[i:min(i + interval, len(wav))]
ans = decoder.decode(chunk_wav, last)
print(ans)
执行结果
{
"nbest" : [{
"sentence" : "人工智能"
}],
"type" : "final_result"
}
百度api
在百度云创建语音识别应用
安装必要库
pip install sys
pip install json
pip install base64
pip install time
代码部分
注意替换其中的api key、secret key
# coding=utf-8
import sys
import json
import base64
import time
is_py3 = sys.version_info.major == 3
if is_py3:
from urllib.request import urlopen
from urllib.request import request
from urllib.error import urlerror
from urllib.parse import urlencode
timer = time.perf_counter
else:
from urllib.request import urlopen
from urllib.request import request
from urllib.request import urlerror
from urllib import urlencode
if sys.platform == "win32":
timer = time.clock
else:
# on most other platforms the best timer is time.time()
timer = time.time
api_key = '填入你的key'
secret_key = '填入你的key'
# 需要识别的文件
audio_file = './audio/16k.wav' # 只支持 pcm/wav/amr 格式
# 文件格式
format = audio_file[-3:] # 文件后缀只支持 pcm/wav/amr 格式
cuid = '123456python'
# 采样率
rate = 16000 # 固定值
# 普通版
dev_pid = 1537 # 1537 表示识别普通话,使用输入法模型。根据文档填写pid,选择语言及识别模型
asr_url = 'http://vop.baidu.com/server_api'
scope = 'audio_voice_assistant_get' # 有此scope表示有asr能力,没有请在网页里勾选,非常旧的应用可能没有
class demoerror(exception):
pass
""" token start """
token_url = 'http://aip.baidubce.com/oauth/2.0/token'
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': api_key,
'client_secret': secret_key}
post_data = urlencode(params)
if (is_py3):
post_data = post_data.encode( 'utf-8')
req = request(token_url, post_data)
try:
f = urlopen(req)
result_str = f.read()
except urlerror as err:
print('token http response http code : ' + str(err.code))
result_str = err.read()
if (is_py3):
result_str = result_str.decode()
print(result_str)
result = json.loads(result_str)
print(result)
if ('access_token' in result.keys() and 'scope' in result.keys()):
print(scope)
if scope and (not scope in result['scope'].split(' ')): # scope = false 忽略检查
raise demoerror('scope is not correct')
print('success with token: %s expires in seconds: %s' % (result['access_token'], result['expires_in']))
return result['access_token']
else:
raise demoerror('maybe api_key or secret_key not correct: access_token or scope not found in token response')
""" token end """
if __name__ == '__main__':
token = fetch_token()
speech_data = []
with open(audio_file, 'rb') as speech_file:
speech_data = speech_file.read()
length = len(speech_data)
if length == 0:
raise demoerror('file %s length read 0 bytes' % audio_file)
speech = base64.b64encode(speech_data)
if (is_py3):
speech = str(speech, 'utf-8')
params = {'dev_pid': dev_pid,
'format': format,
'rate': rate,
'token': token,
'cuid': cuid,
'channel': 1,
'speech': speech,
'len': length
}
post_data = json.dumps(params, sort_keys=false)
# print post_data
req = request(asr_url, post_data.encode('utf-8'))
req.add_header('content-type', 'application/json')
try:
begin = timer()
f = urlopen(req)
result_str = f.read()
print ("request time cost %f" % (timer() - begin))
except urlerror as err:
print('asr http response http code : ' + str(err.code))
result_str = err.read()
if (is_py3):
result_str = str(result_str, 'utf-8')
print(result_str)
with open("result.txt","w") as of:
of.write(result_str)
运行结果
{"corpus_no":"7299264180552717747","err_msg":"success.","err_no":0,"result":["北京科技馆。"],"sn":"626746583651699492376"}
发表评论