1
+ from functools import lru_cache
2
+
1
3
import numpy as np
2
4
from pose_format import Pose
3
- from pose_format .utils .generic import reduce_holistic , correct_wrists , pose_normalization_info
5
+ from pose_format .numpy import NumPyPoseBody
6
+ from pose_format .utils .generic import pose_normalization_info
7
+ from sign_vq .data .normalize import load_pose_header
4
8
5
9
6
10
def normalize_pose_size (pose : Pose ):
@@ -11,17 +15,8 @@ def normalize_pose_size(pose: Pose):
11
15
pose .header .dimensions .height = pose .header .dimensions .width = int (new_width * shift * 2 )
12
16
13
17
14
- def reduce_pose (pose : Pose ):
15
- # Remove legs, simplify face
16
- pose = reduce_holistic (pose )
17
- # Align hand wrists with body wrists
18
- correct_wrists (pose )
19
- # Adjust pose based on shoulder positions
20
- return pose .normalize (pose_normalization_info (pose .header ))
21
-
22
-
23
- def get_pose_apperance (pose : Pose , include_end_frame = False ):
24
- pose = reduce_pose (pose )
18
+ def get_pose_appearance (pose : Pose , include_end_frame = False ):
19
+ pose = pose .normalize (pose_normalization_info (pose .header ))
25
20
26
21
if include_end_frame :
27
22
# Assuming the first and last frames are indicative of the signer's appearance
@@ -47,9 +42,12 @@ def change_appearace(pose: Pose, appearance: np.ndarray):
47
42
new_pose_data [:, :, start :end ] = pose .body .data [:, :, start :end ]
48
43
49
44
# Bring back the wrists
50
- for hand in ['LEFT' , 'RIGHT' ]:
45
+ body_component = next (c for c in pose .header .components if c .name == 'POSE_LANDMARKS' )
46
+ points = [f'{ hand } _{ point } ' for hand in ['LEFT' , 'RIGHT' ] for point in ['WRIST' , 'PINKY' , 'INDEX' , 'THUMB' ]]
47
+ existing_points = [p for p in points if p in body_component .points ]
48
+ for point in existing_points :
51
49
# pylint: disable=protected-access
52
- wrist_index = pose .header ._get_point_index ('POSE_LANDMARKS' , f' { hand } _WRIST' )
50
+ wrist_index = pose .header ._get_point_index ('POSE_LANDMARKS' , point )
53
51
new_pose_data [:, :, wrist_index ] = pose .body .data [:, :, wrist_index ]
54
52
55
53
pose .body .data = new_pose_data
@@ -59,18 +57,36 @@ def change_appearace(pose: Pose, appearance: np.ndarray):
59
57
return pose
60
58
61
59
62
- def remove_appearance ( pose : Pose , include_end_frame = False ):
63
- pose , appearance = get_pose_apperance ( pose , include_end_frame )
60
+ @ lru_cache ( maxsize = 1 )
61
+ def get_mean_appearance ():
64
62
# pylint: disable=import-outside-toplevel
65
63
from sign_vq .data .normalize import load_mean_and_std
66
64
mean , _ = load_mean_and_std ()
67
65
68
- return change_appearace (pose , appearance - mean )
66
+ data = mean .reshape ((1 , 1 , - 1 , 3 )) * 1000
67
+ confidence = np .ones ((1 , 1 , len (mean )))
68
+ body = NumPyPoseBody (fps = 1 , data = data , confidence = confidence )
69
+ pose = Pose (header = load_pose_header (), body = body )
70
+
71
+ return pose
69
72
70
73
71
74
def transfer_appearance (pose : Pose , appearance_pose : Pose , include_end_frame = False ):
72
- pose , appearance = get_pose_apperance (pose , include_end_frame )
73
- _ , new_appearance = get_pose_apperance (appearance_pose , include_end_frame )
75
+ # Making sure the appearance pose has the same components as the pose, in the same order of points
76
+ pose_components = [c .name for c in pose .header .components ]
77
+ pose_components_points = {c .name : c .points for c in pose .header .components }
78
+ appearance_pose = appearance_pose .get_components (pose_components , pose_components_points )
79
+
80
+ assert pose .header .total_points () == appearance_pose .header .total_points (), \
81
+ "Appearance pose missing points"
82
+
83
+ pose , appearance = get_pose_appearance (pose , include_end_frame )
84
+ _ , new_appearance = get_pose_appearance (appearance_pose , include_end_frame )
74
85
75
86
# Switching the pose appearance
76
87
return change_appearace (pose , appearance - new_appearance )
88
+
89
+
90
+ def remove_appearance (pose : Pose , include_end_frame = False ):
91
+ mean_pose = get_mean_appearance ()
92
+ return transfer_appearance (pose , mean_pose , include_end_frame = include_end_frame )
0 commit comments