in utilities/dataset.py [0:0]
def __init__(self, p_num, intent, object_name, mano_pose_params=15,
load_mano=True):
"""
load_mano: Flag can be used to prevent loading MANO hand models, which is
time consuming
"""
if (object_name == 'palm_print') or (object_name == 'hands'):
print('This class is not meant to be used with palm_print or hands')
raise ValueError
self.p_num = p_num
self.intent = intent
self.object_name = object_name
self._mano_pose_params = mano_pose_params
p_id = 'full{:d}_{:s}'.format(p_num, intent)
self.data_dir = osp.join(osp.dirname(__file__), '..', 'data', 'contactpose_data', p_id, object_name)
assert(osp.isdir(self.data_dir))
# read grasp data
with open(self.annotation_filename, 'r') as f:
ann = json.load(f)
self._n_frames = len(ann['frames'])
self._valid_cameras = [cn for cn,cv in ann['cameras'].items() if cv['valid']]
self._is_object_pose_optimized = [f['object_pose_optimized'] for
f in ann['frames']]
self._valid_hands = [hand_idx for hand_idx, hand in enumerate(ann['hands'])
if hand['valid']]
im_filenames = {}
for camera_name in self.valid_cameras:
im_dir = osp.join(self.data_dir, 'images_full', camera_name, '{:s}')
im_filenames[camera_name] = [
osp.join(im_dir, 'frame{:03d}.png'.format(i)) for i in range(len(self))]
self._im_filenames = [{k: v for k,v in zip(im_filenames.keys(), vv)} for
vv in zip(*im_filenames.values())]
oX = [] # 3D joints w.r.t. object
all_oTh = []
for hand_idx, hand in enumerate(ann['hands']):
if hand['valid']:
hX = np.asarray(hand['joints']) # hand joints w.r.t. hand root
if hand['moving']:
# object pose w.r.t. hand
oThs = [np.linalg.inv(mutils.pose_matrix(f['hTo'][hand_idx])) for f
in ann['frames']]
all_oTh.append(oThs)
oX.append([mutils.tform_points(oTh, hX) for oTh in oThs])
else:
oX.append([hX for _ in range(len(self))])
all_oTh.append([np.eye(4) for _ in range(len(self))])
else:
oX.append([None for _ in range(len(self))])
all_oTh.append([np.eye(4) for _ in range(len(self))])
self._oX = list(map(tuple, zip(*oX)))
self._oTh = list(map(tuple, zip(*all_oTh)))
# world pose w.r.t. object
oTws = [mutils.pose_matrix(f['oTw']) for f in ann['frames']]
self._cTo = {} # object pose w.r.t. camera
self._K = {} # camera intrinsics
for camera_name in self.valid_cameras:
cam = ann['cameras'][camera_name]
self._K[camera_name] = np.array([[cam['K']['fx'], 0, cam['K']['cx']],
[0, cam['K']['fy'], cam['K']['cy']],
[0, 0, 1]])
# camera pose w.r.t. world
wTc = mutils.pose_matrix(cam['wTc'])
self._cTo[camera_name] = [np.linalg.inv(oTw @ wTc) for oTw in oTws]
# projections
self._ox = [] # joint projections
self._om = [] # marker projections
# 3D marker locations w.r.t. object
oM = np.loadtxt(osp.join(osp.dirname(__file__), '..', 'data',
'object_marker_locations',
'{:s}_final_marker_locations.txt'.
format(object_name)))[:, :3]
for frame_idx in range(len(self)):
this_ox = {}
this_om = {}
for camera_name in self.valid_cameras:
this_om[camera_name] = mutils.project(self.P(camera_name, frame_idx),
oM)
x = []
for hand_idx in range(2):
if hand_idx not in self._valid_hands:
x.append(None)
else:
x.append(mutils.project(self.P(camera_name, frame_idx),
self._oX[frame_idx][hand_idx]))
this_ox[camera_name] = tuple(x)
self._ox.append(this_ox)
self._om.append(this_om)
# check if MANO code and models are present
if mutils.MANO_PRESENT and load_mano:
# load MANO data for the class
if ContactPose._mano_dicts is not None:
return
ContactPose._mano_dicts = []
for hand_name in ('LEFT', 'RIGHT'):
filename = osp.join(osp.dirname(__file__), '..', 'thirdparty',
'mano', 'models',
'MANO_{:s}.pkl'.format(hand_name))
with open(filename, 'rb') as f:
ContactPose._mano_dicts.append(pickle.load(f, encoding='latin1'))
elif load_mano:
print('MANO code was not detected, please follow steps in README.md. '
'mano_meshes() will return (None, None)')