Spaces:
Sleeping
Sleeping
import cv2 | |
import streamlit as st | |
from PIL import Image | |
import numpy as np | |
from tensorflow.keras.preprocessing import image | |
from tensorflow.keras.applications.vgg16 import preprocess_input | |
import tensorflow as tf | |
from base64 import b64decode, b64encode | |
import PIL | |
import io | |
import html | |
import time | |
from facenet_pytorch import MTCNN | |
model_file_path = 'models/bmi.h5' | |
model = tf.keras.models.load_model(model_file_path) | |
mtcnn2 = MTCNN( | |
image_size=160, margin=40, min_face_size=20, | |
thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=False | |
) | |
# Preprocess the images for VGG16 | |
def preprocess_image(img_path): | |
img = image.load_img(img_path, target_size = (224, 224)) | |
img = image.img_to_array(img) | |
img = np.expand_dims(img, axis = 0) | |
img = preprocess_input(img)/255. | |
return img | |
def main(): | |
st.title("BMI Prediction from Camera Image") | |
st.write("This app predicts the BMI of a person from an image captured using the camera.") | |
# Capture an image from the camera using streamlit-media's camera_input function | |
img_file_buffer = st.camera_input("Take a picture") | |
if img_file_buffer is not None: | |
# To read image file buffer as a PIL Image: | |
img = Image.open(img_file_buffer) | |
detected_image = Image.fromarray(mtcnn2(img).numpy().transpose(1, 2, 0).astype(np.uint8)) | |
st.image(detected_image, caption="Detected Face") | |
embeddings = preprocess_image(img_file_buffer) | |
bmi = round(model.predict(embeddings), 2) - 4 | |
st.write(f"Your BMI is {bmi}") | |
if __name__ == '__main__': | |
main() | |