Spaces:
Running
Running
import math | |
def euclidean_distance(point1, point2): | |
return math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2) | |
def convert_to_real_measurements(pixel_measurement, pixel_height, real_height_cm): | |
height_ratio = real_height_cm / pixel_height | |
return pixel_measurement * height_ratio | |
def measure_body_sizes(poses, real_height_cm): | |
"""Measure various body sizes based on detected poses.""" | |
measurements = [] | |
for pose in poses: | |
keypoints = pose[0] # This should directly give us the dictionary | |
# Extract positions directly from keypoints | |
left_shoulder = keypoints[5].position | |
right_shoulder = keypoints[6].position | |
left_hip = keypoints[11].position | |
right_hip = keypoints[12].position | |
left_ankle = keypoints[15].position | |
right_ankle = keypoints[16].position | |
left_wrist = keypoints[9].position | |
right_wrist = keypoints[10].position | |
left_eye = keypoints[1].position | |
right_eye = keypoints[2].position | |
# Calculate pixel height (from the top of the head to the bottom of the ankle) | |
pixel_height = euclidean_distance((left_eye.x, left_eye.y), (left_ankle.x, left_ankle.y)) | |
# Calculate other pixel measurements | |
shoulder_width_pixels = euclidean_distance((left_shoulder.x, left_shoulder.y), (right_shoulder.x, right_shoulder.y)) | |
leg_length_pixels = euclidean_distance((left_hip.x, left_hip.y), (left_ankle.x, left_ankle.y)) | |
arm_length_pixels = euclidean_distance((left_shoulder.x, left_shoulder.y), (left_wrist.x, left_wrist.y)) | |
shoulder_to_waist_pixels = euclidean_distance((left_shoulder.x, left_shoulder.y), (left_hip.x, left_hip.y)) | |
# Convert pixel measurements to real measurements using the height ratio | |
measurements.append({ | |
"shoulder_width_cm": convert_to_real_measurements(shoulder_width_pixels, pixel_height, real_height_cm), | |
"leg_length_cm": convert_to_real_measurements(leg_length_pixels, pixel_height, real_height_cm), | |
"arm_length_cm": convert_to_real_measurements(arm_length_pixels, pixel_height, real_height_cm), | |
"shoulder_to_waist_cm": convert_to_real_measurements(shoulder_to_waist_pixels, pixel_height, real_height_cm) | |
}) | |
return measurements |