-
Notifications
You must be signed in to change notification settings - Fork 4
/
oi_image.py
234 lines (188 loc) · 8.49 KB
/
oi_image.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import json
import os
import pickle
import imageio
import numpy as np
import trimesh
from oikit.common import quat_to_aa, quat_to_rotmat, rotmat_to_aa
from oikit.common import suppress_trimesh_logging
from .utils import load_object, load_object_by_id, persp_project
def decode_seq_cat(seq_cat):
field_list = seq_cat.split("_")
obj_id = field_list[0]
action_id = field_list[1]
if action_id == "0004":
subject_id = tuple(field_list[2:4])
else:
subject_id = (field_list[2],)
return obj_id, action_id, subject_id
class OakInkImage:
def __init__(self, data_split="all", mode_split="default") -> None:
self._name = "OakInkImage"
self._data_split = data_split
self._mode_split = mode_split
assert 'OAKINK_DIR' in os.environ, "environment variable 'OAKINK_DIR' is not set"
self._data_dir = os.environ['OAKINK_DIR']
self.info_list = json.load(open(os.path.join(self._data_dir, "image", "anno", "seq_all.json")))
self.info_str_list = []
for info in self.info_list:
info_str = "__".join([str(x) for x in info])
info_str = info_str.replace("/", "__")
self.info_str_list.append(info_str)
# load obj
suppress_trimesh_logging()
self.obj_mapping = {}
obj_root = os.path.join(self._data_dir, "image", "obj")
all_obj_fn = sorted(os.listdir(obj_root))
for obj_fn in all_obj_fn:
obj_id = os.path.splitext(obj_fn)[0]
obj_model = load_object(obj_root, obj_fn)
self.obj_mapping[obj_id] = obj_model
self.framedata_color_name = [
"north_east_color",
"south_east_color",
"north_west_color",
"south_west_color",
]
self._image_size = (848, 480) # (W, H)
self._hand_side = "right"
# seq status
with open(os.path.join(self._data_dir, "image", "anno", "seq_status.json"), "r") as f:
self.seq_status = json.load(f)
def __len__(self):
return len(self.info_list)
def get_image_path(self, idx):
info = self.info_list[idx]
# compute image path
offset = os.path.join(info[0], f"{self.framedata_color_name[info[3]]}_{info[2]}.png")
image_path = os.path.join(self._data_dir, "image", "stream_release_v2", offset)
return image_path
def get_image(self, idx):
path = self.get_image_path(idx)
image = np.array(imageio.imread(path, pilmode="RGB"), dtype=np.uint8)
return image
def get_cam_intr(self, idx):
cam_path = os.path.join(self._data_dir, "image", "anno", "cam_intr", f"{self.info_str_list[idx]}.pkl")
with open(cam_path, "rb") as f:
cam_intr = pickle.load(f)
return cam_intr
def get_joints_3d(self, idx):
joints_path = os.path.join(self._data_dir, "image", "anno", "hand_j", f"{self.info_str_list[idx]}.pkl")
with open(joints_path, "rb") as f:
joints_3d = pickle.load(f)
return joints_3d
def get_verts_3d(self, idx):
verts_path = os.path.join(self._data_dir, "image", "anno", "hand_v", f"{self.info_str_list[idx]}.pkl")
with open(verts_path, "rb") as f:
verts_3d = pickle.load(f)
return verts_3d
def get_joints_2d(self, idx):
cam_intr = self.get_cam_intr(idx)
joints_3d = self.get_joints_3d(idx)
return persp_project(joints_3d, cam_intr)
def get_verts_2d(self, idx):
cam_intr = self.get_cam_intr(idx)
verts_3d = self.get_verts_3d(idx)
return persp_project(verts_3d, cam_intr)
def get_mano_pose(self, idx):
general_info_path = os.path.join(self._data_dir, "image", "anno", "general_info",
f"{self.info_str_list[idx]}.pkl")
with open(general_info_path, "rb") as f:
general_info = pickle.load(f)
raw_hand_anno = general_info["hand_anno"]
raw_hand_pose = raw_hand_anno["hand_pose"] # quat (16, 4)
_wrist, _remain = raw_hand_pose[0, :], raw_hand_pose[1:, :]
cam_extr = general_info["cam_extr"] # SE3 (4, 4))
extr_R = cam_extr[:3, :3] # (3, 3)
wrist_R = extr_R.matmul(quat_to_rotmat(_wrist)) # (3, 3)
wrist = rotmat_to_aa(wrist_R).unsqueeze(0).numpy() # (1, 3)
remain = quat_to_aa(_remain).numpy() # (15, 3)
hand_pose = np.concatenate([wrist, remain], axis=0) # (16, 3)
return hand_pose.astype(np.float32)
def get_mano_shape(self, idx):
general_info_path = os.path.join(self._data_dir, "image", "anno", "general_info",
f"{self.info_str_list[idx]}.pkl")
with open(general_info_path, "rb") as f:
general_info = pickle.load(f)
raw_hand_anno = general_info["hand_anno"]
hand_shape = raw_hand_anno["hand_shape"].numpy().astype(np.float32)
return hand_shape
def get_obj_idx(self, idx):
info = self.info_list[idx][0]
seq_cat, _ = info.split("/")
obj_id, _, _ = decode_seq_cat(seq_cat)
return obj_id
def get_obj_faces(self, idx):
obj_id = self.get_obj_idx(idx)
return np.asarray(self.obj_mapping[obj_id].faces).astype(np.int32)
def get_obj_transf(self, idx):
obj_transf_path = os.path.join(self._data_dir, "image", "anno", "obj_transf", f"{self.info_str_list[idx]}.pkl")
with open(obj_transf_path, "rb") as f:
obj_transf = pickle.load(f)
return obj_transf.astype(np.float32)
def get_obj_verts_3d(self, idx):
obj_verts = self.get_obj_verts_can(idx)
obj_transf = self.get_obj_transf(idx)
obj_rot = obj_transf[:3, :3]
obj_tsl = obj_transf[:3, 3]
obj_verts_transf = (obj_rot @ obj_verts.transpose(1, 0)).transpose(1, 0) + obj_tsl
return obj_verts_transf
def get_obj_verts_2d(self, idx):
obj_verts_3d = self.get_obj_verts_3d(idx)
cam_intr = self.get_cam_intr(idx)
return persp_project(obj_verts_3d, cam_intr)
def get_obj_verts_can(self, idx):
obj_id = self.get_obj_idx(idx)
obj_verts = np.asarray(self.obj_mapping[obj_id].vertices).astype(np.float32)
return obj_verts
def get_corners_3d(self, idx):
obj_corners = self.get_corners_can(idx)
obj_transf = self.get_obj_transf(idx)
obj_rot = obj_transf[:3, :3]
obj_tsl = obj_transf[:3, 3]
obj_corners_transf = (obj_rot @ obj_corners.transpose(1, 0)).transpose(1, 0) + obj_tsl
return obj_corners_transf
def get_corners_2d(self, idx):
obj_corners = self.get_corners_3d(idx)
cam_intr = self.get_cam_intr(idx)
return persp_project(obj_corners, cam_intr)
def get_corners_can(self, idx):
obj_id = self.get_obj_idx(idx)
obj_mesh = self.obj_mapping[obj_id]
obj_corners = trimesh.bounds.corners(obj_mesh.bounds)
return obj_corners
def get_sample_status(self, idx):
info = self.info_list[idx][0]
status = self.seq_status[info]
return status
class OakInkImageSequence(OakInkImage):
def __init__(self, seq_id, view_id) -> None:
self.framedata_color_name = [
"north_east_color",
"south_east_color",
"north_west_color",
"south_west_color",
]
view_name = self.framedata_color_name[view_id]
self._name = f"OakInkImage_{seq_id}_{view_name}"
assert 'OAKINK_DIR' in os.environ, "environment variable 'OAKINK_DIR' is not set"
self._data_dir = os.environ['OAKINK_DIR']
info_list_all = json.load(open(os.path.join(self._data_dir, "image", "anno", "seq_all.json")))
seq_cat, seq_timestamp = seq_id.split("/")
self.info_list = [info for info in info_list_all if (info[0] == seq_id and info[3] == view_id)]
# deal with two hand cases.
self.info_list.sort(key=lambda x: x[1] * 1000 + x[2])
self.info_str_list = []
for info in self.info_list:
info_str = "__".join([str(x) for x in info])
info_str = info_str.replace("/", "__")
self.info_str_list.append(info_str)
self.obj_id, self.intent_id, self.subject_id = decode_seq_cat(seq_cat)
# load obj
self.obj_mapping = {}
suppress_trimesh_logging()
obj_root = os.path.join(self._data_dir, "image", "obj")
self.obj_model = load_object_by_id(self.obj_id, obj_root)
self.obj_mapping[self.obj_id] = self.obj_model
self._image_size = (848, 480) # (W, H)
self._hand_side = "right"