-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict.py
95 lines (81 loc) · 2.99 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
85
86
87
88
89
90
91
92
93
94
95
import os
import sys
import shutil
import argparse
from subprocess import call
import front_end.extract_features as fe
import utils.run_back_end as model
import utils.label2textgrid as l2t
__author__ = 'yossiadi'
# run system commands
def easy_call(command):
try:
call(command, shell=True)
except Exception as exception:
print "Error: could not execute the following"
print ">>", command
print type(exception) # the exception instance
print exception.args # arguments stored in .args
exit(-1)
def main(wav_file_name, output_text_grid_file):
try:
# convert the wav file to 16khz sample rate
print "Converting the wav file to 16khz sample rate"
new_file_wav_file = wav_file_name.replace(".wav", "_16.wav")
cmd = "utils/sbin/sox %s -r 16000 %s" % (wav_file_name, new_file_wav_file)
easy_call(cmd)
back_up = wav_file_name
wav_file_name = new_file_wav_file
# consts
tmp_dir = "tmp_data/"
tmp_data_file = "tmp.data"
tmp_labels_file = "tmp.labels"
# clean temporary files
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
# validation
if not os.path.exists(wav_file_name):
print >>sys.stderr, "wav file does not exits"
return
if not os.path.exists(tmp_dir):
os.mkdir(tmp_dir)
data_filename = os.path.abspath(tmp_dir+tmp_data_file)
labels_filename = os.path.abspath(tmp_dir+tmp_labels_file)
abs_wav_filename = os.path.abspath(wav_file_name)
abs_text_grid_path = os.path.abspath(output_text_grid_file)
# extract the features - the front end part
os.chdir("front_end/")
fe.main(abs_wav_filename, data_filename)
os.chdir("../")
os.chdir("utils/")
# predict the vowel onset and offset
model.main(data_filename, labels_filename)
# convert the predictions into text grid file
l2t.main(labels_filename, abs_wav_filename, abs_text_grid_path)
os.chdir("../")
except Exception as e:
print(e.message)
# remove leftovers
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
if os.path.exists(wav_file_name):
os.remove(wav_file_name)
return False
finally:
# remove leftovers
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
if os.path.exists(wav_file_name):
os.remove(wav_file_name)
return True
if __name__ == "__main__":
# the first argument is the wav file path
# the second argument is the TextGrid path
# -------------MENU-------------- #
# command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("wav_file_name", help="The wav file")
parser.add_argument("output_text_grid_file", help="The output text grid file")
args = parser.parse_args()
# main function
main(args.wav_file_name, args.output_text_grid_file)