Spaces:
Runtime error
Runtime error
Update calculations.py
Browse files- calculations.py +36 -57
calculations.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import math
|
| 2 |
import numpy as np
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
import cv2
|
| 5 |
from tensorflow.keras import preprocessing
|
| 6 |
|
|
@@ -11,88 +10,68 @@ def convert_to_real_measurements(pixel_measurement, pixel_height, real_height_cm
|
|
| 11 |
height_ratio = real_height_cm / pixel_height
|
| 12 |
return pixel_measurement * height_ratio
|
| 13 |
|
| 14 |
-
def measure_body_sizes(side_colored_mask, front_colored_mask, sideposes, frontposes, real_height_cm, rainbow):
|
| 15 |
"""Measure various body sizes based on detected poses."""
|
| 16 |
measurements = []
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
for pose in frontposes:
|
| 19 |
-
|
| 20 |
-
keypoints = pose[0] # This should directly give us the dictionary
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
right_ankle = keypoints[16].position
|
| 39 |
-
|
| 40 |
-
# Calculate pixel height (from the top of the head to the bottom of the ankle)
|
| 41 |
-
pixel_height = euclidean_distance((left_eye.x, left_eye.y), (left_ankle.x, left_ankle.y))
|
| 42 |
-
|
| 43 |
|
|
|
|
| 44 |
shoulder_width_cm = convert_to_real_measurements(
|
| 45 |
-
euclidean_distance(
|
| 46 |
pixel_height, real_height_cm
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# arm_length_cm = convert_to_real_measurements(
|
| 50 |
-
# euclidean_distance((right_shoulder.x, right_shoulder.y), (right_elbow.x, right_elbow.y)),
|
| 51 |
-
# pixel_height, real_height_cm
|
| 52 |
-
# ) + convert_to_real_measurements(
|
| 53 |
-
# euclidean_distance((right_elbow.x, right_elbow.y), (right_wrist.x, right_wrist.y)),
|
| 54 |
-
# pixel_height, real_height_cm
|
| 55 |
-
# )
|
| 56 |
-
|
| 57 |
-
# leg_length_cm = convert_to_real_measurements(
|
| 58 |
-
# euclidean_distance((left_hip.x, left_hip.y), (left_knee.x, left_knee.y)),
|
| 59 |
-
# pixel_height, real_height_cm
|
| 60 |
-
# ) + convert_to_real_measurements(
|
| 61 |
-
# euclidean_distance((left_knee.x, left_knee.y), (left_ankle.x, left_ankle.y)),
|
| 62 |
-
# pixel_height, real_height_cm
|
| 63 |
-
# )
|
| 64 |
-
|
| 65 |
arm_length_cm = convert_to_real_measurements(
|
| 66 |
-
euclidean_distance(
|
| 67 |
pixel_height, real_height_cm
|
| 68 |
-
)
|
| 69 |
|
| 70 |
leg_length_cm = convert_to_real_measurements(
|
| 71 |
-
euclidean_distance(
|
| 72 |
pixel_height, real_height_cm
|
| 73 |
)
|
| 74 |
|
| 75 |
shoulder_to_waist_cm = convert_to_real_measurements(
|
| 76 |
-
euclidean_distance(
|
| 77 |
pixel_height, real_height_cm
|
| 78 |
)
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
# Use Ramanujan's approximation for the circumference of an ellipse
|
| 85 |
-
# waist_circumference_px = math.pi * (3*(a + b) - math.sqrt((3*a + b)*(a + 3*b)))
|
| 86 |
-
waist_circumference_cm = 90 #convert_to_real_measurements(waist_circumference_px, pixel_height, real_height_cm)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
# Convert pixel measurements to real measurements using the height ratio
|
| 90 |
measurements.append({
|
| 91 |
"height_cm": real_height_cm,
|
| 92 |
-
"arm_length_cm":
|
| 93 |
"shoulder_width_cm": shoulder_width_cm,
|
| 94 |
"shoulder_to_waist_cm": shoulder_to_waist_cm,
|
| 95 |
"leg_length_cm": leg_length_cm,
|
|
|
|
| 96 |
})
|
| 97 |
|
| 98 |
-
return measurements
|
|
|
|
| 1 |
import math
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import cv2
|
| 4 |
from tensorflow.keras import preprocessing
|
| 5 |
|
|
|
|
| 10 |
height_ratio = real_height_cm / pixel_height
|
| 11 |
return pixel_measurement * height_ratio
|
| 12 |
|
| 13 |
+
def measure_body_sizes(side_colored_mask, front_colored_mask, sideposes, frontposes, real_height_cm, rainbow=None):
|
| 14 |
"""Measure various body sizes based on detected poses."""
|
| 15 |
measurements = []
|
| 16 |
|
| 17 |
+
# Check if poses are available
|
| 18 |
+
if not frontposes or len(frontposes) == 0:
|
| 19 |
+
return measurements
|
| 20 |
+
|
| 21 |
for pose in frontposes:
|
| 22 |
+
keypoints = pose[0] # Directly accessing keypoints
|
|
|
|
| 23 |
|
| 24 |
+
# Extracting position coordinates
|
| 25 |
+
try:
|
| 26 |
+
left_eye = (keypoints[1].position.x, keypoints[1].position.y)
|
| 27 |
+
right_eye = (keypoints[2].position.x, keypoints[2].position.y)
|
| 28 |
+
left_shoulder = (keypoints[5].position.x, keypoints[5].position.y)
|
| 29 |
+
right_shoulder = (keypoints[6].position.x, keypoints[6].position.y)
|
| 30 |
+
left_wrist = (keypoints[9].position.x, keypoints[9].position.y)
|
| 31 |
+
left_hip = (keypoints[11].position.x, keypoints[11].position.y)
|
| 32 |
+
right_hip = (keypoints[12].position.x, keypoints[12].position.y)
|
| 33 |
+
left_ankle = (keypoints[15].position.x, keypoints[15].position.y)
|
| 34 |
+
right_ankle = (keypoints[16].position.x, keypoints[16].position.y)
|
| 35 |
+
except (IndexError, AttributeError):
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
# Calculate pixel height
|
| 39 |
+
pixel_height = euclidean_distance(left_eye, left_ankle)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Calculate measurements
|
| 42 |
shoulder_width_cm = convert_to_real_measurements(
|
| 43 |
+
euclidean_distance(left_shoulder, right_shoulder),
|
| 44 |
pixel_height, real_height_cm
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
arm_length_cm = convert_to_real_measurements(
|
| 48 |
+
euclidean_distance(left_shoulder, left_wrist),
|
| 49 |
pixel_height, real_height_cm
|
| 50 |
+
)
|
| 51 |
|
| 52 |
leg_length_cm = convert_to_real_measurements(
|
| 53 |
+
euclidean_distance(left_hip, left_ankle),
|
| 54 |
pixel_height, real_height_cm
|
| 55 |
)
|
| 56 |
|
| 57 |
shoulder_to_waist_cm = convert_to_real_measurements(
|
| 58 |
+
euclidean_distance(left_shoulder, left_hip),
|
| 59 |
pixel_height, real_height_cm
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Simplified waist circumference - needs more accurate implementation
|
| 63 |
+
waist_width = euclidean_distance(left_hip, right_hip)
|
| 64 |
+
waist_circumference_cm = convert_to_real_measurements(
|
| 65 |
+
waist_width, pixel_height, real_height_cm
|
| 66 |
+
) * 2 # Rough estimate
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
measurements.append({
|
| 69 |
"height_cm": real_height_cm,
|
| 70 |
+
"arm_length_cm": arm_length_cm,
|
| 71 |
"shoulder_width_cm": shoulder_width_cm,
|
| 72 |
"shoulder_to_waist_cm": shoulder_to_waist_cm,
|
| 73 |
"leg_length_cm": leg_length_cm,
|
| 74 |
+
"waist_circumference_cm": waist_circumference_cm
|
| 75 |
})
|
| 76 |
|
| 77 |
+
return measurements
|