Datasets:

File size: 6,035 Bytes
dbf8921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.

from abc import abstractmethod
from dataclasses import dataclass
from typing import Any, Dict, List, Optional

import numpy as np
import torch
from data_loaders.loader_hand_poses import (
    Handedness,
    HandPose,
    HandPose3dCollection,
    load_hand_poses,
)
from projectaria_tools.core.sensor_data import TimeDomain, TimeQueryOptions  # @manual

from .pose_utils import lookup_timestamp


@dataclass
class HandPose3dCollectionWithDt:
    pose3d_collection: HandPose3dCollection
    time_delta_ns: int


class HandDataProviderBase:
    def __init__(
        self,
    ) -> None:
        self._hand_poses = None
        self._sorted_timestamp_ns_list: List[int]

    def _init_hand_poses(self, hand_pose_trajectory_filepath: str) -> None:
        #self._hand_poses是一个字典,key是timestamp_ns,value是HandPose3dCollection对象,里面包含timestamp_ns和poses字典
        # 取某一帧
        # frame = hand_poses[1234567890]

        # # 取右手
        # right_hand = frame.poses[Handedness.Right]

        # # 取右手手腕位置
        # wrist_se3 = right_hand.wrist_pose

        # # 取右手关节角度
        # angles = right_hand.joint_angles

        self._hand_poses = load_hand_poses(hand_pose_trajectory_filepath)
        self._sorted_timestamp_ns_list: List[int] = sorted(self._hand_poses.keys())

    @property
    def timestamp_ns_list(self) -> List[int]:
        return self._sorted_timestamp_ns_list

    def get_data_statistics(self) -> Dict[str, Any]:
        """
        Returns the stats of the Hand data
        """
        assert self._hand_poses is not None

        stats = {}
        stats["num_frames"] = len(self._sorted_timestamp_ns_list)
        stats["num_right_hands"] = sum(
            [
                1
                for it in self._hand_poses.values()
                if Handedness.Right in it.poses.keys()
            ]
        )
        stats["num_left_hands"] = sum(
            [
                1
                for it in self._hand_poses.values()
                if Handedness.Left in it.poses.keys()
            ]
        )
        return stats

    def get_pose_at_timestamp(
        self,
        timestamp_ns: int,
        time_query_options: TimeQueryOptions,
        time_domain: TimeDomain,
        acceptable_time_delta: Optional[int] = None,
    ) -> Optional[HandPose3dCollectionWithDt]:
        """
        Return the list of hands available at a given timestamp
        """
        if time_domain is not TimeDomain.TIME_CODE:
            raise ValueError("Value other than TimeDomain.TIME_CODE not yet supported.")

        hand_pose_collection, time_delta_ns = lookup_timestamp(
            time_indexed_dict=self._hand_poses,
            sorted_timestamp_list=self._sorted_timestamp_ns_list,
            query_timestamp=timestamp_ns,
            time_query_options=time_query_options,
        )

        if (
            hand_pose_collection is None
            or time_delta_ns is None
            or (
                acceptable_time_delta is not None
                and abs(time_delta_ns) > acceptable_time_delta
            )
        ):
            return None
        else:
            return HandPose3dCollectionWithDt(
                pose3d_collection=hand_pose_collection, time_delta_ns=time_delta_ns
            )

    @abstractmethod
    def get_hand_mesh_vertices(
        self, hand_wrist_data: HandPose
    ) -> Optional[torch.Tensor]:
        """
        Return the hand mesh corresponding to given HandPose
        """

    @abstractmethod
    def get_hand_mesh_faces_and_normals(
        self, hand_wrist_data: HandPose
    ) -> Optional[List[np.ndarray]]:
        """
        Return the hand mesh faces and normals
        """

    @abstractmethod
    def get_hand_landmarks(self, hand_wrist_data: HandPose) -> Optional[torch.Tensor]:
        """
        Return the hand joint landmarks corresponding to given HandPose
        See how to map the vertices together to represent a Hand as linked lines using LANDMARK_CONNECTIVITY
        """

    @staticmethod
    def normalized(
        vecs: np.ndarray, axis: int = -1, add_const_to_denom: bool = True
    ) -> np.ndarray:
        """
        Normalize a set of vectors.
        Args:
            vecs: np.ndarray of shape (..., V).
            axis: axis along which to normalize.
            add_const_to_denom: if True, add a small constant to the denominator to prevent numerical issues.
        Returns:
            np.ndarray of the same shape as vecs.
        """
        denom = np.linalg.norm(vecs, axis=axis, keepdims=True)
        if add_const_to_denom:
            denom += 1e-5
        return vecs / denom

    @staticmethod
    def get_triangular_mesh_normals(
        vertices: np.ndarray, triangles: np.ndarray
    ) -> np.ndarray:
        """
        Compute the normals of a triangular mesh.
        Args:
            vertices: np.ndarray of shape (..., V, 3).
            triangles: np.ndarray of shape (..., F, 3).
        Returns:
            normals: np.ndarray of shape (..., F, 3).
        """
        norm = np.zeros_like(vertices)
        tris = vertices[triangles]
        n = np.cross(tris[::, 1] - tris[::, 0], tris[::, 2] - tris[::, 0])
        n = HandDataProviderBase.normalized(n)
        norm[triangles[:, 0]] += n
        norm[triangles[:, 1]] += n
        norm[triangles[:, 2]] += n
        return HandDataProviderBase.normalized(norm)