-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathlive-rec-test.py
239 lines (178 loc) · 7.37 KB
/
live-rec-test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
## this file is created to test WER/LER against live audio
## it should be run on a desktop/laptop using a microphone to speak
## either you can type in the transcript yourself or let audio transcribe for the GroundTruth label
#using silence is too complicated. Just use ctrl+c as works on all systems and can cope with background noise
import argparse
import datetime
import wave
from os import path
import pandas as pd
import pyaudio
import speech_recognition as sr
from model import *
from report import *
from utils import *
from data import combine_all_wavs_and_trans_from_csvs
from generator import *
#####################################################
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "./live/rec.wav")
OUTPUT_DIR = "./live/"
def clean(word):
# token = re.compile("[\w-]+|'m|'t|'ll|'ve|'d|'s|\'")
## LC ALL & strip fullstop, comma and semi-colon which are not required
new = word.lower().replace('.', '')
new = new.replace(',', '')
new = new.replace(';', '')
new = new.replace('"', '')
new = new.replace('!', '')
new = new.replace('?', '')
new = new.replace(':', '')
new = new.replace('-', '')
return new
def startloop(rec_number):
##read in data from csv
# df = pd.read_csv(TRANSCRIPT_SOURCE, sep=',', header=None)
#HEADERS
wav_filename = []
wav_filesize = []
transcript = []
# print("when ready press enter to start recording and then ctrl+c to stop")
# time.sleep(1)
trans = str(raw_input('please type the exact words you will speak (for WER calculation), or press enter to use Google Transcribe for WER calc\n:'))
trans = clean(trans)
if trans == "":
trans = "N/A"
print("Transcript is:", trans)
inputvar = str(raw_input('ready? press enter to begin recording and ctrl+c to stop'))
filename = "rec"
if inputvar == "":
r = record(filename, OUTPUT_DIR, trans)
# inputcheck = str(raw_input('press enter if you are happy, or r to redo.'))
wav_filename.append(r)
wav_filesize.append(os.path.getsize(r))
if trans == "N/A":
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
trans = r.recognize_google(audio)
trans = trans.lower()
transcript.append(trans)
a = {'wav_filename': wav_filename,
'wav_filesize': wav_filesize,
'transcript': transcript
}
df_train = pd.DataFrame(a, columns=['wav_filename', 'wav_filesize', 'transcript'], dtype=int)
df_train.to_csv("./data/live/live.csv", sep=',', header=True, index=False, encoding='ascii')
def record(name, dir, trans):
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK)
#print("Recording to:", dir, name)
print ("\n" * 30) #works on all OS
print("RECORDING, press ctrl+c to stop recording")
print(trans)
frames = []
#for i in range(0, int(RATE / CHUNK * RECORD_MINUTES * 60)):
while 1:
try:
data = stream.read(CHUNK)
frames.append(data)
except KeyboardInterrupt:
print("STOPPING")
break
stream.stop_stream()
stream.close()
p.terminate()
print("Finished recording to:", dir, name)
fileindir = dir+name+".wav"
wf = wave.open(fileindir, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
return fileindir
if __name__ == '__main__':
parser = argparse.ArgumentParser()
#parser.add_argument('--loadcheckpointpath', type=str, default='./checkpoints/trimmed/',
#parser.add_argument('--loadcheckpointpath', type=str, default='./checkpoints/epoch/LER-WER-best-DS3_2017-09-02_13-40',
parser.add_argument('--loadcheckpointpath', type=str, default='../checkpoints/epoch/LER-WER-best-DS3_2017-09-03_12-12',
help='If value set, load the checkpoint json '
'weights assumed as same name '
' e.g. --loadcheckpointpath ./checkpoints/'
'TRIMMED_ds_ctc_model ')
parser.add_argument('--model_arch', type=int, default=3,
help='choose between model_arch versions (when training not loading) '
'--model_arch=1 uses DS1 fully connected layers with simplernn'
'--model_arch=2 uses DS2 fully connected with GRU'
'--model_arch=3 is custom model')
parser.add_argument('--name', type=str, default='',
help='name of run')
args = parser.parse_args()
runtime = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')
if args.name == "":
args.name = "DS" + str(args.model_arch) + "_" + runtime
# check any special data model requirments e.g. a spectrogram
print(args)
if(args.model_arch == 1):
model_input_type = "mfcc"
elif(args.model_arch == 2 or args.model_arch == 5):
print("Spectrogram required")
# spectrogram = True
model_input_type = "spectrogram"
else:
model_input_type = "mfcc"
#1. load model
if args.loadcheckpointpath:
# load existing
print("Loading model")
cp = args.loadcheckpointpath
assert(os.path.isdir(cp))
trimmed = False
if trimmed:
model_path = os.path.join(cp, "TRIMMED_ds_model")
else:
model_path = os.path.join(cp, "model")
# assert(os.path.isfile(model_path))
model = load_model_checkpoint(model_path)
opt = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
print("Model loaded")
else:
# new model
raise("You need to load an existing trained model")
model.compile(optimizer=opt, loss=ctc)
try:
y_pred = model.get_layer('ctc').input[0]
except Exception as e:
print("error", e)
print("couldn't find ctc layer, possibly a trimmed layer, trying other name")
y_pred = model.get_layer('out').output
input_data = model.get_layer('the_input').input
K.set_learning_phase(0)
#2. record data and put it in live folder LOOP
while 1:
startloop(1)
args.test_files = "./live/live.csv"
print("Getting data from arguments")
test_dataprops, df_test = combine_all_wavs_and_trans_from_csvs(args.test_files, sortagrad=False)
## x. init data generators
print("Creating data batch generators")
testdata = BatchGenerator(dataframe=df_test, dataproperties=test_dataprops,
training=False, batch_size=1, model_input_type=model_input_type)
## RUN TEST
report = K.function([input_data, K.learning_phase()], [y_pred])
report_cb = ReportCallback(report, testdata, model, args.name, save=False)
report_cb.force_output = True
report_cb.on_epoch_end(0, logs=None)
tryagain = str(raw_input('do you want to try again? press N to stop \n:'))
if(tryagain == "N" or tryagain == "n"):
break
K.clear_session()