Spaces:
Runtime error
Runtime error
| import math | |
| import numpy as np | |
| import cv2 | |
| from tensorflow.keras import preprocessing | |
| 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(side_colored_mask, front_colored_mask, sideposes, frontposes, real_height_cm, rainbow=None): | |
| """Measure various body sizes based on detected poses.""" | |
| measurements = [] | |
| # Check if poses are available | |
| if not frontposes or len(frontposes) == 0: | |
| return measurements | |
| for pose in frontposes: | |
| keypoints = pose[0] # Directly accessing keypoints | |
| # Extracting position coordinates | |
| try: | |
| left_eye = (keypoints[1].position.x, keypoints[1].position.y) | |
| right_eye = (keypoints[2].position.x, keypoints[2].position.y) | |
| left_shoulder = (keypoints[5].position.x, keypoints[5].position.y) | |
| right_shoulder = (keypoints[6].position.x, keypoints[6].position.y) | |
| left_wrist = (keypoints[9].position.x, keypoints[9].position.y) | |
| left_hip = (keypoints[11].position.x, keypoints[11].position.y) | |
| right_hip = (keypoints[12].position.x, keypoints[12].position.y) | |
| left_ankle = (keypoints[15].position.x, keypoints[15].position.y) | |
| right_ankle = (keypoints[16].position.x, keypoints[16].position.y) | |
| except (IndexError, AttributeError): | |
| continue | |
| # Calculate pixel height | |
| pixel_height = euclidean_distance(left_eye, left_ankle) | |
| # Calculate measurements | |
| shoulder_width_cm = convert_to_real_measurements( | |
| euclidean_distance(left_shoulder, right_shoulder), | |
| pixel_height, real_height_cm | |
| ) | |
| arm_length_cm = convert_to_real_measurements( | |
| euclidean_distance(left_shoulder, left_wrist), | |
| pixel_height, real_height_cm | |
| ) | |
| leg_length_cm = convert_to_real_measurements( | |
| euclidean_distance(left_hip, left_ankle), | |
| pixel_height, real_height_cm | |
| ) | |
| shoulder_to_waist_cm = convert_to_real_measurements( | |
| euclidean_distance(left_shoulder, left_hip), | |
| pixel_height, real_height_cm | |
| ) | |
| # Simplified waist circumference - needs more accurate implementation | |
| waist_width = euclidean_distance(left_hip, right_hip) | |
| waist_circumference_cm = convert_to_real_measurements( | |
| waist_width, pixel_height, real_height_cm | |
| ) * 2 # Rough estimate | |
| measurements.append({ | |
| "height_cm": real_height_cm, | |
| "arm_length_cm": arm_length_cm, | |
| "shoulder_width_cm": shoulder_width_cm, | |
| "shoulder_to_waist_cm": shoulder_to_waist_cm, | |
| "leg_length_cm": leg_length_cm, | |
| "waist_circumference_cm": waist_circumference_cm | |
| }) | |
| return measurements |