Skip to content

Instantly share code, notes, and snippets.

@ineersa
Created September 5, 2023 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ineersa/bc989414d32b9593c4de1f225df6a541 to your computer and use it in GitHub Desktop.
Save ineersa/bc989414d32b9593c4de1f225df6a541 to your computer and use it in GitHub Desktop.
def extract_features(landmarks):
distances = calculate_pairwise_distances(landmarks)
angles = []
num_points = len(landmarks) // 2
for i in range(num_points):
for j in range(num_points):
for k in range(num_points):
if i != j and i != k and j != k:
angle = calculate_angle([landmarks[2*i], landmarks[2*i + 1]],
[landmarks[2*j], landmarks[2*j + 1]],
[landmarks[2*k], landmarks[2*k + 1]])
angles.append(angle)
keypoints = [(landmarks[i], landmarks[i + 1]) for i in range(0, len(landmarks), 2)]
# Bounding Box
min_x = min([kp[0] for kp in keypoints])
max_x = max([kp[0] for kp in keypoints])
min_y = min([kp[1] for kp in keypoints])
max_y = max([kp[1] for kp in keypoints])
bounding_box_area = (max_x - min_x) * (max_y - min_y)
# Complexity of Pose
hull = ConvexHull(keypoints)
convex_hull_area = hull.volume
# Normalization using torso length
torso_length = np.sqrt((keypoints[MOVENET_LANDMARK_NAMES["NOSE"]][0] -
(keypoints[MOVENET_LANDMARK_NAMES["LEFT_HIP"]][0] +
keypoints[MOVENET_LANDMARK_NAMES["RIGHT_HIP"]][0]) / 2) ** 2 +
(keypoints[MOVENET_LANDMARK_NAMES["NOSE"]][1] -
(keypoints[MOVENET_LANDMARK_NAMES["LEFT_HIP"]][1] +
keypoints[MOVENET_LANDMARK_NAMES["RIGHT_HIP"]][1]) / 2) ** 2)
normalized_distances = [d / torso_length for d in distances]
symmetry_metrics = compute_symmetry(keypoints)
return distances + angles + [bounding_box_area, convex_hull_area] + normalized_distances + symmetry_metrics
def extract_additional_features(keypoints):
# Convert keypoints to pairs for simplicity
keypoints = [(keypoints[i], keypoints[i + 1]) for i in range(0, len(keypoints), 2)]
# Areas of triangles
triangle_areas = [triangle_area(kp1[0], kp1[1], kp2[0], kp2[1], kp3[0], kp3[1])
for kp1 in keypoints for kp2 in keypoints for kp3 in keypoints]
# Center of Gravity
centroid = compute_centroid(keypoints)
distances_to_centroid = [np.sqrt((kp[0] - centroid[0]) ** 2 + (kp[1] - centroid[1]) ** 2) for kp in keypoints]
# Angles
angles = [angle_between_three_points(kp1, kp2, kp3)
for kp1 in keypoints for kp2 in keypoints for kp3 in keypoints if
kp1 != kp2 and kp2 != kp3 and kp1 != kp3]
return triangle_areas + distances_to_centroid + angles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment