Format the Qualisys sports data preprocessed from Matlab
def parse_args():
parser = argparse.ArgumentParser(
description='Reformat qtmdata so that we can train.'
)
parser.add_argument(
'--data-file',
dest='data_file',
help='qtm text file that has been formated in matlab',
type=str
)
parser.add_argument(
'--output-dir',
dest='output_dir',
help='directory for reformated keypoint data (default: ./)',
default='./',
type=str
)
parser.add_argument(
'--cut-frame',
dest='cut_frame',
help='input what frame to cut the dataframe at.',
type=int,
)
parser.add_argument(
'--camera',
dest='camera',
help='which misqus camera (1, 2, 3) to use in runningpose dataset',
type=int,
choices=range(1, 4)
)
return parser.parse_args()
TODO: Fix 2D converter
def main(args):
"""
Loads the qtm data then removes unwanted keypoints.
Then it infers new keypoints adds them.
"""
# Loads the textfiles
labels_np = np.loadtxt('qtm_labels.txt', dtype = 'str')
data_3D = np.loadtxt(args.data_file, dtype = 'float', delimiter= ',')
# Reformats the data to a dataframe
data_3D = pd.DataFrame(data_3D, index=labels_np).T
# Remove unwanted keypoints
data_3D = data_3D.drop(
columns=[
'HeadL', 'HeadR', 'Chest', 'LThighFrontLow', 'RThighFrontLow',
'LShinFrontHigh', 'RShinFrontHigh', 'LForefoot5', 'RForefoot5',
'LHeelBack', 'RHeelBack', 'LArm', 'RArm','WaistLFront', 'WaistL',
'WaistRFront', 'WaistR', 'LHand2', 'RHand2'
]
)
# Create "new" keypoints by finding the mean between specific keypoints
left_elbow_3D = data_3D.loc[:, ['LElbowOut','LElbowIn']].mean(axis=1)
right_elbow_3D = data_3D.loc[:, ['RElbowOut','RElbowIn']].mean(axis=1)
left_wrist_3D = data_3D.loc[:, ['LWristIn','LWristOut']].mean(axis=1)
right_wrist_3D = data_3D.loc[:, ['RWristOut','RWristIn']].mean(axis=1)
left_knee_3D = data_3D.loc[:, ['LKneeOut','LKneeIn']].mean(axis=1)
right_knee_3D = data_3D.loc[:, ['RKneeOut','RKneeIn']].mean(axis=1)
left_ankle_3D = data_3D.loc[:, ['LAnkleOut','LAnkleIn']].mean(axis=1)
right_ankle_3D = data_3D.loc[:, ['RAnkleOut','RAnkleIn']].mean(axis=1)
# Remove the keypoints that was taken as a mean
data_3D = data_3D.drop(
columns=[
'LElbowOut','LElbowIn', 'RElbowOut','RElbowIn',
'LWristIn','LWristOut', 'RWristIn','RWristOut',
'LKneeIn', 'LKneeOut','RKneeIn', 'RKneeOut',
'LAnkleOut','LAnkleIn','RAnkleOut','RAnkleIn'
]
)
# Adds the new keypoint data to the dataframe
data_3D['LElbow'] = left_elbow_3D
data_3D['RElbow'] = right_elbow_3D
data_3D['LWrist'] = left_wrist_3D
data_3D['RWrist'] = right_wrist_3D
data_3D['LKnee'] = left_knee_3D
data_3D['RKnee'] = right_knee_3D
data_3D['LAnkle'] = left_ankle_3D
data_3D['RAnkle'] = right_ankle_3D
# Remove every other frame, our videodata Miqus is 85hz and the data is 170hz
# OBS! (3 rows corresponds to 1 frame.)
remove = True
for i in range(0, data_3D.shape[0]):
if i % 3 == 0:
remove = not(remove)
if remove:
data_3D = data_3D.drop(i)
data_3D = data_3D.reset_index(drop=True)
# Convert 3D world to 2D camera coordinates
# data_2D = convert_to_2D(data_3D, args.camera-1) # args.camera-1 for cameras (0,1,2) instead of (1,2,3)
# Creates output names that depends on the name of the data file
data_file_name = os.path.basename(
os.path.normpath(args.data_file)).rsplit(".")[0]
# out_2D = os.path.join(
# args.output_dir, data_file_name + '_2D_keypoints.csv')
out_3D = os.path.join(
args.output_dir, data_file_name + '_3D_keypoints.csv')
# Save the keypoint data as csv files
# TODO: Add reformat to 2D data i.e 3DWorld -> 3DCamera -> 2D (projection)
# pd.DataFrame.to_csv(data_2D, path_or_buf=out_2D)
# TODO: Check if it is better to save this as npz instead.
pd.DataFrame.to_csv(data_3D, path_or_buf=out_3D)
# pd.DataFrame.to_csv(data_2D, path_or_buf=out_2D)