| import gradio as gr |
| from keras.models import load_model |
| from keras.preprocessing.image import img_to_array |
| from PIL import Image |
| import numpy as np |
|
|
|
|
| model = load_model("best_malaria_detector_model.h5") |
|
|
| |
| def preprocess_image(img, target_size=(64, 64)): |
| img = img.resize(target_size) |
| img_array = img_to_array(img) |
| img_array = img_array.astype("float32") / 255.0 |
| img_array = np.expand_dims(img_array, axis=0) |
| return img_array |
|
|
| |
| def predict(image): |
| img_array = preprocess_image(image) |
| prediction = model.predict(img_array)[0][0] |
| label = "Infectée (1)" if prediction >= 0.5 else "Non infectée (0)" |
| confidence = prediction if prediction >= 0.5 else 1 - prediction |
| return f"{label} (confiance : {confidence:.2%})" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown( |
| """ |
| # 🐾 Détecteur de Malaria |
| Téléversez une image pour savoir si la persone est **infecté** ou **non infecté**. |
| |
| 👉 Formats supportés : JPG, PNG, JPEG |
| """ |
| ) |
|
|
| with gr.Row(): |
| image_input = gr.Image(type="pil", label="Image à analyser") |
| result_output = gr.Textbox(label="Résultat", lines=2) |
|
|
| submit_btn = gr.Button("🔍 Prédire") |
|
|
| submit_btn.click(fn=predict, inputs=image_input, outputs=result_output) |
|
|
| gr.Examples( |
| examples=[ |
| ["infecté.jpeg"], |
| ["non_infecté.jpg"] |
| ], |
| inputs=image_input |
| ) |
|
|
| demo.launch() |
|
|