Eric2mangel commited on
Commit
2666269
·
1 Parent(s): d26908b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -1
app.py CHANGED
@@ -8,6 +8,131 @@ model_cubisme = tf.keras.models.load_model("Cubisme_MobileNetV2_UL_c2_l0_v96_202
8
  model_expressionnisme = tf.keras.models.load_model("Expressionnisme_MobileNetV2_UL_c2_l0_v84_20251012_232500.keras")
9
  model_postimp = tf.keras.models.load_model("Postimpressionnisme_MobileNetV2_UL_c2_l0_v89_20251013_111049.keras")
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # === Liste des classes ===
12
  classes = ["Cubisme", "Expressionnisme", "Post-impressionnisme"]
13
 
@@ -65,4 +190,4 @@ demo = gr.Interface(
65
  theme=gr.themes.Soft()
66
  )
67
 
68
- demo.launch()
 
8
  model_expressionnisme = tf.keras.models.load_model("Expressionnisme_MobileNetV2_UL_c2_l0_v84_20251012_232500.keras")
9
  model_postimp = tf.keras.models.load_model("Postimpressionnisme_MobileNetV2_UL_c2_l0_v89_20251013_111049.keras")
10
 
11
+ # === Dictionnaire des modèles ===
12
+ modeles_disponibles = {
13
+ "Cubisme": model_cubisme,
14
+ "Expressionnisme": model_expressionnisme,
15
+ "Post-impressionnisme": model_postimp
16
+ }
17
+
18
+ # === Fonction de prédiction ===
19
+ def predire(image, mouvements_selectionnes):
20
+ # Vérifier qu'au moins un mouvement est sélectionné
21
+ if not mouvements_selectionnes:
22
+ return "⚠️ Veuillez sélectionner au moins un mouvement pictural à analyser."
23
+
24
+ # Prétraitement
25
+ image_resized = tf.image.resize(image, (224, 224)) / 255.0
26
+ image_batch = tf.expand_dims(image_resized, axis=0)
27
+
28
+ # Prédictions uniquement pour les modèles sélectionnés
29
+ resultats = {}
30
+ for mouvement in mouvements_selectionnes:
31
+ modele = modeles_disponibles[mouvement]
32
+ prob = float(modele.predict(image_batch, verbose=0)[0][0])
33
+ resultats[mouvement] = prob
34
+
35
+ # Filtrer les mouvements reconnus (≥ 50%)
36
+ mouvements_reconnus = {m: p for m, p in resultats.items() if p >= 0.5}
37
+
38
+ # Si aucun mouvement n'atteint 50%
39
+ if not mouvements_reconnus:
40
+ return "❌ Aucun des mouvements picturaux sélectionnés n'a été reconnu."
41
+
42
+ # Tri par probabilité décroissante
43
+ mouvements_tries = sorted(mouvements_reconnus.items(), key=lambda x: x[1], reverse=True)
44
+ classes_triees = [m for m, _ in mouvements_tries]
45
+ probs_triees = [p for _, p in mouvements_tries]
46
+
47
+ # Couleur verte pour tous (car tous sont ≥ 50%)
48
+ colors = ['#2ecc71'] * len(classes_triees)
49
+
50
+ # === Construction du graphique ===
51
+ fig = go.Figure(go.Bar(
52
+ x=classes_triees,
53
+ y=probs_triees,
54
+ marker=dict(color=colors, line=dict(color='black', width=1)),
55
+ text=[f"{p*100:.1f}%" for p in probs_triees],
56
+ textposition='auto'
57
+ ))
58
+
59
+ fig.update_layout(
60
+ xaxis=dict(
61
+ fixedrange=True,
62
+ tickangle=45,
63
+ tickfont=dict(size=15),
64
+ automargin=True
65
+ ),
66
+ yaxis=dict(
67
+ fixedrange=True,
68
+ range=[0, 1],
69
+ title="Probabilité",
70
+ tickfont=dict(size=14)
71
+ ),
72
+ title=dict(
73
+ text="Mouvements picturaux<br>reconnus (≥ 50%)",
74
+ y=0.90,
75
+ pad=dict(b=30)
76
+ ),
77
+ margin=dict(l=20, r=20, t=0, b=60),
78
+ height=600,
79
+ font=dict(size=13)
80
+ )
81
+
82
+ fig.data[0].textfont = dict(color='black', size=14, family="Arial")
83
+
84
+ return fig
85
+
86
+ # === Interface Gradio ===
87
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
88
+ gr.Markdown("# 🎨 Classification de style pictural (3 CNN binaires)")
89
+ gr.Markdown(
90
+ "Sélectionnez les mouvements picturaux à analyser. "
91
+ "Seuls ceux atteignant une probabilité ≥ 50% seront affichés."
92
+ )
93
+
94
+ with gr.Row():
95
+ with gr.Column(scale=1):
96
+ image_input = gr.Image(type="numpy", label="Importer une œuvre")
97
+
98
+ mouvements_checkbox = gr.CheckboxGroup(
99
+ choices=["Cubisme", "Expressionnisme", "Post-impressionnisme"],
100
+ value=["Cubisme", "Expressionnisme", "Post-impressionnisme"],
101
+ label="Mouvements à analyser",
102
+ info="Cochez les mouvements picturaux à tester"
103
+ )
104
+
105
+ analyser_btn = gr.Button("🔍 Analyser", variant="primary", size="lg")
106
+
107
+ with gr.Column(scale=1):
108
+ output_plot = gr.Plot(label="Résultats de la classification")
109
+
110
+ analyser_btn.click(
111
+ fn=predire,
112
+ inputs=[image_input, mouvements_checkbox],
113
+ outputs=output_plot
114
+ )
115
+
116
+ gr.Markdown(
117
+ "---\n"
118
+ "**Note :** Chaque CNN évalue indépendamment la probabilité d'appartenance "
119
+ "à un mouvement pictural. Les barres vertes indiquent une reconnaissance ≥ 50%."
120
+ )
121
+
122
+ demo.launch()
123
+
124
+ """
125
+ # Première version
126
+ import gradio as gr
127
+ import tensorflow as tf
128
+ import numpy as np
129
+ import plotly.graph_objects as go
130
+
131
+ # === Charger les trois modèles binaires ===
132
+ model_cubisme = tf.keras.models.load_model("Cubisme_MobileNetV2_UL_c2_l0_v96_20251013_114051.keras")
133
+ model_expressionnisme = tf.keras.models.load_model("Expressionnisme_MobileNetV2_UL_c2_l0_v84_20251012_232500.keras")
134
+ model_postimp = tf.keras.models.load_model("Postimpressionnisme_MobileNetV2_UL_c2_l0_v89_20251013_111049.keras")
135
+
136
  # === Liste des classes ===
137
  classes = ["Cubisme", "Expressionnisme", "Post-impressionnisme"]
138
 
 
190
  theme=gr.themes.Soft()
191
  )
192
 
193
+ demo.launch()"""