forked from nl8590687/ASRT_SpeechRecognition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
readdata.py
186 lines (165 loc) · 5.37 KB
/
readdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
from general_function.file_wav import *
from python_speech_features import mfcc
from python_speech_features import delta
from python_speech_features import logfbank
import random
#import scipy.io.wavfile as wav
class DataSpeech():
def __init__(self,path):
'''
初始化
参数:
path:数据存放位置根目录
'''
self.datapath = path; # 数据存放位置根目录
if('\\'!=self.datapath[-1]): # 在目录路径末尾增加斜杠
self.datapath=self.datapath+'\\'
self.dic_wavlist = {}
self.dic_symbollist = {}
self.SymbolNum = 0 # 记录拼音符号数量
self.list_symbol = self.GetSymbolList() # 全部汉语拼音符号列表
self.list_wavnum=[] # wav文件标记列表
self.list_symbolnum=[] # symbol标记列表
self.DataNum = 0 # 记录数据量
pass
def LoadDataList(self,type):
'''
加载用于计算的数据列表
参数:
type:选取的数据集类型
train 训练集
dev 开发集
test 测试集
'''
# 设定选取哪一项作为要使用的数据集
if(type=='train'):
filename_wavlist='doc\\doc\\list\\train.wav.lst'
filename_symbollist='doc\\doc\\trans\\train.syllable.txt'
elif(type=='dev'):
filename_wavlist='doc\\doc\\list\\cv.wav.lst'
filename_symbollist='doc\\doc\\trans\\cv.syllable.txt'
elif(type=='test'):
filename_wavlist='doc\\doc\\list\\test.wav.lst'
filename_symbollist='doc\\doc\\trans\\test.syllable.txt'
else:
filename_wavlist='' # 默认留空
filename_symbollist=''
# 读取数据列表,wav文件列表和其对应的符号列表
self.dic_wavlist,self.list_wavnum = get_wav_list(self.datapath+filename_wavlist)
self.dic_symbollist,self.list_symbolnum = get_wav_symbol(self.datapath+filename_symbollist)
self.DataNum = self.GetDataNum()
def GetDataNum(self):
'''
获取数据的数量
当wav数量和symbol数量一致的时候返回正确的值,否则返回-1,代表出错。
'''
if(len(self.dic_wavlist) == len(self.dic_symbollist)):
DataNum = len(self.dic_wavlist)
else:
DataNum = -1
return DataNum
def GetData(self,n_start,n_amount=1):
'''
读取数据,返回神经网络输入值和输出值矩阵(可直接用于神经网络训练的那种)
参数:
n_start:从编号为n_start数据开始选取数据
n_amount:选取的数据数量,默认为1,即一次一个wav文件
返回:
三个包含wav特征矩阵的神经网络输入值,和一个标定的类别矩阵神经网络输出值
'''
# 读取一个文件
filename = self.dic_wavlist[self.list_wavnum[n_start]]
filename=filename.replace('/','\\') # windows系统下需要添加这一行
wavsignal,fs=read_wav_data(self.datapath+filename)
# 获取输入特征
feat_mfcc=mfcc(wavsignal[0],fs)
feat_mfcc_d=delta(feat_mfcc,2)
feat_mfcc_dd=delta(feat_mfcc_d,2)
# 获取输出特征
list_symbol=self.dic_symbollist[self.list_symbolnum[n_start]]
feat_out=[]
#print("数据编号",n_start,filename)
for i in list_symbol:
if(''!=i):
n=self.SymbolToNum(i)
v=self.NumToVector(n)
feat_out.append(v)
#print('feat_out:',feat_out)
# 返回值分别是mfcc特征向量的矩阵及其一阶差分和二阶差分矩阵,以及对应的拼音符号矩阵
data_input = np.column_stack((feat_mfcc, feat_mfcc_d, feat_mfcc_dd))
data_label = np.array(feat_out)
return data_input, data_label
def data_genetator(self, batch_size=32):
'''
数据生成器函数,用于Keras的generator_fit训练
batch_size: 一次产生的数据量
需要再修改。。。
'''
X = np.zeros((batch_size, 1500,39), dtype=np.int16)
y = np.zeros((batch_size, 60, self.SymbolNum), dtype=np.int16)
while True:
#generator = ImageCaptcha(width=width, height=height)
ran_num = random.randint(0,self.DataNum - 1) # 获取一个随机数
for i in range(batch_size):
data_input, data_labels = self.GetData((ran_num + i) % self.DataNum) # 从随机数开始连续向后取一定数量数据
#print(data_input, data_labels)
#print('data_input长度:',len(data_input))
X[i,0:len(data_input)] = data_input
#print('data_labels长度:',len(data_labels))
#print(data_labels)
y[i,0:len(data_labels)] = data_labels
yield X, y
pass
def GetSymbolList(self):
'''
加载拼音符号列表,用于标记符号
返回一个列表list类型变量
'''
txt_obj=open(self.datapath+'dict.txt','r',encoding='UTF-8') # 打开文件并读入
txt_text=txt_obj.read()
txt_lines=txt_text.split('\n') # 文本分割
list_symbol=[] # 初始化符号列表
for i in txt_lines:
if(i!=''):
txt_l=i.split('\t')
list_symbol.append(txt_l[0])
txt_obj.close()
list_symbol.append('_')
self.SymbolNum = len(list_symbol)
return list_symbol
def GetSymbolNum(self):
'''
获取拼音符号数量
'''
return len(self.list_symbol)
def SymbolToNum(self,symbol):
'''
符号转为数字
'''
return self.list_symbol.index(symbol)
def NumToVector(self,num):
'''
数字转为对应的向量
'''
v_tmp=[]
for i in range(0,len(self.list_symbol)):
if(i==num):
v_tmp.append(1)
else:
v_tmp.append(0)
v=np.array(v_tmp)
return v
if(__name__=='__main__'):
#path='E:\\语音数据集'
#l=DataSpeech(path)
#l.LoadDataList('train')
#print(l.GetDataNum())
#print(l.GetData(0))
#aa=l.data_genetator()
#for i in aa:
#a,b=i
#print(a,b)
pass