Update app
Browse files
app.py
CHANGED
|
@@ -9,27 +9,27 @@ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
|
| 9 |
# Define the BMI classes
|
| 10 |
bmi_classes = ["underweight", "normal weight", "overweight", "obesity"]
|
| 11 |
|
| 12 |
-
# Define the secondary models for BMI predictions and
|
| 13 |
bmi_ranges = {
|
| 14 |
"underweight": {
|
| 15 |
-
"BMI < 16.0": (0, 16.0,
|
| 16 |
-
"16.0 ≤ BMI ≤ 16.99": (16.0, 16.99, 16.5),
|
| 17 |
-
"17.0 ≤ BMI ≤ 18.49": (17.0, 18.49, 17.75)
|
| 18 |
},
|
| 19 |
"normal weight": {
|
| 20 |
-
"18.5 ≤ BMI ≤ 20.4": (18.5, 20.4, 19.45),
|
| 21 |
-
"20.5 ≤ BMI ≤ 22.4": (20.5, 22.4, 21.45),
|
| 22 |
-
"22.5 ≤ BMI ≤ 24.9": (22.5, 24.9, 23.7)
|
| 23 |
},
|
| 24 |
"overweight": {
|
| 25 |
-
"25.0 ≤ BMI ≤ 26.9": (25.0, 26.9, 25.95),
|
| 26 |
-
"27.0 ≤ BMI ≤ 28.9": (27.0, 28.9, 27.95),
|
| 27 |
-
"29.0 ≤ BMI ≤ 29.9": (29.0, 29.9, 29.45)
|
| 28 |
},
|
| 29 |
"obesity": {
|
| 30 |
-
"30.0 ≤ BMI ≤ 34.9": (30.0, 34.9, 32.5),
|
| 31 |
-
"35.0 ≤ BMI ≤ 39.9": (35.0, 39.9, 37.45),
|
| 32 |
-
"BMI ≥ 40.0": (40.0, 100, 40.0)
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
|
@@ -45,8 +45,8 @@ def predict_bmi(image, height_in_inches):
|
|
| 45 |
max_prob_index = probs.argmax().item()
|
| 46 |
predicted_bmi_class = bmi_classes[max_prob_index]
|
| 47 |
|
| 48 |
-
# Use the
|
| 49 |
-
bmi_prediction =
|
| 50 |
|
| 51 |
# Calculate the weight based on the predicted BMI and user-provided height
|
| 52 |
predicted_weight = calculate_weight(bmi_prediction, height_in_inches)
|
|
@@ -61,12 +61,18 @@ def predict_bmi(image, height_in_inches):
|
|
| 61 |
|
| 62 |
return result
|
| 63 |
|
| 64 |
-
def
|
| 65 |
-
"""Return the
|
| 66 |
category_ranges = bmi_ranges.get(weight_category.lower())
|
| 67 |
if category_ranges:
|
| 68 |
for range_label, (low, high, mid) in category_ranges.items():
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
return None
|
| 71 |
|
| 72 |
def get_bmi_label(bmi_value):
|
|
|
|
| 9 |
# Define the BMI classes
|
| 10 |
bmi_classes = ["underweight", "normal weight", "overweight", "obesity"]
|
| 11 |
|
| 12 |
+
# Define the secondary models for BMI predictions and boundaries
|
| 13 |
bmi_ranges = {
|
| 14 |
"underweight": {
|
| 15 |
+
"BMI < 16.0": (0, 16.0, 16.0), # Upper boundary 16.0
|
| 16 |
+
"16.0 ≤ BMI ≤ 16.99": (16.0, 16.99, 16.5), # Midpoint 16.5
|
| 17 |
+
"17.0 ≤ BMI ≤ 18.49": (17.0, 18.49, 17.75) # Midpoint 17.75
|
| 18 |
},
|
| 19 |
"normal weight": {
|
| 20 |
+
"18.5 ≤ BMI ≤ 20.4": (18.5, 20.4, 19.45), # Midpoint 19.45
|
| 21 |
+
"20.5 ≤ BMI ≤ 22.4": (20.5, 22.4, 21.45), # Midpoint 21.45
|
| 22 |
+
"22.5 ≤ BMI ≤ 24.9": (22.5, 24.9, 23.7) # Midpoint 23.7
|
| 23 |
},
|
| 24 |
"overweight": {
|
| 25 |
+
"25.0 ≤ BMI ≤ 26.9": (25.0, 26.9, 25.95), # Midpoint 25.95
|
| 26 |
+
"27.0 ≤ BMI ≤ 28.9": (27.0, 28.9, 27.95), # Midpoint 27.95
|
| 27 |
+
"29.0 ≤ BMI ≤ 29.9": (29.0, 29.9, 29.45) # Midpoint 29.45
|
| 28 |
},
|
| 29 |
"obesity": {
|
| 30 |
+
"30.0 ≤ BMI ≤ 34.9": (30.0, 34.9, 32.5), # Midpoint 32.5
|
| 31 |
+
"35.0 ≤ BMI ≤ 39.9": (35.0, 39.9, 37.45), # Midpoint 37.45
|
| 32 |
+
"BMI ≥ 40.0": (40.0, 100, 40.0) # Lower boundary 40.0
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
|
|
|
| 45 |
max_prob_index = probs.argmax().item()
|
| 46 |
predicted_bmi_class = bmi_classes[max_prob_index]
|
| 47 |
|
| 48 |
+
# Use the adjusted BMI value for the predicted class
|
| 49 |
+
bmi_prediction = get_adjusted_bmi(predicted_bmi_class)
|
| 50 |
|
| 51 |
# Calculate the weight based on the predicted BMI and user-provided height
|
| 52 |
predicted_weight = calculate_weight(bmi_prediction, height_in_inches)
|
|
|
|
| 61 |
|
| 62 |
return result
|
| 63 |
|
| 64 |
+
def get_adjusted_bmi(weight_category):
|
| 65 |
+
"""Return the appropriate BMI value for the given weight category."""
|
| 66 |
category_ranges = bmi_ranges.get(weight_category.lower())
|
| 67 |
if category_ranges:
|
| 68 |
for range_label, (low, high, mid) in category_ranges.items():
|
| 69 |
+
# Check if it's a "less than" range or "greater than or equal to" range
|
| 70 |
+
if "BMI <" in range_label:
|
| 71 |
+
return high # Use the upper boundary
|
| 72 |
+
elif "BMI ≥" in range_label:
|
| 73 |
+
return low # Use the lower boundary
|
| 74 |
+
else:
|
| 75 |
+
return mid # Use the midpoint
|
| 76 |
return None
|
| 77 |
|
| 78 |
def get_bmi_label(bmi_value):
|