Skip to content

Instantly share code, notes, and snippets.

@rejuvyesh
Created December 18, 2015 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rejuvyesh/e0e2616526580ee9b901 to your computer and use it in GitHub Desktop.
Save rejuvyesh/e0e2616526580ee9b901 to your computer and use it in GitHub Desktop.
CMU MoCap Handling
#!/usr/bin/env python
#
# File: amc.py
#
# Created: Friday, December 18 2015 by rejuvyesh <mail@rejuvyesh.com>
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
#
from __future__ import print_function
import numpy as np
class Frame(object):
""" Each frame from amc file
"""
def __init__(self, frame_num):
""" Constructor
Arguments:
- `frame_num`:
"""
self.frame_num = frame_num
self.frame_dict = {}
def add_joint(self, name, values):
"""
Arguments:
- `name`:
- `values`:
"""
self.frame_dict[name] = values
class AMC(object):
""" Define `amc` file structure and functions to operate
"""
frames = []
header = []
def __init__(self, filename=None):
"""
Arguments:
- `filename`:
"""
if filename:
self._load_amc(filename)
def _read_header(self, content):
for idx, line in enumerate(content):
if('DEGREES' in line):
self.header.append(content[0:idx+1])
return content[idx+1:]
def _load_amc(self, filename):
""" Load `amc` file
Arguments:
- `filename`:
"""
with open(filename, 'r') as fid:
content = fid.readlines()
headerless_content = self._read_header(content)
for line in headerless_content:
words = line.rstrip().split()
try:
frame_num = int(words[0])
self.frames.append(Frame(frame_num))
except ValueError:
self.frames[-1].add_joint(words[0], list(map(float, words[1:])))
def get_joint_values(self, joint_name):
temp_list = []
for frame in self.frames:
if joint_name in frame.frame_dict:
temp_list.append(frame.frame_dict[joint_name])
else:
print('Error {0} does not exist!'.format(joint_name))
return []
return temp_list
def get_all_values_ordered(self, sequence):
res = []
for frame in self.frames:
row = [value for joint in sequence for value in frame.frame_dict[joint]]
res.append(row)
return res
def get_seq_matrix(self):
seq = [ 'root',
'lowerback',
'upperback' ,
'thorax',
'lowerneck',
'upperneck',
'head',
'rclavicle',
'rhumerus',
'rradius',
'rwrist',
'rhand',
'rfingers',
'rthumb',
'lclavicle',
'lhumerus',
'lradius',
'lwrist',
'lhand',
'lfingers',
'lthumb',
'rfemur',
'rtibia',
'rfoot',
'rtoes',
'lfemur',
'ltibia',
'lfoot',
'ltoes' ]
return seq, self.get_all_values_ordered(seq)
if __name__ == '__main__':
amc = AMC('test.amc')
print('HEADER')
print('-'*6)
print(amc.header)
print()
print('Example 1')
print('-'*9)
ltoes = amc.get_joint_values('ltoes')[2:7]
print('ltoes: {}'.format(ltoes))
print()
print('Example 2')
print('-'*9)
mat_seq = amc.get_seq_matrix()
print('Size of matrix: {}x{} -> (frames)x(params)'.format(len(mat_seq), len(mat_seq[0])))
print()
print('Example 3')
print('-'*9)
sequence = ['ltoes', 'root', 'lthumb']
print('Sequence: {}'.format(sequence))
dataset = amc.get_all_values_ordered(sequence)
print('Size of matrix: {}x{} -> (frames)x(params)'.format(len(dataset), len(dataset[0])))
print()
#!OML:ASF F:\VICON\USERDATA\INSTALL\rory3\rory3.ASF
:FULLY-SPECIFIED
:DEGREES
1
root -16.6006 15.8086 11.0482 -171.418 65.5658 -173.49
lowerback 6.77619 -1.158 -0.867523
upperback -1.86368 -1.60993 0.249563
thorax -5.71151 -0.759165 0.933319
lowerneck -2.7139 -14.8645 -19.0975
upperneck 21.674 -20.9541 12.3257
head 10.8072 -10.1708 4.77281
rclavicle 1.36043e-014 5.56597e-015
rhumerus -28.6068 3.63367 -92.9606
rradius 23.075
rwrist 16.2352
rhand -14.3021 15.2809
rfingers 7.12502
rthumb 11.8378 -14.3645
lclavicle 1.36043e-014 5.56597e-015
lhumerus -30.0606 3.84893 87.4297
lradius 22.9301
lwrist -5.52295
lhand -10.9539 14.5012
lfingers 7.12502
lthumb 15.0649 43.9331
rfemur -8.93509 10.7929 23.2793
rtibia 27.2806
rfoot -17.6942 -14.6535
rtoes -3.80339
lfemur -11.1938 -8.63114 -21.0449
ltibia 29.2343
lfoot -20.1877 15.4683
ltoes -0.284049
2
root -16.5997 15.8063 11.0359 -171.544 65.4958 -173.663
lowerback 6.73112 -1.13447 -0.876743
upperback -1.92244 -1.57877 0.253423
thorax -5.74785 -0.742794 0.938726
lowerneck -3.30328 -14.6009 -18.7687
upperneck 22.3625 -20.4436 11.8443
head 10.9832 -9.91053 4.61616
rclavicle -1.01877e-014 3.18055e-015
rhumerus -28.6478 3.31186 -92.6885
rradius 22.8809
rwrist 16.5232
rhand -14.2263 15.5958
rfingers 7.12502
rthumb 11.9109 -14.0454
lclavicle -1.01877e-014 3.18055e-015
lhumerus -29.9606 3.88876 87.4434
lradius 23.1114
lwrist -5.97383
lhand -9.58401 14.9762
lfingers 7.12502
lthumb 16.3842 44.3042
rfemur -8.96352 10.5569 23.144
rtibia 27.3465
rfoot -17.8789 -14.5132
rtoes -3.96242
lfemur -11.2117 -8.84481 -21.1818
ltibia 29.2567
lfoot -20.4081 15.2871
ltoes 1.71096
3
root -16.6018 15.8061 11.0285 -171.582 65.3226 -173.886
lowerback 6.5979 -1.16502 -0.9077
upperback -2.00947 -1.62177 0.240193
thorax -5.76194 -0.762572 0.945274
lowerneck -3.90541 -14.2391 -18.4088
upperneck 23.0508 -19.8069 11.4299
head 11.1507 -9.58572 4.50264
rclavicle -2.86374e-014 -2.38542e-015
rhumerus -28.6254 2.9391 -92.4452
rradius 22.6265
rwrist 16.8994
rhand -14.1572 15.7178
rfingers 7.12502
rthumb 11.9776 -13.9195
lclavicle -2.86374e-014 -2.38542e-015
lhumerus -29.8564 3.70368 87.5285
lradius 23.3348
lwrist -5.76975
lhand -9.36103 14.8895
lfingers 7.12502
lthumb 16.5989 44.1996
rfemur -9.13081 10.3401 22.9939
rtibia 27.3764
rfoot -17.9986 -14.4558
rtoes -4.3813
lfemur -11.4753 -9.03018 -21.2721
ltibia 29.394
lfoot -20.6305 15.5983
ltoes 0.217829
4
root -16.607 15.8056 11.0249 -171.392 65.051 -173.835
lowerback 6.50086 -1.21899 -0.815025
upperback -2.03851 -1.68567 0.222934
thorax -5.73883 -0.797779 0.880543
lowerneck -4.5859 -13.7934 -17.8551
upperneck 23.6958 -19.0513 10.9883
head 11.2775 -9.19303 4.36921
rclavicle 3.00041e-014 -3.57812e-015
rhumerus -28.4792 2.41605 -92.2167
rradius 22.29
rwrist 17.356
rhand -14.2232 15.7607
rfingers 7.12502
rthumb 11.9139 -13.8803
lclavicle 3.00041e-014 -3.57812e-015
lhumerus -29.6712 3.12849 87.7181
lradius 23.5631
lwrist -4.90231
lhand -10.0836 14.6857
lfingers 7.12502
lthumb 15.9032 44.0527
rfemur -9.27614 10.1264 22.9934
rtibia 27.407
rfoot -18.1184 -14.4616
rtoes -4.65918
lfemur -11.7536 -9.17987 -21.2117
ltibia 29.5336
lfoot -20.7579 15.85
ltoes -1.30478
5
root -16.6096 15.8028 11.0177 -170.918 64.809 -173.386
lowerback 6.41313 -1.21272 -0.559673
upperback -2.01303 -1.65275 0.252114
thorax -5.66887 -0.790684 0.761325
lowerneck -5.36532 -13.4346 -17.2125
upperneck 24.4288 -18.3874 10.3905
head 11.4555 -8.84519 4.14007
rclavicle 5.85172e-015 -5.1684e-015
rhumerus -28.2791 1.5778 -91.8209
rradius 21.9054
rwrist 18.0606
rhand -14.5975 15.5927
rfingers 7.12502
rthumb 11.553 -14.0689
lclavicle 5.85172e-015 -5.1684e-015
lhumerus -29.3675 2.47205 87.9987
lradius 23.6733
lwrist -3.82932
lhand -11.1809 14.5189
lfingers 7.12502
lthumb 14.8462 43.9671
rfemur -9.31916 10.0414 23.1868
rtibia 27.4243
rfoot -18.249 -14.5204
rtoes -4.77409
lfemur -11.9292 -9.31057 -20.996
ltibia 29.7027
lfoot -20.7462 15.6398
ltoes -0.714982
6
root -16.6116 15.7975 11.006 -170.774 64.5771 -173.241
lowerback 6.40904 -1.16121 -0.408879
upperback -2.03078 -1.56855 0.291708
thorax -5.68936 -0.753453 0.708704
lowerneck -6.10635 -13.2207 -16.7743
upperneck 25.299 -17.9274 9.83287
head 11.7522 -8.603 3.91719
rclavicle -4.70126e-014 1.19271e-015
rhumerus -28.2216 0.472061 -91.1744
rradius 21.5422
rwrist 19.1219
rhand -15.1832 15.1079
rfingers 7.12502
rthumb 10.9881 -14.5845
lclavicle -4.70126e-014 1.19271e-015
lhumerus -29.1533 1.95526 88.3043
lradius 23.8763
lwrist -3.29376
lhand -11.5722 14.5969
lfingers 7.12502
lthumb 14.4692 44.0725
rfemur -9.27327 9.92775 23.2357
rtibia 27.3907
rfoot -18.4216 -14.5326
rtoes -4.8873
lfemur -12.0338 -9.43729 -20.9444
ltibia 29.9059
lfoot -20.8009 15.32
ltoes 0.231876
7
root -16.6114 15.7943 10.9923 -170.786 64.3749 -173.226
lowerback 6.45183 -1.11468 -0.325134
upperback -2.09066 -1.49436 0.372221
thorax -5.77768 -0.717431 0.73905
lowerneck -6.63324 -12.9923 -16.5799
upperneck 26.0873 -17.4666 9.42298
head 12.0281 -8.37867 3.79648
rclavicle -1.46479e-014 -3.97569e-015
rhumerus -28.3481 -0.0309859 -90.6416
rradius 21.3469
rwrist 19.5807
rhand -15.4398 14.8738
rfingers 7.12502
rthumb 10.7405 -14.8316
lclavicle -1.46479e-014 -3.97569e-015
lhumerus -29.076 1.50167 88.7123
lradius 24.1174
lwrist -2.66148
lhand -11.857 14.7524
lfingers 7.12502
lthumb 14.1948 44.2474
rfemur -9.13324 9.83267 23.2343
rtibia 27.2951
rfoot -18.6203 -14.5506
rtoes -4.88873
lfemur -12.1336 -9.51393 -20.9534
ltibia 30.1579
lfoot -21.0012 15.1798
ltoes 0.197747
8
root -16.6086 15.7929 10.9761 -170.759 64.2288 -173.175
lowerback 6.44415 -1.09034 -0.263745
upperback -2.12602 -1.44987 0.480015
thorax -5.80979 -0.695112 0.812043
lowerneck -7.14586 -12.6644 -16.3218
upperneck 26.72 -16.8491 8.96703
head 12.2102 -8.09046 3.67853
rclavicle 1.3244e-014 -9.54166e-015
rhumerus -28.4955 -0.0670093 -90.2997
rradius 21.306
rwrist 19.6016
rhand -15.1105 15.2073
rfingers 7.12502
rthumb 11.0582 -14.4814
lclavicle 1.3244e-014 -9.54166e-015
lhumerus -28.9376 1.1995 89.0511
lradius 24.2589
lwrist -2.09388
lhand -11.7027 14.1816
lfingers 7.12502
lthumb 14.3435 43.6661
rfemur -8.98033 9.72438 23.2344
rtibia 27.2028
rfoot -18.7617 -14.5748
rtoes -5.0592
lfemur -12.2529 -9.65736 -20.9475
ltibia 30.4233
lfoot -21.2189 15.1055
ltoes 0.0732046
9
root -16.6082 15.789 10.9616 -170.185 64.1412 -172.533
lowerback 6.45946 -1.04294 0.0353777
upperback -2.14934 -1.35116 0.622115
thorax -5.84802 -0.653847 0.781522
lowerneck -7.67721 -12.3079 -15.9951
upperneck 27.2901 -16.2105 8.56528
head 12.3731 -7.78373 3.57285
rclavicle 5.53553e-014 7.95139e-015
rhumerus -28.618 0.021565 -90.1034
rradius 21.3151
rwrist 19.4538
rhand -14.5357 15.7671
rfingers 7.12502
rthumb 11.6125 -13.8911
lclavicle 5.53553e-014 7.95139e-015
lhumerus -28.7898 0.919793 89.2768
lradius 24.4442
lwrist -1.78818
lhand -11.0449 13.2644
lfingers 7.12502
lthumb 14.9772 42.7028
rfemur -8.89895 9.74767 23.5093
rtibia 27.1437
rfoot -18.8603 -14.6161
rtoes -5.09557
lfemur -12.3777 -9.75144 -20.6627
ltibia 30.6618
lfoot -21.5746 15.1429
ltoes -0.398818
10
root -16.6091 15.782 10.9491 -169.65 64.0737 -171.896
lowerback 6.52707 -0.970655 0.305965
upperback -2.15392 -1.22272 0.736776
thorax -5.89628 -0.597495 0.7358
lowerneck -8.14722 -11.9783 -15.7524
upperneck 27.8529 -15.6571 8.35673
head 12.568 -7.50498 3.54068
rclavicle 1.90523e-014 5.96354e-015
rhumerus -28.7192 -0.0263023 -89.8577
rradius 21.2963
rwrist 19.4227
rhand -14.2988 16.0737
rfingers 7.12502
rthumb 11.841 -13.5715
lclavicle 1.90523e-014 5.96354e-015
lhumerus -28.6057 0.779428 89.4361
lradius 24.6006
lwrist -1.55873
lhand -11.0037 13.4577
lfingers 7.12502
lthumb 15.0169 42.8932
rfemur -8.84091 9.85405 23.8001
rtibia 27.1689
rfoot -18.9545 -14.7122
rtoes -5.2525
lfemur -12.4199 -9.65281 -20.3877
ltibia 30.8349
lfoot -21.8696 15.0945
ltoes -0.961666
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment