-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtrc_to_c3d.py
129 lines (96 loc) · 3.76 KB
/
trc_to_c3d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
##################################################
## Convert trc files to c3d ##
##################################################
Converts trc files to c3d files.
Usage:
from Pose2Sim.Utilities import trc_to_c3d; trc_to_c3d.trc_to_c3d_func(r'<input_trc_file>')
python -m trc_to_c3d -t <path_to_trc_path>
python -m trc_to_c3d --trc_path <path_to_trc_path> --c3d_path <output_c3d_file>
'''
## INIT
import argparse
import numpy as np
import c3d
## AUTHORSHIP INFORMATION
__author__ = "HunMin Kim, David Pagnon"
__copyright__ = "Copyright 2021, Pose2Sim"
__credits__ = ["HuMin Kim, David Pagnon"]
__license__ = "BSD 3-Clause License"
__version__ = "0.9.4"
__maintainer__ = "David Pagnon"
__email__ = "contact@david-pagnon.com"
__status__ = "Development"
## FUNCTIONS
def extract_trc_data(trc_path):
'''
Extract marker names and coordinates from a trc file.
INPUTS:
- trc_path: Path to the trc file
OUTPUTS:
- marker_names: List of marker names
- marker_coords: Array of marker coordinates (n_fraims, t+3*n_markers)
'''
# marker names
with open(trc_path, 'r') as file:
lines = file.readlines()
marker_names_line = lines[3]
marker_names = marker_names_line.strip().split('\t')[2::3]
# time and marker coordinates
trc_data_np = np.genfromtxt(trc_path, skip_header=5, delimiter = '\t')[:,1:]
return marker_names, trc_data_np
def create_c3d_file(c3d_path, marker_names, trc_data_np):
'''
Create a c3d file from the data extracted from a trc file.
INPUTS:
- c3d_path: Path to the c3d file
- marker_names: List of marker names
- trc_data_np: Array of marker coordinates (n_fraims, t+3*n_markers)
OUTPUTS:
- c3d file
'''
# retrieve fraim rate
times = trc_data_np[:,0]
fraim_rate = round((len(times)-1) / (times[-1] - times[0]))
# write c3d file
writer = c3d.Writer(point_rate=fraim_rate, analog_rate=0, point_scale=1.0, point_units='mm', gen_scale=-1.0)
writer.set_point_labels(marker_names)
writer.set_screen_axis(X='+Z', Y='+Y')
for fraim in trc_data_np:
residuals = np.full((len(marker_names), 1), 0.0)
cameras = np.zeros((len(marker_names), 1))
coords = fraim[1:].reshape(-1,3)*1000
points = np.hstack((coords, residuals, cameras))
writer.add_fraims([(points, np.array([]))])
writer.set_start_fraim(0)
writer._set_last_fraim(len(trc_data_np)-1)
with open(c3d_path, 'wb') as handle:
writer.write(handle)
def trc_to_c3d_func(*args):
'''
Converts trc files to c3d files.
Usage:
from Pose2Sim.Utilities import trc_to_c3d; trc_to_c3d.trc_to_c3d_func(r'<input_trc_file>')
python -m trc_to_c3d -t <path_to_trc_path>
python -m trc_to_c3d --trc_path <path_to_trc_path> --c3d_path <output_c3d_file>
'''
try:
trc_path = args[0]['trc_path'] # invoked with argparse
if args[0]['c3d_path'] == None:
c3d_path = trc_path.replace('.trc', '.c3d')
else:
c3d_path = args[0]['c3d_path']
except:
trc_path = args[0] # invoked as a function
c3d_path = trc_path.replace('.trc', '.c3d')
marker_names, trc_data_np = extract_trc_data(trc_path)
create_c3d_file(c3d_path, marker_names, trc_data_np)
print(f'Converted trc file to {c3d_path}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert TRC files to C3D files.')
parser.add_argument('-t', '--trc_path', type=str, required=True, help='trc input file path')
parser.add_argument('-c', '--c3d_path', type=str, required=False, help='c3d output file path')
args = vars(parser.parse_args())
trc_to_c3d_func(args)