diff --git a/.gitignore b/.gitignore index dc3cfcc..8b03486 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ data/*.* output/*.* tracking_model.dat tracking_model.dat.gz +temp/*.* diff --git a/extract_features.py b/extract_features.py index 3294130..b9dc120 100644 --- a/extract_features.py +++ b/extract_features.py @@ -4,15 +4,12 @@ import argparse import numpy as np import wave import os -from os import listdir -from os.path import isfile, join import math from scipy.fftpack.realtransforms import dct from scipy.signal import lfilter, hamming from scipy.fftpack import fft, ifft #from scikits.talkbox.linpred import lpc # obsolete from helpers.conch_lpc import lpc -import shutil from helpers.utilities import * epsilon = 0.0000000001 @@ -274,6 +271,7 @@ def create_features(input_wav_filename, feature_filename, begin=None, end=None, arr = [input_wav_filename] arr.extend(build_single_feature_row(X, Atal)) np.savetxt(feature_filename, np.asarray([arr]), delimiter=",", fmt="%s") + os.remove(tmp_wav16_filename) return arr arcep_mat = [] 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)) arcep_mat.append(arr) np.savetxt(feature_filename, np.asarray(arcep_mat), delimiter=",", fmt="%s") + + os.remove(tmp_wav16_filename) + return arcep_mat diff --git a/formants.py b/formants.py index 8a6272c..020ec2c 100644 --- a/formants.py +++ b/formants.py @@ -1,4 +1,3 @@ -from json import load import extract_features as features import argparse 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): - 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) if begin > 0.0 or end > 0.0: print(wav_filename + " interval " + str(begin) + "-" + str(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) else: features.create_features(wav_filename, tmp_features_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): @@ -34,26 +35,27 @@ def predict_from_textgrid(wav_filename, preds_filename, textgrid_filename, textg # extract tier names 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) - # run over all intervals in the tier - for interval in textgrid[tier_index]: - if re.search(r'\S', interval.mark()): - tmp_features_filename = generate_tmp_filename("features") - tmp_preds = generate_tmp_filename("preds") - features.create_features(wav_filename, tmp_features_filename, interval.xmin(), interval.xmax()) - load_estimation_model(tmp_features_filename, tmp_preds) - #easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds) - csv_append_row(tmp_preds, preds_filename) - else: # process first tier - for interval in textgrid[0]: - if re.search(r'\S', interval.mark()): - tmp_features_filename = generate_tmp_filename("features") - tmp_preds = generate_tmp_filename("preds") - features.create_features(wav_filename, tmp_features_filename, interval.xmin(), interval.xmax()) - load_estimation_model(tmp_features_filename, tmp_preds) - #easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds) - csv_append_row(tmp_preds, preds_filename) + textgrid_tier = textgrid[tier_index] + else: # process first tier + textgrid_tier = textgrid[0] + + for interval in textgrid_tier: + if re.search(r'\S', interval.mark()): + tmp_features_filename = generate_tmp_filename("features") + tmp_preds = generate_tmp_filename("preds") + begin = interval.xmin() + end = interval.xmax() + features.create_features(wav_filename, tmp_features_filename, begin, end) + load_estimation_model(tmp_features_filename, tmp_preds, begin, end) + #easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + tmp_preds) + csv_append_row(tmp_preds, preds_filename) + delete_temp_files() + + delete_temp_files() + if __name__ == "__main__": # parse arguments diff --git a/helpers/utilities.py b/helpers/utilities.py index 19eaa47..90adea9 100644 --- a/helpers/utilities.py +++ b/helpers/utilities.py @@ -25,6 +25,8 @@ import wave import tempfile import os +from isort import file + 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): - 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"): @@ -169,3 +171,8 @@ def is_valid_wav(filename): or wav_file.getcomptype() != 'NONE': return False return True + +def delete_temp_files(): + print("Clearing temp files...") + for filename in os.listdir("temp"): + os.remove("temp/" + filename) diff --git a/load_estimation_model.py b/load_estimation_model.py index f4bf11d..5c04657 100644 --- a/load_estimation_model.py +++ b/load_estimation_model.py @@ -26,7 +26,7 @@ class LambdaReduce(LambdaBase): 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: contents = rf.read() contents = contents.split(",") @@ -54,5 +54,7 @@ def load_estimation_model(inputfilename, outputfilename): my_prediction = model.forward(data) with open(outputfilename, "w") as wf: - wf.write("NAME,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,begin,end,F1,F2,F3,F4\n") + 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")