BMIAPP / app.py
LEWOPO's picture
Update app.py
426784f verified
import cv2
import streamlit as st
from keras_vggface.vggface import VGGFace
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D
import numpy as np
import pickle
pickle_file_path = 'svm_model.pkl'
with open(pickle_file_path, 'rb') as file:
svm_model = pickle.load(file)
base_model = VGGFace(model='vgg16', include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
model = Model(inputs=base_model.input, outputs=x)
# Function to preprocess the image
def preprocess_image(img):
img = cv2.resize(img, (224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img[0] # Remove the extra dimension
return img
def extract_features(image_array):
# img = np.squeeze(image_array, axis=0)
img = np.expand_dims(image_array, axis=0)
img = tf.keras.applications.resnet50.preprocess_input(img)
features = model.predict(img,verbose=0)
return features.flatten()
# Function to predict BMI
def predict_bmi(img):
pre_img = preprocess_image(img)
features = extract_features(pre_img)
features = features.reshape(1,-1)
pred = svm_model.predict(features)
return pred
def main():
st.title("Prédiction de l'IMC à partir de l'image de la caméra")
st.write("Cette application prédit l'IMC d'une personne à partir d'une image capturée à l'aide de l'appareil photo.")
# Capture an image from the camera using streamlit-media's camera_input function
img_file_buffer = st.camera_input("Prendre une photo")
if img_file_buffer is not None:
# Load the image data from the file buffer
file_bytes = np.asarray(bytearray(img_file_buffer.getvalue()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, 1)
# Preprocess the image and predict BMI
bmi_label = predict_bmi(img)
# Display the predicted BMI
st.write("IMC prédit ::", str(bmi_label[0] - 5))
if __name__ == '__main__':
main()