slr-easz / landmarks.py
Niharmahesh's picture
Create landmarks.py
e5b4dc5 verified
raw
history blame
1.09 kB
import numpy as np
from numpy.linalg import norm
def normalize_landmarks(landmarks):
"""
Normalize the hand landmarks.
Parameters:
landmarks (numpy.ndarray): Array of hand landmarks.
Returns:
numpy.ndarray: Normalized landmarks.
"""
center = np.mean(landmarks, axis=0)
landmarks_centered = landmarks - center
std_dev = np.std(landmarks_centered, axis=0)
landmarks_normalized = landmarks_centered / std_dev
return np.nan_to_num(landmarks_normalized)
def calculate_angles(landmarks):
"""
Calculate angles between hand landmarks.
Parameters:
landmarks (numpy.ndarray): Array of hand landmarks.
Returns:
list: List of calculated angles.
"""
angles = []
for i in range(20):
for j in range(i + 1, 21):
vector = landmarks[j] - landmarks[i]
angle_x = np.arccos(np.clip(vector[0] / norm(vector), -1.0, 1.0))
angle_y = np.arccos(np.clip(vector[1] / norm(vector), -1.0, 1.0))
angles.extend([angle_x, angle_y])
return angles