Update src/streamlit_app1.py
Browse files- src/streamlit_app1.py +36 -23
src/streamlit_app1.py
CHANGED
|
@@ -3,36 +3,49 @@ import tensorflow as tf
|
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def predict(model, img_array, classes):
|
| 10 |
prediction = model.predict(img_array)
|
| 11 |
index = np.argmax(prediction)
|
| 12 |
-
return classes[index], prediction[0][index]
|
| 13 |
|
|
|
|
| 14 |
st.title("🧠 Classification d’images")
|
| 15 |
|
|
|
|
| 16 |
option = st.selectbox("Choisissez le modèle :", ("Infecté / Non Infecté", "Chat / Chien"))
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
if uploaded_file:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# Chargement des modèles avec message
|
| 7 |
+
with st.spinner("Chargement des modèles..."):
|
| 8 |
+
try:
|
| 9 |
+
model_infect = tf.keras.models.load_model("src/exo1.keras")
|
| 10 |
+
model_animals = tf.keras.models.load_model("src/exo2.keras")
|
| 11 |
+
st.success("Modèles chargés avec succès ✅")
|
| 12 |
+
except Exception as e:
|
| 13 |
+
st.error(f"Erreur lors du chargement des modèles : {e}")
|
| 14 |
+
st.stop()
|
| 15 |
+
|
| 16 |
+
# Fonction de prédiction
|
| 17 |
def predict(model, img_array, classes):
|
| 18 |
prediction = model.predict(img_array)
|
| 19 |
index = np.argmax(prediction)
|
| 20 |
+
return classes[index], float(prediction[0][index])
|
| 21 |
|
| 22 |
+
# Interface utilisateur
|
| 23 |
st.title("🧠 Classification d’images")
|
| 24 |
|
| 25 |
+
# Choix du modèle
|
| 26 |
option = st.selectbox("Choisissez le modèle :", ("Infecté / Non Infecté", "Chat / Chien"))
|
| 27 |
|
| 28 |
+
# Upload d'image
|
| 29 |
+
uploaded_file = st.file_uploader("Uploader une image", type=["jpg", "jpeg", "png"])
|
| 30 |
|
| 31 |
if uploaded_file:
|
| 32 |
+
try:
|
| 33 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 34 |
+
st.image(image, caption="Image chargée", use_column_width=True)
|
| 35 |
+
|
| 36 |
+
# Bouton pour déclencher la prédiction
|
| 37 |
+
if st.button("Prédire"):
|
| 38 |
+
with st.spinner("Prétraitement de l’image..."):
|
| 39 |
+
image = image.resize((224, 224))
|
| 40 |
+
img_array = np.array(image) / 255.0
|
| 41 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 42 |
+
|
| 43 |
+
with st.spinner("Prédiction en cours..."):
|
| 44 |
+
if option == "Infecté / Non Infecté":
|
| 45 |
+
label, confidence = predict(model_infect, img_array, ["Non Infecté", "Infecté"])
|
| 46 |
+
else:
|
| 47 |
+
label, confidence = predict(model_animals, img_array, ["Chat", "Chien"])
|
| 48 |
+
|
| 49 |
+
st.success(f"✅ Classe prédite : **{label}** avec une confiance de **{confidence:.2f}**")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"❌ Erreur lors du traitement : {e}")
|