From 4cacbb8b37b1ad594952803daebca53fb0137803 Mon Sep 17 00:00:00 2001 From: Vidya Koesmahargyo Date: Mon, 30 Nov 2020 09:49:25 -0500 Subject: [PATCH] added math_util --- .../raw_features/util/math_util.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 dbm_lib/dbm_features/raw_features/util/math_util.py diff --git a/dbm_lib/dbm_features/raw_features/util/math_util.py b/dbm_lib/dbm_features/raw_features/util/math_util.py new file mode 100644 index 00000000..283acadc --- /dev/null +++ b/dbm_lib/dbm_features/raw_features/util/math_util.py @@ -0,0 +1,57 @@ +""" +file_name: facial_tremor +project_name: cdx_analysis +created: 2019-03-16 +author: Deshana Desai +""" +import sys, os, glob, cv2 +import pandas as pd +import numpy as np + + +def euclidean_distance(point1, point2): + """ + Compute euclidean distance between points + """ + + return np.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) + + +# def detect_peaks() + + +def expand_landmarks(landmarks): + """ + util method to expand landmark list: + eg: [1,2] -> [['l1_x', 'l1_y'], ['l2_x', 'l2_y']] + """ + return [['l{}_x'.format(l), 'l{}_y'.format(l)] for l in landmarks] + + + +def calc_displacement_vec(df, landmarks, num_frames): + """ + Calculates displacement vector frame by frame + """ + + landmarks = expand_landmarks(landmarks) + + disp_vec = np.zeros((len(landmarks), num_frames)) + prev_point = np.zeros((len(landmarks), 2)) + + # initialize + for j, pair in enumerate(landmarks): + first_row = df.iloc[0] + prev_point[j] = (first_row[pair[0]], first_row[pair[1]]) + + + for i in range(num_frames): + frame_row = df.iloc[i] + for j, pair in enumerate(landmarks): + x, y = pair[0], pair[1] + current = (frame_row[x], frame_row[y]) + deviation = euclidean_distance( current, prev_point[j]) + disp_vec[j][i] = deviation + prev_point[j] = current + + return disp_vec