Datasets:

FKZZddd's picture
Upload folder using huggingface_hub
dbf8921 verified
Raw
History Blame Contribute Delete
6.22 kB
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from dataclasses import dataclass
from enum import auto, Enum
from typing import Dict, List, Optional
import numpy as np
from projectaria_tools.core.sophus import SE3 # @manual
LEFT_HAND_INDEX = 0
RIGHT_HAND_INDEX = 1
class Handedness(Enum):
Left = int(LEFT_HAND_INDEX)
Right = int(RIGHT_HAND_INDEX)
class HandType(Enum):
Mano = auto()
Umetrack = auto()
@dataclass
class HandPose:
"""Define a Hand pose as wrist_pose (SE3), and joint_angles."""
handedness: Handedness
wrist_pose: Optional[SE3]
joint_angles: List[float]
def is_left_hand(self) -> bool:
return self.handedness == Handedness.Left
def is_right_hand(self) -> bool:
return self.handedness == Handedness.Right
def handedness_label(self) -> str:
return "left" if self.is_left_hand() else "right"
@dataclass
class HandPose3dCollection:
"""
Class to store the Hand poses for a given timestamp
"""
timestamp_ns: int
poses: Dict[Handedness, HandPose]
TimestampHandPoses3d = Dict[int, HandPose3dCollection]
def _get_hand_pose(handedness: str, hand_poses_json: Dict) -> Optional[SE3]:
if handedness in hand_poses_json.keys():
wrist = hand_poses_json[handedness]["wrist_xform"]
#JSONL文件,可以直接用标签读取,不用像csv文件一样用header.index()读取
quaternion_w = wrist["q_wxyz"][0]
quaternion_xyz = wrist["q_wxyz"][1:4]
translation = wrist["t_xyz"]
hand_pose = SE3.from_quat_and_translation(
float(quaternion_w),
np.array([float(o) for o in quaternion_xyz]),
np.array([float(o) for o in translation]),
)[0]
return hand_pose
return None
def _get_joint_angles(handedness: str, hand_poses_json: Dict) -> Optional[List[float]]:
if handedness in hand_poses_json.keys():
if "pose" in hand_poses_json[handedness].keys():
return hand_poses_json[handedness]["pose"] # MANO pose_pca
elif "joint_angles" in hand_poses_json[handedness].keys():
return hand_poses_json[handedness]["joint_angles"] # UMETRACK joint angles
return None
def parse_hand_poses_from_fileobject(fileobject):
hand_poses_per_timestamp: TimestampHandPoses3d = {}
for line in fileobject:
# Parse the JSON file line
#把json转移成字典
hand_pose_instance = json.loads(line)
timestamp_ns = hand_pose_instance["timestamp_ns"]
hand_poses_json = hand_pose_instance["hand_poses"]
# Read hand pose data
# 在哪里
left_hand_pose = _get_hand_pose(str(LEFT_HAND_INDEX), hand_poses_json)
right_hand_pose = _get_hand_pose(str(RIGHT_HAND_INDEX), hand_poses_json)
# 各关节角度(手势)
left_joint_angles = _get_joint_angles(str(LEFT_HAND_INDEX), hand_poses_json)
right_joint_angles = _get_joint_angles(str(RIGHT_HAND_INDEX), hand_poses_json)
# If hand pose data is available, add it to the dictionary
if (
left_hand_pose is not None or right_hand_pose is not None
) and timestamp_ns not in hand_poses_per_timestamp:
#将hand_poses_per_timestamp[timestamp_ns]放入一个HandPose3dCollection对象,里面包含timestamp_ns=timestamp_ns, poses={}
hand_poses_per_timestamp[timestamp_ns] = HandPose3dCollection(
timestamp_ns=timestamp_ns, poses={}
)
if left_hand_pose is not None:
#调用上面创建的对象中的posees字典,poses是一个dict Dict[Handedness, HandPose]
#Handedness0=左手 Handedness1=右手
hand_poses_per_timestamp[timestamp_ns].poses[Handedness.Left] = HandPose(
Handedness.Left, left_hand_pose, left_joint_angles
)
if right_hand_pose is not None:
hand_poses_per_timestamp[timestamp_ns].poses[Handedness.Right] = HandPose(
Handedness.Right, right_hand_pose, right_joint_angles
)
return hand_poses_per_timestamp
def load_hand_poses(filename: str) -> TimestampHandPoses3d:
"""Load Hand Poses meta data from a JSONL file.
Keyword arguments:
filename -- the jsonl file i.e. sequence_folder + "/hand_pose_trajectory.jsonl"
"""
with open(filename, "r") as fobj:
hand_poses_per_timestamp = parse_hand_poses_from_fileobject(fobj)
return hand_poses_per_timestamp
def load_hand_pose_as_json_lines(filename: str) -> Dict[int, Dict]:
"""
Load Hand Poses as JSON payload (Dict) per timestamp from a json line file
"""
timestamp_jsons = {}
with open(filename, "r") as f:
for line in f:
# Parse the JSON file line
hand_pose_instance = json.loads(line)
timestamp_ns = hand_pose_instance["timestamp_ns"]
hand_poses_json = hand_pose_instance["hand_poses"]
timestamp_jsons[timestamp_ns] = hand_poses_json
return timestamp_jsons
def load_mano_shape_params(filename: str) -> Optional[List[float]]:
betas = None
with open(filename, "rb") as f:
for line in f:
hand_pose_instance = json.loads(line)
for handedness in ["0", "1"]:
if (
handedness in hand_pose_instance["hand_poses"].keys()
and "betas" in hand_pose_instance["hand_poses"][handedness]
):
betas = hand_pose_instance["hand_poses"][handedness]["betas"]
break
if betas is not None:
break
return betas