File size: 2,147 Bytes
a8d3cd1
 
 
 
 
 
 
 
 
 
 
426784f
a8d3cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426784f
 
a8d3cd1
 
426784f
a8d3cd1
 
 
 
 
 
 
 
 
 
426784f
a8d3cd1
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
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()