def compare(elbow_input, knee_input, job_result, file_path): #Compare elbow angles and plot elbow_ans = np.load('elbow_2.npy') elbow_input_trim, elbow_ans_trim, elbow_score, elbow_trim_frame = dtw.DTW( elbow_input, elbow_ans) ax1.plot(elbow_input_trim, label='Uploaded') ax1.plot(elbow_ans_trim, label='Suggested') ax1.legend(loc='best') ax1.get_xaxis().set_visible(False) ax1.set_title('Elbow Angle Comparison') #Compare knee angles and plot knee_ans = np.load('knee_2.npy') knee_input_trim, knee_ans_trim, knee_score, knee_trim_frame = dtw.DTW( knee_input, knee_ans) ax2.plot(knee_input_trim, label='Uploaded') ax2.plot(knee_ans_trim, label='Suggested') ax2.legend(loc='best') ax2.get_xaxis().set_visible(False) ax2.set_title('Knee Angle Comparison') bar.get_tk_widget().pack(side="right") # print(elbow_score) # print(knee_score) # print(get_average(elbow_input_trim)) # print(get_average(elbow_ans_trim)) # print(get_average(knee_input_trim)) # print(get_average(knee_ans_trim)) if elbow_score < 1000 and knee_score < 1000: print_text = "Overall, Good Job!" else: print_text = "" if elbow_score > 1000: if get_average(elbow_input_trim) > get_average(elbow_ans_trim): print_text += "Overall, bend your elbows a little more. " else: print_text += "Overall, bend your elbows a little less. " if knee_score > 1000: if get_average(knee_input_trim) > get_average(knee_ans_trim): print_text += "Overall, bend your knees a little more. " else: print_text += "Overall, bend your knees a little less. " info_label.config(text=print_text) timer_display_elbow(0, elbow_trim_frame, elbow_input_trim, elbow_ans_trim, knee_trim_frame, knee_input_trim, knee_ans_trim, job_result, file_path)
def __init__(self, samplingFrequency=8000, framePeriod=25e-3, hopPeriod=10e-3, trainDir="./train_audio/", thresh=1.2): self.samplingFrequency = samplingFrequency self.framePeriod = framePeriod self.hopPeriod = hopPeriod self.trainDir = trainDir self.hopLength = int(samplingFrequency * hopPeriod) self.frameLength = int(samplingFrequency * framePeriod) self.referenceMFCC = [] for file_name in os.listdir(trainDir): if file_name.endswith(".wav"): (fs, data) = wv.read(trainDir + file_name) num_frames = int(data.shape[0] / self.hopLength) - int(np.ceil(self.frameLength / self.hopLength)) MFCC_calculator = mfcc.MFCC() MFCC_MATRIX = np.empty([39, abs(num_frames)]) for k in range(num_frames): MFCC_MATRIX[:, k] = MFCC_calculator.compute_mfcc( data[k * self.hopLength: k * self.hopLength + self.frameLength]) self.referenceMFCC.append(MFCC_MATRIX) DTW_calculator = dtw.DTW() distance_list = [DTW_calculator.compute_distance(np.transpose(matrix), np.transpose(matrix2)) for matrix in self.referenceMFCC for matrix2 in self.referenceMFCC] self.thresh = np.mean(np.array((distance_list))) * thresh
def distance(self, fileName): """ This function is used for calculating the DTW distance between the test utterance and each of the 10 training utterances. :param fileName: Name of test utterance .wav file :type fileName: str :returns: List of DTW distances of test utterance with each training utterance :rtype: list """ if isinstance(fileName, (bytes, bytearray)): data = np.fromstring(fileName) else: (fs, data) = wv.read(fileName) DTW_calculator = dtw.DTW() num_frames = int(data.shape[0] / self.hopLength) - int(np.ceil(self.frameLength / self.hopLength)) if num_frames <= 0: return 10000 MFCC_calculator = mfcc.MFCC() MFCC_MATRIX = abs(np.empty([39, num_frames])) for k in range(num_frames): MFCC_MATRIX[:, k] = MFCC_calculator.compute_mfcc( data[k * self.hopLength: k * self.hopLength + self.frameLength]) distance_list = [DTW_calculator.compute_distance(np.transpose(matrix), np.transpose(MFCC_MATRIX)) for matrix in self.referenceMFCC] return distance_list
def run_dtw(train, valid): dtw_alg = dtw.DTW() dtw_alg.train(train[0], train[1]) dtw_alg.test(valid[0], valid[1])