File size: 1,602 Bytes
8fa6a1e
 
f823189
8fa6a1e
f823189
 
 
 
 
 
 
 
33811e0
8fa6a1e
f823189
 
8fa6a1e
33811e0
 
 
 
 
f823189
 
 
8fa6a1e
f823189
33811e0
8fa6a1e
f823189
 
8fa6a1e
 
 
 
 
 
 
 
33811e0
 
 
 
 
 
 
 
 
8fa6a1e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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()