-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict.py
84 lines (69 loc) · 2.77 KB
/
predict.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
import argparse
import os
import shutil
import sys
from label2textgrid import create_text_grid
from lib import utils
from post_process import post_process
def predict(input_path, output_path, model):
tmp_dir = 'tmp/'
tmp_features = 'tmp.features'
tmp_prob = 'tmp.prob'
tmp_prediction = 'tmp.prediction'
tmp_duration = 'tmp.dur'
if not os.path.exists(input_path):
print >> sys.stderr, "wav file does not exits"
return
t_model = model.upper()
if t_model == 'RNN':
model_path = 'results/1_layer_model.net'
print '==> using single RNN layer'
elif t_model == '2RNN':
model_path = 'results/2_layer_model.net'
print '==> using 2 stacked layers of RNN'
elif t_model == 'BIRNN':
model_path = 'results/1_bi_model.net'
print '==> using single bi-directional RNN layer'
elif t_model == '2BIRNN':
model_path = 'results/2_bi_model.net'
print '==> using two stacked layers of bi-directional RNN'
else:
model_path = 'results/1_layer_model.net'
print '==> unknown model, using default model: single layer of RNN'
length = utils.get_wav_file_length(input_path)
prob_file = tmp_dir + tmp_prob
predict_file = tmp_dir + tmp_prediction
dur_file = tmp_dir+tmp_duration
# remove tmo dir if exists
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
os.mkdir(tmp_dir)
print '\n1) Extracting features and classifying ...'
cmd = 'python predict_single_file.py %s %s ' % (
os.path.abspath(os.path.abspath(input_path)), os.path.abspath(tmp_dir) + '/' + tmp_features)
os.chdir("front_end/")
utils.easy_call(cmd)
os.chdir("..")
print '\n2) Model predictions ...'
cmd = 'th classify.lua -folder_path %s -x_filename %s -class_path %s -prob_path %s -model_path %s' % (
os.path.abspath(tmp_dir), tmp_features, os.path.abspath(predict_file), os.path.abspath(prob_file), model_path)
os.chdir("back_end/")
utils.easy_call(cmd)
os.chdir("..")
print '\n3) Extracting duration'
post_process(os.path.abspath(predict_file), dur_file)
print '\n4) Writing TextGrid file to %s ...' % output_path
create_text_grid(dur_file, output_path, length, float(0.0))
# remove leftovers
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
if __name__ == "__main__":
# -------------MENU-------------- #
# command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("input_path", help="The path to the wav file")
parser.add_argument("output_path", help="The path to save new text-grid file")
parser.add_argument("model", help="The type pf model: rnn | 2rnn | birnn")
args = parser.parse_args()
# main function
predict(args.input_path, args.output_path, args.model)