Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,21 @@
|
|
| 1 |
-
import
|
| 2 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Forcer TensorFlow à utiliser le CPU
|
| 3 |
-
|
| 4 |
-
from fastapi import FastAPI, File, UploadFile
|
| 5 |
-
import cv2
|
| 6 |
-
import numpy as np
|
| 7 |
import tensorflow as tf
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Charger le modèle IA
|
| 12 |
model = tf.keras.models.load_model("tomato_disease_mobilenetv2_25Epc.h5")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
},
|
| 20 |
-
"Tomato___Early_blight": {
|
| 21 |
-
"description": "Le mildiou précoce entraîne des taches brun foncé concentriques sur les feuilles, qui finissent par se dessécher.",
|
| 22 |
-
"recommendations": "Supprimez les feuilles infectées et appliquez un fongicide préventif. Évitez l'humidité excessive."
|
| 23 |
-
},
|
| 24 |
-
"Tomato___Late_blight": {
|
| 25 |
-
"description": "Le mildiou tardif provoque des taches gris-brun entourées d’un halo jaune, et peut détruire rapidement la plante.",
|
| 26 |
-
"recommendations": "Utilisez un fongicide systémique et éliminez les plantes contaminées immédiatement pour éviter la propagation."
|
| 27 |
-
},
|
| 28 |
-
"Tomato___healthy": {
|
| 29 |
-
"description": "Votre plante est en bonne santé ! 🎉",
|
| 30 |
-
"recommendations": "Continuez à surveiller l’humidité et appliquez un entretien régulier."
|
| 31 |
-
}
|
| 32 |
-
}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
img =
|
| 38 |
-
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
|
| 39 |
-
|
| 40 |
-
# Prétraitement de l’image
|
| 41 |
-
img = cv2.resize(img, (224, 224))
|
| 42 |
img = img / 255.0 # Normalisation
|
| 43 |
img = np.expand_dims(img, axis=0)
|
| 44 |
|
|
@@ -47,14 +24,16 @@ async def predict(file: UploadFile = File(...)):
|
|
| 47 |
predicted_class = np.argmax(prediction)
|
| 48 |
confidence = np.max(prediction) * 100 # Score de confiance en %
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
"confidence": round(confidence, 2),
|
| 58 |
-
"description": description,
|
| 59 |
-
"recommendations": recommendations
|
| 60 |
-
}
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
|
| 6 |
+
# Charger le modèle (Remplace par ton modèle)
|
|
|
|
|
|
|
| 7 |
model = tf.keras.models.load_model("tomato_disease_mobilenetv2_25Epc.h5")
|
| 8 |
|
| 9 |
+
# Classes des maladies (modifie selon ton dataset)
|
| 10 |
+
classes = ["Tomato___Bacterial_spot", "Tomato___Early_blight", "Tomato___Late_blight",
|
| 11 |
+
"Tomato___Leaf_Mold", "Tomato___Septoria_leaf_spot", "Tomato___Spider_mites Two-spotted_spider_mite",
|
| 12 |
+
"Tomato___Target_Spot", "Tomato___Tomato_Yellow_Leaf_Curl_Virus",
|
| 13 |
+
"Tomato___Tomato_mosaic_virus", "Tomato___healthy"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Fonction de prédiction avec score de confiance
|
| 16 |
+
def predict_image(image):
|
| 17 |
+
# Convertir en format compatible avec le modèle
|
| 18 |
+
img = cv2.resize(image, (224, 224)) # Adapter à la taille du modèle
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
img = img / 255.0 # Normalisation
|
| 20 |
img = np.expand_dims(img, axis=0)
|
| 21 |
|
|
|
|
| 24 |
predicted_class = np.argmax(prediction)
|
| 25 |
confidence = np.max(prediction) * 100 # Score de confiance en %
|
| 26 |
|
| 27 |
+
return f"🌱 Résultat : {classes[predicted_class]} \n🔍 Confiance : {confidence:.2f}%"
|
| 28 |
+
|
| 29 |
+
# Interface Gradio
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=predict_image,
|
| 32 |
+
inputs=gr.Image(type="numpy"),
|
| 33 |
+
outputs="text",
|
| 34 |
+
title="Détection des Maladies des Plantes 🌿",
|
| 35 |
+
description="📸 Télécharge une image d'une feuille de plante pour obtenir une analyse IA."
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
# Lancer l’application
|
| 39 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|