Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- .gitattributes +1 -0
- src/bmi_model_gender.keras +3 -0
- src/streamlit_app.py +89 -38
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
src/bmi_model_gender.keras filter=lfs diff=lfs merge=lfs -text
|
src/bmi_model_gender.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dfcdb0ee5d382409c1724689e32aff643eb44aa98972f3c2c7b17e90baa2a276
|
| 3 |
+
size 149871248
|
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,91 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import cv2
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 7 |
+
from tensorflow.keras.applications.vgg19 import preprocess_input
|
| 8 |
+
from mtcnn import MTCNN
|
| 9 |
+
import joblib
|
| 10 |
+
import tensorflow.keras.backend as K
|
| 11 |
+
|
| 12 |
+
# === Custom Metric ===
|
| 13 |
+
def pearson_corr(y_true, y_pred):
|
| 14 |
+
x = y_true - K.mean(y_true)
|
| 15 |
+
y = y_pred - K.mean(y_pred)
|
| 16 |
+
return K.sum(x * y) / (K.sqrt(K.sum(K.square(x))) * K.sqrt(K.sum(K.square(y))) + K.epsilon())
|
| 17 |
+
|
| 18 |
+
# === Download model
|
| 19 |
+
import gdown
|
| 20 |
+
url = "https://drive.google.com/file/d/1XevL2OQH6i6vTRnK7GUc49x4OD6IEFxz/view?usp=sharing" # NOT the share link
|
| 21 |
+
output = "bmi_model_gender.keras"
|
| 22 |
+
gdown.download(url, output, quiet=False)
|
| 23 |
+
|
| 24 |
+
# === Load model and scaler ===
|
| 25 |
+
# model = tf.keras.models.load_model("bmi_model_gender.keras", custom_objects={'pearson_corr': pearson_corr})
|
| 26 |
+
model = tf.keras.models.load_model(
|
| 27 |
+
output,
|
| 28 |
+
custom_objects={'pearson_corr': pearson_corr} # Include if used
|
| 29 |
+
)
|
| 30 |
+
scaler = joblib.load("./Data/label_scaler.pkl")
|
| 31 |
+
|
| 32 |
+
# === Sidebar ===
|
| 33 |
+
st.sidebar.title("Settings")
|
| 34 |
+
gender_option = st.sidebar.radio("Select Gender", ['Male', 'Female'])
|
| 35 |
+
gender_value = 1.0 if gender_option.lower() == 'male' else 0.0
|
| 36 |
+
|
| 37 |
+
# === Main UI ===
|
| 38 |
+
st.title("Face-based BMI Prediction App")
|
| 39 |
+
st.markdown("Upload a facial image to estimate BMI using a deep learning model.")
|
| 40 |
+
|
| 41 |
+
# === Face Detector ===
|
| 42 |
+
detector = MTCNN()
|
| 43 |
+
|
| 44 |
+
def detect_and_align_face(image):
|
| 45 |
+
image_rgb = np.array(image.convert('RGB'))
|
| 46 |
+
results = detector.detect_faces(image_rgb)
|
| 47 |
+
if not results:
|
| 48 |
+
return None, "❌ No face detected."
|
| 49 |
+
|
| 50 |
+
box = results[0]['box']
|
| 51 |
+
keypoints = results[0]['keypoints']
|
| 52 |
+
x, y, w, h = box
|
| 53 |
+
x, y = max(0, x), max(0, y)
|
| 54 |
+
|
| 55 |
+
left_eye = keypoints['left_eye']
|
| 56 |
+
right_eye = keypoints['right_eye']
|
| 57 |
+
dx, dy = right_eye[0] - left_eye[0], right_eye[1] - left_eye[1]
|
| 58 |
+
angle = np.degrees(np.arctan2(dy, dx))
|
| 59 |
+
center = np.mean([left_eye, right_eye], axis=0)
|
| 60 |
+
eyes_center = (float(center[0]), float(center[1]))
|
| 61 |
+
|
| 62 |
+
M = cv2.getRotationMatrix2D(eyes_center, angle, 1.0)
|
| 63 |
+
aligned = cv2.warpAffine(image_rgb, M, (image_rgb.shape[1], image_rgb.shape[0]), flags=cv2.INTER_CUBIC)
|
| 64 |
+
|
| 65 |
+
face = aligned[y:y+h, x:x+w]
|
| 66 |
+
face_resized = cv2.resize(face, (224, 224))
|
| 67 |
+
return face_resized, None
|
| 68 |
+
|
| 69 |
+
# === Upload ===
|
| 70 |
+
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png", "bmp"], label_visibility="collapsed")
|
| 71 |
+
|
| 72 |
+
# === Result & Image Display ===
|
| 73 |
+
if uploaded_file:
|
| 74 |
+
image = Image.open(uploaded_file)
|
| 75 |
+
face_img, error = detect_and_align_face(image)
|
| 76 |
+
|
| 77 |
+
if error:
|
| 78 |
+
st.error(error)
|
| 79 |
+
else:
|
| 80 |
+
img_array = img_to_array(face_img).astype(np.float32)
|
| 81 |
+
img_array = preprocess_input(img_array)
|
| 82 |
+
img_batch = np.expand_dims(img_array, axis=0)
|
| 83 |
+
gender_batch = np.array([[gender_value]], dtype=np.float32)
|
| 84 |
+
|
| 85 |
+
bmi_scaled = model.predict([img_batch, gender_batch])[0][0]
|
| 86 |
+
bmi = scaler.inverse_transform([[bmi_scaled]])[0][0]
|
| 87 |
|
| 88 |
+
st.success(f"🎯 **Predicted BMI: {bmi:.2f}**")
|
| 89 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 90 |
+
else:
|
| 91 |
+
st.markdown("📤 *Upload an image above to receive a prediction.*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|