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|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
# Chargement du modèle pré-entraîné
|
| 10 |
+
model = tf.keras.models.load_model("babong_kidney_classification_model_Tensorflow.h5")
|
| 11 |
+
|
| 12 |
+
# Redéfinition des classes (nouvelles légendes)
|
| 13 |
+
class_names = ["Cyst", "Normal", "Stone", "Tumor"]
|
| 14 |
+
|
| 15 |
+
# Configuration de la page et style global
|
| 16 |
+
st.set_page_config(page_title="Analyse d'Imagerie Rénale", page_icon="🧬", layout="wide")
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
body {
|
| 20 |
+
background-color: #e0f7fa;
|
| 21 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 22 |
+
}
|
| 23 |
+
.main {
|
| 24 |
+
background-color: #e0f7fa;
|
| 25 |
+
}
|
| 26 |
+
h1, h2, h3 {
|
| 27 |
+
color: #006064;
|
| 28 |
+
text-align: center;
|
| 29 |
+
}
|
| 30 |
+
.stButton>button {
|
| 31 |
+
background-color: #006064;
|
| 32 |
+
color: white;
|
| 33 |
+
border-radius: 5px;
|
| 34 |
+
padding: 10px 20px;
|
| 35 |
+
}
|
| 36 |
+
.upload-area {
|
| 37 |
+
border: 2px dashed #006064;
|
| 38 |
+
border-radius: 10px;
|
| 39 |
+
padding: 20px;
|
| 40 |
+
text-align: center;
|
| 41 |
+
background-color: #ffffff;
|
| 42 |
+
margin-bottom: 20px;
|
| 43 |
+
}
|
| 44 |
+
.result-card {
|
| 45 |
+
background-color: #ffffff;
|
| 46 |
+
border-radius: 10px;
|
| 47 |
+
padding: 20px;
|
| 48 |
+
margin-bottom: 20px;
|
| 49 |
+
box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
|
| 50 |
+
}
|
| 51 |
+
</style>
|
| 52 |
+
""", unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
+
# Titre et description principale
|
| 55 |
+
st.title("Analyse d'Imagerie Rénale 🧬")
|
| 56 |
+
st.subheader("Diagnostic assisté par Intelligence Artificielle")
|
| 57 |
+
st.write(
|
| 58 |
+
"Chargez vos radiographies rénales pour obtenir une prédiction détaillée sur la présence de Kyste, Calcul, Tumeur ou un état Normal.")
|
| 59 |
+
|
| 60 |
+
# Zone de téléversement dans la barre latérale
|
| 61 |
+
st.sidebar.header("Téléversement des Images")
|
| 62 |
+
uploaded_files = st.sidebar.file_uploader(
|
| 63 |
+
"Glissez-déposez vos images ou cliquez ici (formats acceptés: JPG, PNG, JPEG)",
|
| 64 |
+
type=["jpg", "png", "jpeg"],
|
| 65 |
+
accept_multiple_files=True,
|
| 66 |
+
key="fileUploader"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if uploaded_files:
|
| 70 |
+
images = []
|
| 71 |
+
predictions_list = []
|
| 72 |
+
|
| 73 |
+
# Traitement de chaque image téléversée
|
| 74 |
+
for uploaded_file in uploaded_files:
|
| 75 |
+
with st.container():
|
| 76 |
+
st.markdown(f"<div class='result-card'><strong>Analyse de l'image : {uploaded_file.name}</strong></div>",
|
| 77 |
+
unsafe_allow_html=True)
|
| 78 |
+
image = Image.open(uploaded_file)
|
| 79 |
+
st.image(image, caption=f"Image fournie : {uploaded_file.name}", use_column_width=True)
|
| 80 |
+
|
| 81 |
+
# Prétraitement de l'image
|
| 82 |
+
image_resized = image.resize((224, 224))
|
| 83 |
+
image_array = np.array(image_resized)
|
| 84 |
+
if image_array.shape[-1] == 4: # Gestion des images avec canal alpha
|
| 85 |
+
image_array = image_array[..., :3]
|
| 86 |
+
image_array = np.expand_dims(image_array, axis=0) / 255.0
|
| 87 |
+
|
| 88 |
+
# Prédiction avec animation de chargement
|
| 89 |
+
with st.spinner("Analyse en cours..."):
|
| 90 |
+
time.sleep(2)
|
| 91 |
+
predictions = model.predict(image_array)
|
| 92 |
+
|
| 93 |
+
index = np.argmax(predictions)
|
| 94 |
+
predicted_label = class_names[index]
|
| 95 |
+
confidence = np.max(predictions)
|
| 96 |
+
|
| 97 |
+
predictions_list.append((predicted_label, confidence, predictions[0]))
|
| 98 |
+
images.append(image)
|
| 99 |
+
|
| 100 |
+
st.success(f"**Résultat :** {predicted_label}")
|
| 101 |
+
st.info(f"**Niveau de Confiance :** {confidence:.2f}")
|
| 102 |
+
|
| 103 |
+
# Affichage graphique des scores
|
| 104 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
| 105 |
+
sns.barplot(x=class_names, y=predictions[0], palette="viridis", ax=ax)
|
| 106 |
+
ax.set_ylim(0, 1)
|
| 107 |
+
ax.set_title("Répartition des Scores de Diagnostic", fontsize=14, color="#004d40")
|
| 108 |
+
ax.set_xlabel("Catégories", fontsize=12, color="#004d40")
|
| 109 |
+
ax.set_ylabel("Score", fontsize=12, color="#004d40")
|
| 110 |
+
st.pyplot(fig)
|
| 111 |
+
|
| 112 |
+
# Si deux images sont téléversées, comparaison côte à côte
|
| 113 |
+
if len(images) == 2:
|
| 114 |
+
st.markdown("<hr>", unsafe_allow_html=True)
|
| 115 |
+
st.subheader("Comparaison Visuelle et Statistique")
|
| 116 |
+
col1, col2 = st.columns(2)
|
| 117 |
+
with col1:
|
| 118 |
+
st.image(images[0], caption="Image A", use_column_width=True)
|
| 119 |
+
st.write(f"**Diagnostic :** {predictions_list[0][0]}")
|
| 120 |
+
st.write(f"**Score :** {predictions_list[0][1]:.2f}")
|
| 121 |
+
with col2:
|
| 122 |
+
st.image(images[1], caption="Image B", use_column_width=True)
|
| 123 |
+
st.write(f"**Diagnostic :** {predictions_list[1][0]}")
|
| 124 |
+
st.write(f"**Score :** {predictions_list[1][1]:.2f}")
|
| 125 |
+
|
| 126 |
+
# Graphique comparatif des deux images
|
| 127 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 128 |
+
x = np.arange(len(class_names))
|
| 129 |
+
width = 0.35
|
| 130 |
+
ax.bar(x - width / 2, predictions_list[0][2], width, label="Image A", color="#00796b")
|
| 131 |
+
ax.bar(x + width / 2, predictions_list[1][2], width, label="Image B", color="#c62828")
|
| 132 |
+
ax.set_xticks(x)
|
| 133 |
+
ax.set_xticklabels(class_names)
|
| 134 |
+
ax.legend(title="Images", fontsize=10)
|
| 135 |
+
ax.set_title("Comparaison des Scores de Classification", fontsize=14, color="#004d40")
|
| 136 |
+
ax.set_ylabel("Score de Classification", fontsize=12, color="#004d40")
|
| 137 |
+
st.pyplot(fig)
|
| 138 |
+
|
| 139 |
+
# Note informative en bas de page
|
| 140 |
+
st.markdown("""
|
| 141 |
+
<div style='text-align: center; padding: 20px;'>
|
| 142 |
+
<em>Attention : Ce système fournit une aide au diagnostic basée sur l'IA. Pour un diagnostic définitif, consultez un professionnel de santé.</em>
|
| 143 |
+
</div>
|
| 144 |
+
""", unsafe_allow_html=True)
|