Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing import image | |
| import numpy as np | |
| # Charger le modèle (doit être dans le même dossier que app.py) | |
| model = tf.keras.models.load_model("orange_disease_model.h5") | |
| # Noms des classes (dans le même ordre qu'à l'entraînement) | |
| class_names = ['blackspot', 'canker', 'fresh', 'grenning'] | |
| def predict_image(img): | |
| """ | |
| img : PIL Image (fourni par Gradio) | |
| """ | |
| # Redimensionner à la taille attendue par le modèle | |
| img = img.resize((256, 256)) | |
| # Convertir en tableau numpy et normaliser | |
| img_array = image.img_to_array(img) | |
| img_array = img_array / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) # ajouter la dimension batch | |
| # Prédiction | |
| predictions = model.predict(img_array) | |
| predicted_index = np.argmax(predictions) | |
| predicted_class = class_names[predicted_index] | |
| confidence = np.max(predictions) * 100 | |
| return f"{predicted_class} ({confidence:.2f}%)" | |
| # Interface Gradio | |
| iface = gr.Interface( | |
| fn=predict_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="Orange Disease Classification", | |
| description="Téléchargez une image d'orange pour détecter la maladie : blackspot, canker, fresh ou greening." | |
| ) | |
| # Lancer l'application | |
| iface.launch() |