text grid integration; temp file management

This commit is contained in:
Jason
2022-04-03 11:25:18 -07:00
parent 906d649a39
commit ca26e3f058
5 changed files with 42 additions and 29 deletions
+1
View File
@@ -5,3 +5,4 @@ data/*.*
output/*.* output/*.*
tracking_model.dat tracking_model.dat
tracking_model.dat.gz tracking_model.dat.gz
temp/*.*
+4 -3
View File
@@ -4,15 +4,12 @@ import argparse
import numpy as np import numpy as np
import wave import wave
import os import os
from os import listdir
from os.path import isfile, join
import math import math
from scipy.fftpack.realtransforms import dct from scipy.fftpack.realtransforms import dct
from scipy.signal import lfilter, hamming from scipy.signal import lfilter, hamming
from scipy.fftpack import fft, ifft from scipy.fftpack import fft, ifft
#from scikits.talkbox.linpred import lpc # obsolete #from scikits.talkbox.linpred import lpc # obsolete
from helpers.conch_lpc import lpc from helpers.conch_lpc import lpc
import shutil
from helpers.utilities import * from helpers.utilities import *
epsilon = 0.0000000001 epsilon = 0.0000000001
@@ -274,6 +271,7 @@ def create_features(input_wav_filename, feature_filename, begin=None, end=None,
arr = [input_wav_filename] arr = [input_wav_filename]
arr.extend(build_single_feature_row(X, Atal)) arr.extend(build_single_feature_row(X, Atal))
np.savetxt(feature_filename, np.asarray([arr]), delimiter=",", fmt="%s") np.savetxt(feature_filename, np.asarray([arr]), delimiter=",", fmt="%s")
os.remove(tmp_wav16_filename)
return arr return arr
arcep_mat = [] arcep_mat = []
for i in range(len(X)): for i in range(len(X)):
@@ -281,6 +279,9 @@ def create_features(input_wav_filename, feature_filename, begin=None, end=None,
arr.extend(build_single_feature_row(X[i], Atal)) arr.extend(build_single_feature_row(X[i], Atal))
arcep_mat.append(arr) arcep_mat.append(arr)
np.savetxt(feature_filename, np.asarray(arcep_mat), delimiter=",", fmt="%s") np.savetxt(feature_filename, np.asarray(arcep_mat), delimiter=",", fmt="%s")
os.remove(tmp_wav16_filename)
return arcep_mat return arcep_mat
+24 -22
View File
@@ -1,4 +1,3 @@
from json import load
import extract_features as features import extract_features as features
import argparse import argparse
from helpers.textgrid import * from helpers.textgrid import *
@@ -7,17 +6,19 @@ from load_estimation_model import load_estimation_model
def predict_from_times(wav_filename, preds_filename, begin, end): def predict_from_times(wav_filename, preds_filename, begin, end):
tmp_features_filename = tempfile._get_default_tempdir() + "/" + next(tempfile._get_candidate_names()) + ".txt" tmp_features_filename = "temp/" + next(tempfile._get_candidate_names()) + ".txt"
print("Input Array Path: " + tmp_features_filename) print("Input Array Path: " + tmp_features_filename)
if begin > 0.0 or end > 0.0: if begin > 0.0 or end > 0.0:
print(wav_filename + " interval " + str(begin) + "-" + str(end) + ":") print(wav_filename + " interval " + str(begin) + "-" + str(end) + ":")
features.create_features(wav_filename, tmp_features_filename, begin, end) features.create_features(wav_filename, tmp_features_filename, begin, end)
load_estimation_model(tmp_features_filename, preds_filename) load_estimation_model(tmp_features_filename, preds_filename, begin, end)
#easy_call("luajit load_estimation_model.lua " + tmp_features_filename + ' ' + preds_filename) #easy_call("luajit load_estimation_model.lua " + tmp_features_filename + ' ' + preds_filename)
else: else:
features.create_features(wav_filename, tmp_features_filename) features.create_features(wav_filename, tmp_features_filename)
easy_call("luajit load_tracking_model.lua " + tmp_features_filename + ' ' + preds_filename) easy_call("luajit load_tracking_model.lua " + tmp_features_filename + ' ' + preds_filename)
delete_temp_files()
def predict_from_textgrid(wav_filename, preds_filename, textgrid_filename, textgrid_tier): def predict_from_textgrid(wav_filename, preds_filename, textgrid_filename, textgrid_tier):
@@ -34,26 +35,27 @@ def predict_from_textgrid(wav_filename, preds_filename, textgrid_filename, textg
# extract tier names # extract tier names
tier_names = textgrid.tierNames() tier_names = textgrid.tierNames()
if textgrid_tier in tier_names:
if textgrid_tier in tier_names: # run over all intervals in the tier
tier_index = tier_names.index(textgrid_tier) tier_index = tier_names.index(textgrid_tier)
# run over all intervals in the tier textgrid_tier = textgrid[tier_index]
for interval in textgrid[tier_index]: else: # process first tier
if re.search(r'\S', interval.mark()): textgrid_tier = textgrid[0]
tmp_features_filename = generate_tmp_filename("features")
tmp_preds = generate_tmp_filename("preds") for interval in textgrid_tier:
features.create_features(wav_filename, tmp_features_filename, interval.xmin(), interval.xmax()) if re.search(r'\S', interval.mark()):
load_estimation_model(tmp_features_filename, tmp_preds) tmp_features_filename = generate_tmp_filename("features")
#easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds) tmp_preds = generate_tmp_filename("preds")
csv_append_row(tmp_preds, preds_filename) begin = interval.xmin()
else: # process first tier end = interval.xmax()
for interval in textgrid[0]: features.create_features(wav_filename, tmp_features_filename, begin, end)
if re.search(r'\S', interval.mark()): load_estimation_model(tmp_features_filename, tmp_preds, begin, end)
tmp_features_filename = generate_tmp_filename("features") #easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds)
tmp_preds = generate_tmp_filename("preds") csv_append_row(tmp_preds, preds_filename)
features.create_features(wav_filename, tmp_features_filename, interval.xmin(), interval.xmax()) delete_temp_files()
load_estimation_model(tmp_features_filename, tmp_preds)
#easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds) delete_temp_files()
csv_append_row(tmp_preds, preds_filename)
if __name__ == "__main__": if __name__ == "__main__":
# parse arguments # parse arguments
+8 -1
View File
@@ -25,6 +25,8 @@ import wave
import tempfile import tempfile
import os import os
from isort import file
def csv_append_row(tmp_preds, preds_filename, with_headers=True): def csv_append_row(tmp_preds, preds_filename, with_headers=True):
@@ -55,7 +57,7 @@ def csv_append_row(tmp_preds, preds_filename, with_headers=True):
def generate_tmp_filename(extension): def generate_tmp_filename(extension):
return tempfile._get_default_tempdir() + "/" + next(tempfile._get_candidate_names()) + "." + extension return "temp/" + next(tempfile._get_candidate_names()) + "." + extension
def logging_defaults(logging_level="INFO"): def logging_defaults(logging_level="INFO"):
@@ -169,3 +171,8 @@ def is_valid_wav(filename):
or wav_file.getcomptype() != 'NONE': or wav_file.getcomptype() != 'NONE':
return False return False
return True return True
def delete_temp_files():
print("Clearing temp files...")
for filename in os.listdir("temp"):
os.remove("temp/" + filename)
+5 -3
View File
@@ -26,7 +26,7 @@ class LambdaReduce(LambdaBase):
return reduce(self.lambda_func,self.forward_prepare(input)) return reduce(self.lambda_func,self.forward_prepare(input))
def load_estimation_model(inputfilename, outputfilename): def load_estimation_model(inputfilename, outputfilename, begin, end):
with open(inputfilename, "r") as rf: with open(inputfilename, "r") as rf:
contents = rf.read() contents = rf.read()
contents = contents.split(",") contents = contents.split(",")
@@ -54,5 +54,7 @@ def load_estimation_model(inputfilename, outputfilename):
my_prediction = model.forward(data) my_prediction = model.forward(data)
with open(outputfilename, "w") as wf: with open(outputfilename, "w") as wf:
wf.write("NAME,F1,F2,F3,F4\n") wf.write("NAME,begin,end,F1,F2,F3,F4\n")
wf.write(name + "," + str(1000 * float(my_prediction[0][0])) + "," + str(1000 * float(my_prediction[0][1])) + "," + str(1000 * float(my_prediction[0][2])) + "," + str(1000 * float(my_prediction[0][3]))) wf.write(name + "," + str(begin) + "," + str(end) + "," + \
str(1000 * float(my_prediction[0][0])) + "," + str(1000 * float(my_prediction[0][1])) + "," + \
str(1000 * float(my_prediction[0][2])) + "," + str(1000 * float(my_prediction[0][3])) + "\n")