Commit
·
1a0e122
1
Parent(s):
3da0177
Update app.py
Browse files
app.py
CHANGED
|
@@ -159,132 +159,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 159 |
|
| 160 |
demo.launch()
|
| 161 |
|
| 162 |
-
"""
|
| 163 |
-
VERSION 2 avec sélection des mouvements et affichage des graphiques uniquement pour les mouvements reconnus
|
| 164 |
-
import gradio as gr
|
| 165 |
-
import tensorflow as tf
|
| 166 |
-
import numpy as np
|
| 167 |
-
import plotly.graph_objects as go
|
| 168 |
-
|
| 169 |
-
# === Charger les trois modèles binaires ===
|
| 170 |
-
model_cubisme = tf.keras.models.load_model("Cubisme_MobileNetV2_UL_c2_l0_v96_20251013_114051.keras")
|
| 171 |
-
model_expressionnisme = tf.keras.models.load_model("Expressionnisme_MobileNetV2_UL_c2_l0_v84_20251012_232500.keras")
|
| 172 |
-
model_postimp = tf.keras.models.load_model("Postimpressionnisme_MobileNetV2_UL_c2_l0_v89_20251013_111049.keras")
|
| 173 |
-
|
| 174 |
-
# === Dictionnaire des modèles ===
|
| 175 |
-
modeles_disponibles = {
|
| 176 |
-
"Cubisme": model_cubisme,
|
| 177 |
-
"Expressionnisme": model_expressionnisme,
|
| 178 |
-
"Post-impressionnisme": model_postimp
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
# === Fonction de prédiction ===
|
| 182 |
-
def predire(image, mouvements_selectionnes):
|
| 183 |
-
# Vérifier qu'au moins un mouvement est sélectionné
|
| 184 |
-
if not mouvements_selectionnes:
|
| 185 |
-
return None, gr.update(value="⚠️ **Veuillez sélectionner au moins un mouvement pictural à analyser.**", visible=True)
|
| 186 |
-
|
| 187 |
-
# Prétraitement
|
| 188 |
-
image_resized = tf.image.resize(image, (224, 224)) / 255.0
|
| 189 |
-
image_batch = tf.expand_dims(image_resized, axis=0)
|
| 190 |
-
|
| 191 |
-
# Prédictions uniquement pour les modèles sélectionnés
|
| 192 |
-
resultats = {}
|
| 193 |
-
for mouvement in mouvements_selectionnes:
|
| 194 |
-
modele = modeles_disponibles[mouvement]
|
| 195 |
-
prob = float(modele.predict(image_batch, verbose=0)[0][0])
|
| 196 |
-
resultats[mouvement] = prob
|
| 197 |
-
|
| 198 |
-
# Filtrer les mouvements reconnus (≥ 50%)
|
| 199 |
-
mouvements_reconnus = {m: p for m, p in resultats.items() if p >= 0.5}
|
| 200 |
-
|
| 201 |
-
# Si aucun mouvement n'atteint 50%
|
| 202 |
-
if not mouvements_reconnus:
|
| 203 |
-
return None, gr.update(value="❌ **Aucun des mouvements picturaux sélectionnés n'a été reconnu.**", visible=True)
|
| 204 |
-
|
| 205 |
-
# Tri par probabilité décroissante
|
| 206 |
-
mouvements_tries = sorted(mouvements_reconnus.items(), key=lambda x: x[1], reverse=True)
|
| 207 |
-
classes_triees = [m for m, _ in mouvements_tries]
|
| 208 |
-
probs_triees = [p for _, p in mouvements_tries]
|
| 209 |
-
|
| 210 |
-
# Couleur verte pour tous (car tous sont ≥ 50%)
|
| 211 |
-
colors = ['#2ecc71'] * len(classes_triees)
|
| 212 |
-
|
| 213 |
-
# === Construction du graphique ===
|
| 214 |
-
fig = go.Figure(go.Bar(
|
| 215 |
-
x=classes_triees,
|
| 216 |
-
y=probs_triees,
|
| 217 |
-
marker=dict(color=colors, line=dict(color='black', width=1)),
|
| 218 |
-
text=[f"{p*100:.1f}%" for p in probs_triees],
|
| 219 |
-
textposition='auto',
|
| 220 |
-
width=0.4
|
| 221 |
-
))
|
| 222 |
-
|
| 223 |
-
fig.update_layout(
|
| 224 |
-
xaxis=dict(
|
| 225 |
-
fixedrange=True,
|
| 226 |
-
tickangle=45,
|
| 227 |
-
tickfont=dict(size=15),
|
| 228 |
-
automargin=True
|
| 229 |
-
),
|
| 230 |
-
yaxis=dict(
|
| 231 |
-
fixedrange=True,
|
| 232 |
-
range=[0, 1],
|
| 233 |
-
title="Probabilité",
|
| 234 |
-
tickfont=dict(size=14)
|
| 235 |
-
),
|
| 236 |
-
title=dict(
|
| 237 |
-
text="Mouvements picturaux<br>reconnus (≥ 50%)",
|
| 238 |
-
y=0.90,
|
| 239 |
-
pad=dict(b=30)
|
| 240 |
-
),
|
| 241 |
-
margin=dict(l=20, r=20, t=0, b=60),
|
| 242 |
-
height=600,
|
| 243 |
-
font=dict(size=13)
|
| 244 |
-
)
|
| 245 |
-
|
| 246 |
-
fig.data[0].textfont = dict(color='black', size=14, family="Arial")
|
| 247 |
-
|
| 248 |
-
return fig, gr.update(visible=False)
|
| 249 |
-
|
| 250 |
-
# === Interface Gradio ===
|
| 251 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 252 |
-
gr.Markdown("# 🎨 Classification de style pictural (3 CNN binaires)")
|
| 253 |
-
gr.Markdown(
|
| 254 |
-
"Sélectionnez les mouvements picturaux à analyser. "
|
| 255 |
-
"Seuls ceux atteignant une probabilité ≥ 50% seront affichés."
|
| 256 |
-
)
|
| 257 |
-
|
| 258 |
-
with gr.Row():
|
| 259 |
-
with gr.Column(scale=1):
|
| 260 |
-
image_input = gr.Image(type="numpy", label="Importer une œuvre")
|
| 261 |
-
|
| 262 |
-
mouvements_checkbox = gr.CheckboxGroup(
|
| 263 |
-
choices=["Cubisme", "Expressionnisme", "Post-impressionnisme"],
|
| 264 |
-
value=["Cubisme", "Expressionnisme", "Post-impressionnisme"],
|
| 265 |
-
label="Mouvements à analyser",
|
| 266 |
-
info="Cochez les mouvements picturaux à tester"
|
| 267 |
-
)
|
| 268 |
-
|
| 269 |
-
analyser_btn = gr.Button("🔍 Analyser", variant="primary", size="lg")
|
| 270 |
-
|
| 271 |
-
with gr.Column(scale=1):
|
| 272 |
-
output_plot = gr.Plot(label="Résultats de la classification")
|
| 273 |
-
output_message = gr.Markdown(visible=False)
|
| 274 |
-
|
| 275 |
-
analyser_btn.click(
|
| 276 |
-
fn=predire,
|
| 277 |
-
inputs=[image_input, mouvements_checkbox],
|
| 278 |
-
outputs=[output_plot, output_message]
|
| 279 |
-
)
|
| 280 |
-
|
| 281 |
-
gr.Markdown(
|
| 282 |
-
"---\n"
|
| 283 |
-
"**Note :** Chaque CNN évalue indépendamment la probabilité d'appartenance "
|
| 284 |
-
"à un mouvement pictural. Les barres vertes indiquent une reconnaissance ≥ 50%."
|
| 285 |
-
)
|
| 286 |
-
|
| 287 |
-
demo.launch()"""
|
| 288 |
|
| 289 |
"""
|
| 290 |
# Première version
|
|
|
|
| 159 |
|
| 160 |
demo.launch()
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
"""
|
| 164 |
# Première version
|