Update ARCTICHUGGFACE/hand_reader.py
Browse files- ARCTICHUGGFACE/hand_reader.py +130 -18
ARCTICHUGGFACE/hand_reader.py
CHANGED
|
@@ -2,11 +2,10 @@
|
|
| 2 |
ARCTIC Hand Data Reader
|
| 3 |
Only reads hand (MANO) parameters from raw_seqs.
|
| 4 |
No body model forward pass, no object data, no splits needed.
|
| 5 |
-
|
| 6 |
Usage:
|
| 7 |
python scripts_data/hand_reader.py
|
| 8 |
-
python scripts_data/hand_reader.py --mano_p ./
|
| 9 |
-
python scripts_data/hand_reader.py --mano_p ./
|
| 10 |
"""
|
| 11 |
|
| 12 |
import argparse
|
|
@@ -22,6 +21,7 @@ sys.path = ["."] + sys.path
|
|
| 22 |
|
| 23 |
DATA_ROOT = "./data"
|
| 24 |
|
|
|
|
| 25 |
# view 0 = ego cam (head-mounted, moves every frame)
|
| 26 |
# view 1-8 = 8 fixed allocentric cameras
|
| 27 |
VIEW_NAMES = ["ego", "allo_1", "allo_2", "allo_3", "allo_4",
|
|
@@ -32,7 +32,6 @@ def load_hand_seq(mano_p):
|
|
| 32 |
"""
|
| 33 |
Load one sequence. Mirrors construct_loader() in
|
| 34 |
src/arctic/preprocess_dataset.py but keeps only hand fields.
|
| 35 |
-
|
| 36 |
Returns a dict with:
|
| 37 |
right / left -> MANO params (tensors)
|
| 38 |
ego_cam -> per-frame ego camera (world2ego, K, dist8)
|
|
@@ -45,11 +44,20 @@ def load_hand_seq(mano_p):
|
|
| 45 |
|
| 46 |
def _load_hand(side):
|
| 47 |
return {
|
| 48 |
-
|
| 49 |
-
"
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
right = _load_hand("right")
|
|
@@ -61,11 +69,17 @@ def load_hand_seq(mano_p):
|
|
| 61 |
# -- Ego camera (same as preprocess_dataset.py construct_loader) --
|
| 62 |
ego_p = mano_p.replace("mano.npy", "egocam.dist.npy")
|
| 63 |
egocam = np.load(ego_p, allow_pickle=True).item()
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# build homogeneous transform, same as preprocess_dataset.py
|
| 71 |
world2ego = torch.zeros((num_frames, 4, 4))
|
|
@@ -74,8 +88,8 @@ def load_hand_seq(mano_p):
|
|
| 74 |
world2ego[:, 3, 3] = 1.0
|
| 75 |
|
| 76 |
# -- Static allo cameras (same as process_seqs.py statcams) --
|
| 77 |
-
sid =
|
| 78 |
-
misc_p = op.join(DATA_ROOT, "misc.json")
|
| 79 |
with open(misc_p) as f:
|
| 80 |
misc = json.load(f)
|
| 81 |
|
|
@@ -84,7 +98,7 @@ def load_hand_seq(mano_p):
|
|
| 84 |
allo_K = torch.FloatTensor(np.array(sub["intris_mat"])) # (8, 3, 3)
|
| 85 |
image_size = np.array(sub["image_size"]) # (9, 2) [w, h]
|
| 86 |
|
| 87 |
-
seq_name =
|
| 88 |
|
| 89 |
return {
|
| 90 |
"right": right,
|
|
@@ -173,10 +187,104 @@ def print_frame(frame, idx):
|
|
| 173 |
print(f" {view:8s}: {path}")
|
| 174 |
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
def construct_args():
|
| 177 |
parser = argparse.ArgumentParser()
|
|
|
|
| 178 |
parser.add_argument("--mano_p", type=str, default=None)
|
| 179 |
-
|
|
|
|
|
|
|
| 180 |
return parser.parse_args()
|
| 181 |
|
| 182 |
|
|
@@ -197,6 +305,10 @@ def main():
|
|
| 197 |
frame = get_frame(seq_data, args.frame)
|
| 198 |
print_frame(frame, args.frame)
|
| 199 |
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
if __name__ == "__main__":
|
| 202 |
main()
|
|
|
|
|
|
| 2 |
ARCTIC Hand Data Reader
|
| 3 |
Only reads hand (MANO) parameters from raw_seqs.
|
| 4 |
No body model forward pass, no object data, no splits needed.
|
|
|
|
| 5 |
Usage:
|
| 6 |
python scripts_data/hand_reader.py
|
| 7 |
+
python scripts_data/hand_reader.py --mano_p ./data/raw_seqs/s01/box_grab_01.mano.npy
|
| 8 |
+
python scripts_data/hand_reader.py --mano_p ./data/raw_seqs/s01/box_grab_01.mano.npy --frame 10
|
| 9 |
"""
|
| 10 |
|
| 11 |
import argparse
|
|
|
|
| 21 |
|
| 22 |
DATA_ROOT = "./data"
|
| 23 |
|
| 24 |
+
# the view information contained in misc.json
|
| 25 |
# view 0 = ego cam (head-mounted, moves every frame)
|
| 26 |
# view 1-8 = 8 fixed allocentric cameras
|
| 27 |
VIEW_NAMES = ["ego", "allo_1", "allo_2", "allo_3", "allo_4",
|
|
|
|
| 32 |
"""
|
| 33 |
Load one sequence. Mirrors construct_loader() in
|
| 34 |
src/arctic/preprocess_dataset.py but keeps only hand fields.
|
|
|
|
| 35 |
Returns a dict with:
|
| 36 |
right / left -> MANO params (tensors)
|
| 37 |
ego_cam -> per-frame ego camera (world2ego, K, dist8)
|
|
|
|
| 44 |
|
| 45 |
def _load_hand(side):
|
| 46 |
return {
|
| 47 |
+
# (F, 3)
|
| 48 |
+
"rot": torch.FloatTensor(data[side]["rot"]),
|
| 49 |
+
# (F, 45)
|
| 50 |
+
#posture
|
| 51 |
+
"pose": torch.FloatTensor(data[side]["pose"]),
|
| 52 |
+
# (F, 3)
|
| 53 |
+
#location
|
| 54 |
+
"trans": torch.FloatTensor(data[side]["trans"]),
|
| 55 |
+
# (F, 10)
|
| 56 |
+
#mano shape
|
| 57 |
+
"shape": torch.FloatTensor(data[side]["shape"]).repeat(num_frames, 1),
|
| 58 |
+
#an error, lower error means more confident
|
| 59 |
+
# (F,)
|
| 60 |
+
"fitting_err": data[side]["fitting_err"],
|
| 61 |
}
|
| 62 |
|
| 63 |
right = _load_hand("right")
|
|
|
|
| 69 |
# -- Ego camera (same as preprocess_dataset.py construct_loader) --
|
| 70 |
ego_p = mano_p.replace("mano.npy", "egocam.dist.npy")
|
| 71 |
egocam = np.load(ego_p, allow_pickle=True).item()
|
| 72 |
+
#direction of camera under world coordinate
|
| 73 |
+
# (F, 3, 3)
|
| 74 |
+
R_ego = torch.FloatTensor(egocam["R_k_cam_np"])
|
| 75 |
+
#location world coordinate
|
| 76 |
+
# (F, 3, 1)
|
| 77 |
+
T_ego = torch.FloatTensor(egocam["T_k_cam_np"])
|
| 78 |
+
# intrinsics
|
| 79 |
+
# (3, 3)
|
| 80 |
+
K_ego = torch.FloatTensor(egocam["intrinsics"])
|
| 81 |
+
# (8,)
|
| 82 |
+
dist8 = torch.FloatTensor(egocam["dist8"])
|
| 83 |
|
| 84 |
# build homogeneous transform, same as preprocess_dataset.py
|
| 85 |
world2ego = torch.zeros((num_frames, 4, 4))
|
|
|
|
| 88 |
world2ego[:, 3, 3] = 1.0
|
| 89 |
|
| 90 |
# -- Static allo cameras (same as process_seqs.py statcams) --
|
| 91 |
+
sid = op.basename(op.dirname(mano_p))
|
| 92 |
+
misc_p = op.join(DATA_ROOT, "meta", "misc.json")
|
| 93 |
with open(misc_p) as f:
|
| 94 |
misc = json.load(f)
|
| 95 |
|
|
|
|
| 98 |
allo_K = torch.FloatTensor(np.array(sub["intris_mat"])) # (8, 3, 3)
|
| 99 |
image_size = np.array(sub["image_size"]) # (9, 2) [w, h]
|
| 100 |
|
| 101 |
+
seq_name = op.basename(mano_p).replace(".mano.npy", "")
|
| 102 |
|
| 103 |
return {
|
| 104 |
"right": right,
|
|
|
|
| 187 |
print(f" {view:8s}: {path}")
|
| 188 |
|
| 189 |
|
| 190 |
+
def project_point(pt_world, world2cam, K):
|
| 191 |
+
"""Project a 3D world point to 2D image coords via a camera."""
|
| 192 |
+
pt_h = np.array([*pt_world, 1.0], dtype=np.float32) # (4,)
|
| 193 |
+
pt_cam = (world2cam @ pt_h)[:3] # (3,)
|
| 194 |
+
if pt_cam[2] <= 0:
|
| 195 |
+
return None
|
| 196 |
+
x = K[0, 0] * pt_cam[0] / pt_cam[2] + K[0, 2]
|
| 197 |
+
y = K[1, 1] * pt_cam[1] / pt_cam[2] + K[1, 2]
|
| 198 |
+
return (float(x), float(y))
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def visualize_wrists(frame, idx, seq_data):
|
| 202 |
+
"""
|
| 203 |
+
For each allo camera that has a cropped image, overlay the projected
|
| 204 |
+
right (red) and left (blue) wrist positions on the image.
|
| 205 |
+
"""
|
| 206 |
+
import matplotlib.pyplot as plt
|
| 207 |
+
import matplotlib.image as mpimg
|
| 208 |
+
import matplotlib.patches as mpatches
|
| 209 |
+
|
| 210 |
+
if "images" not in frame:
|
| 211 |
+
print("No cropped images found — run unzip first.")
|
| 212 |
+
return
|
| 213 |
+
|
| 214 |
+
world2cam = seq_data["static_cams"]["world2cam"].numpy() # (8, 4, 4)
|
| 215 |
+
K_allo = seq_data["static_cams"]["K"].numpy() # (8, 3, 3)
|
| 216 |
+
|
| 217 |
+
trans_r = frame["right"]["trans"].numpy() # (3,)
|
| 218 |
+
trans_l = frame["left"]["trans"].numpy() # (3,)
|
| 219 |
+
|
| 220 |
+
# collect allo views (view_id 1-8 → cam index 0-7)
|
| 221 |
+
allo_items = [(name, path) for name, path in frame["images"].items()
|
| 222 |
+
if name.startswith("allo")]
|
| 223 |
+
|
| 224 |
+
if not allo_items:
|
| 225 |
+
print("No allo images found for this frame.")
|
| 226 |
+
return
|
| 227 |
+
|
| 228 |
+
ncols = len(allo_items)
|
| 229 |
+
fig, axes = plt.subplots(1, ncols, figsize=(5 * ncols, 5))
|
| 230 |
+
if ncols == 1:
|
| 231 |
+
axes = [axes]
|
| 232 |
+
|
| 233 |
+
for ax, (view_name, img_path) in zip(axes, allo_items):
|
| 234 |
+
cam_idx = int(view_name.split("_")[1]) - 1 # "allo_1" → 0
|
| 235 |
+
W2C = world2cam[cam_idx] # (4, 4)
|
| 236 |
+
K = K_allo[cam_idx] # (3, 3)
|
| 237 |
+
|
| 238 |
+
img = mpimg.imread(img_path)
|
| 239 |
+
crop_h, crop_w = img.shape[:2]
|
| 240 |
+
|
| 241 |
+
# project both wrists to full-image coords
|
| 242 |
+
pt_r = project_point(trans_r, W2C, K)
|
| 243 |
+
pt_l = project_point(trans_l, W2C, K)
|
| 244 |
+
|
| 245 |
+
# estimate crop center as midpoint of the two wrists (heuristic,
|
| 246 |
+
# since we don't have the exact bbox without splits data)
|
| 247 |
+
if pt_r is not None and pt_l is not None:
|
| 248 |
+
crop_cx = (pt_r[0] + pt_l[0]) / 2
|
| 249 |
+
crop_cy = (pt_r[1] + pt_l[1]) / 2
|
| 250 |
+
elif pt_r is not None:
|
| 251 |
+
crop_cx, crop_cy = pt_r
|
| 252 |
+
elif pt_l is not None:
|
| 253 |
+
crop_cx, crop_cy = pt_l
|
| 254 |
+
else:
|
| 255 |
+
ax.imshow(img)
|
| 256 |
+
ax.set_title(f"{view_name} frame {idx}")
|
| 257 |
+
ax.axis("off")
|
| 258 |
+
continue
|
| 259 |
+
|
| 260 |
+
# place image so estimated crop center aligns with wrist midpoint
|
| 261 |
+
extent = [crop_cx - crop_w / 2, crop_cx + crop_w / 2,
|
| 262 |
+
crop_cy + crop_h / 2, crop_cy - crop_h / 2]
|
| 263 |
+
ax.imshow(img, extent=extent, aspect="auto")
|
| 264 |
+
ax.set_title(f"{view_name} frame {idx}")
|
| 265 |
+
ax.axis("off")
|
| 266 |
+
|
| 267 |
+
for pt, color in [(pt_r, "red"), (pt_l, "blue")]:
|
| 268 |
+
if pt is not None:
|
| 269 |
+
ax.plot(*pt, "o", color=color, markersize=10, markeredgewidth=2,
|
| 270 |
+
markeredgecolor="white")
|
| 271 |
+
|
| 272 |
+
r_patch = mpatches.Patch(color="red", label="right wrist")
|
| 273 |
+
l_patch = mpatches.Patch(color="blue", label="left wrist")
|
| 274 |
+
fig.legend(handles=[r_patch, l_patch], loc="lower center", ncol=2, fontsize=12)
|
| 275 |
+
plt.tight_layout()
|
| 276 |
+
plt.savefig(f"wrist_vis_frame{idx}.png", dpi=100)
|
| 277 |
+
plt.show()
|
| 278 |
+
print(f"Saved to wrist_vis_frame{idx}.png")
|
| 279 |
+
|
| 280 |
+
|
| 281 |
def construct_args():
|
| 282 |
parser = argparse.ArgumentParser()
|
| 283 |
+
#mano_p: information of position and shape
|
| 284 |
parser.add_argument("--mano_p", type=str, default=None)
|
| 285 |
+
# for a specific frame
|
| 286 |
+
parser.add_argument("--frame", type=int, default=55)
|
| 287 |
+
parser.add_argument("--vis", action="store_true", default=True, help="visualize wrist projection")
|
| 288 |
return parser.parse_args()
|
| 289 |
|
| 290 |
|
|
|
|
| 305 |
frame = get_frame(seq_data, args.frame)
|
| 306 |
print_frame(frame, args.frame)
|
| 307 |
|
| 308 |
+
if args.vis:
|
| 309 |
+
visualize_wrists(frame, args.frame, seq_data)
|
| 310 |
+
|
| 311 |
|
| 312 |
if __name__ == "__main__":
|
| 313 |
main()
|
| 314 |
+
|