|
| 1 | +from pathlib import Path |
| 2 | +from typing import List, Tuple, Dict, Iterable |
| 3 | +from collections import defaultdict |
| 4 | +import numpy as np |
| 5 | +from numpy import ma |
| 6 | +from pose_format import Pose |
| 7 | + |
| 8 | + |
| 9 | +def pose_remove_world_landmarks(pose: Pose) -> Pose: |
| 10 | + return pose.remove_components(["POSE_WORLD_LANDMARKS"]) |
| 11 | + |
| 12 | + |
| 13 | +def get_component_names_and_points_dict( |
| 14 | + pose: Pose, |
| 15 | +) -> Tuple[List[str], Dict[str, List[str]]]: |
| 16 | + component_names = [] |
| 17 | + points_dict = defaultdict(list) |
| 18 | + for component in pose.header.components: |
| 19 | + component_names.append(component.name) |
| 20 | + |
| 21 | + for point in component.points: |
| 22 | + points_dict[component.name].append(point) |
| 23 | + |
| 24 | + return component_names, points_dict |
| 25 | + |
| 26 | + |
| 27 | +def get_face_and_hands_from_pose(pose: Pose) -> Pose: |
| 28 | + # based on MediaPipe Holistic format. |
| 29 | + components_to_keep = [ |
| 30 | + "FACE_LANDMARKS", |
| 31 | + "LEFT_HAND_LANDMARKS", |
| 32 | + "RIGHT_HAND_LANDMARKS", |
| 33 | + ] |
| 34 | + return pose.get_components(components_to_keep) |
| 35 | + |
| 36 | + |
| 37 | +def load_pose_file(pose_path: Path) -> Pose: |
| 38 | + pose_path = Path(pose_path).resolve() |
| 39 | + with pose_path.open("rb") as f: |
| 40 | + pose = Pose.read(f.read()) |
| 41 | + return pose |
| 42 | + |
| 43 | + |
| 44 | +def reduce_poses_to_intersection( |
| 45 | + poses: Iterable[Pose], |
| 46 | +) -> List[Pose]: |
| 47 | + poses = list(poses) # get a list, no need to copy |
| 48 | + |
| 49 | + # look at the first pose |
| 50 | + component_names = {c.name for c in poses[0].header.components} |
| 51 | + points = {c.name: set(c.points) for c in poses[0].header.components} |
| 52 | + |
| 53 | + # remove anything that other poses don't have |
| 54 | + for pose in poses[1:]: |
| 55 | + component_names.intersection_update({c.name for c in pose.header.components}) |
| 56 | + for component in pose.header.components: |
| 57 | + points[component.name].intersection_update(set(component.points)) |
| 58 | + |
| 59 | + # change datatypes to match get_components, then update the poses |
| 60 | + points_dict = {} |
| 61 | + for c_name in points.keys(): |
| 62 | + points_dict[c_name] = list(points[c_name]) |
| 63 | + poses = [pose.get_components(list(component_names), points_dict) for pose in poses] |
| 64 | + return poses |
| 65 | + |
| 66 | + |
| 67 | +def zero_pad_shorter_poses(poses: Iterable[Pose]) -> List[Pose]: |
| 68 | + poses = [pose.copy() for pose in poses] |
| 69 | + # arrays = [pose.body.data for pose in poses] |
| 70 | + |
| 71 | + # first dimension is frames. Then People, joint-points, XYZ or XY |
| 72 | + max_frame_count = max(len(pose.body.data) for pose in poses) |
| 73 | + # Pad the shorter array with zeros |
| 74 | + for pose in poses: |
| 75 | + if len(pose.body.data) < max_frame_count: |
| 76 | + desired_shape = list(pose.body.data.shape) |
| 77 | + desired_shape[0] = max_frame_count - len(pose.body.data) |
| 78 | + padding_tensor = ma.zeros(desired_shape) |
| 79 | + padding_tensor_conf = ma.ones(desired_shape[:-1]) |
| 80 | + pose.body.data = ma.concatenate([pose.body.data, padding_tensor], axis=0) |
| 81 | + pose.body.confidence = ma.concatenate( |
| 82 | + [pose.body.confidence, padding_tensor_conf] |
| 83 | + ) |
| 84 | + return poses |
| 85 | + |
| 86 | + |
| 87 | +def pose_hide_low_conf(pose: Pose, confidence_threshold: float = 0.2) -> None: |
| 88 | + mask = pose.body.confidence <= confidence_threshold |
| 89 | + pose.body.confidence[mask] = 0 |
| 90 | + stacked_confidence = np.stack([mask, mask, mask], axis=3) |
| 91 | + masked_data = ma.masked_array(pose.body.data, mask=stacked_confidence) |
| 92 | + pose.body.data = masked_data |
0 commit comments