forked from ljk10/asl_translator_3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
37 lines (30 loc) · 1.22 KB
/
data_utils.py
File metadata and controls
37 lines (30 loc) · 1.22 KB
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
import numpy as np
def normalize_landmarks(landmarks):
"""
Converts raw landmarks to body-relative coordinates.
Input: Flat array or list of (Pose + LH + RH) coordinates.
Output: Normalized flat array.
"""
# 1. Reshape to (N, 3) for easier math
# Structure: 33 Pose + 21 LH + 21 RH = 75 points
# Each point has x, y, z
reshaped = np.array(landmarks).reshape(-1, 3)
# 2. Extract Key Body Points (MediaPipe Pose Indices)
# 11: Left Shoulder, 12: Right Shoulder
pose_start = 0
left_shoulder = reshaped[pose_start + 11]
right_shoulder = reshaped[pose_start + 12]
# 3. Calculate Center (Midpoint of shoulders)
# This becomes our new (0,0,0)
center = (left_shoulder + right_shoulder) / 2
# 4. Calculate Scale (Shoulder width)
# We use this to divide all coordinates so 'distance' doesn't matter
shoulder_width = np.linalg.norm(left_shoulder - right_shoulder)
# Avoid division by zero if shoulders are not found
if shoulder_width < 0.01:
shoulder_width = 1.0
# 5. Normalize
# (Point - Center) / Scale
normalized = (reshaped - center) / shoulder_width
# 6. Flatten back to 1D array
return normalized.flatten()