Skip to content

Commit 5ae7eaf

Browse files
committed
read_trc more robust
1 parent aa6779c commit 5ae7eaf

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

Sports2D/process.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -882,28 +882,30 @@ def write_angle_as_list(img, ang, ang_name, person_label_position, ang_label_lin
882882

883883
def read_trc(trc_path):
884884
'''
885-
Read trc file
885+
Read a TRC file and extract its contents.
886886
887887
INPUTS:
888-
- trc_path: path to the trc file
888+
- trc_path (str): The path to the TRC file.
889889
890890
OUTPUTS:
891-
- Q_coords: dataframe of coordinates
892-
- frames_col: series of frames
893-
- time_col: series of time
894-
- markers: list of marker names
895-
- header: list of header lines
891+
- tuple: A tuple containing the Q coordinates, frames column, time column, marker names, and header.
896892
'''
897-
898-
with open(trc_path, 'r') as trc_file:
899-
header = [next(trc_file) for line in range(5)]
900-
markers = header[3].split('\t')[2::3]
901-
902-
trc_df = pd.read_csv(trc_path, sep="\t", skiprows=4)
903-
frames_col, time_col = pd.Series(trc_df.iloc[:,0], name='frames'), pd.Series(trc_df.iloc[:,1], name='time')
904-
Q_coords = trc_df.drop(trc_df.columns[[0, 1]], axis=1)
905893

906-
return Q_coords, frames_col, time_col, markers, header
894+
try:
895+
with open(trc_path, 'r') as trc_file:
896+
header = [next(trc_file) for _ in range(5)]
897+
markers = header[3].split('\t')[2::3]
898+
markers = [m.strip() for m in markers if m.strip()] # remove last \n character
899+
900+
trc_df = pd.read_csv(trc_path, sep="\t", skiprows=4, encoding='utf-8')
901+
frames_col, time_col = trc_df.iloc[:, 0], trc_df.iloc[:, 1]
902+
Q_coords = trc_df.drop(trc_df.columns[[0, 1]], axis=1)
903+
Q_coords = Q_coords.loc[:, ~Q_coords.columns.str.startswith('Unnamed')] # remove unnamed columns
904+
905+
return Q_coords, frames_col, time_col, markers, header
906+
907+
except Exception as e:
908+
raise ValueError(f"Error reading TRC file at {trc_path}: {e}")
907909

908910

909911
def load_pose_file(Q_coords):

0 commit comments

Comments
 (0)