Commit
·
bfb1531
1
Parent(s):
de7656b
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
|
| 6 |
+
# === Charger les trois modèles binaires ===
|
| 7 |
+
model_cubisme = tf.keras.models.load_model("model_cubisme.keras")
|
| 8 |
+
model_expressionnisme = tf.keras.models.load_model("model_expressionnisme.keras")
|
| 9 |
+
model_postimp = tf.keras.models.load_model("model_postimpressionnisme.keras")
|
| 10 |
+
|
| 11 |
+
# === Liste des classes ===
|
| 12 |
+
classes = ["Cubisme", "Expressionnisme", "Post-impressionnisme"]
|
| 13 |
+
|
| 14 |
+
# === Fonction de prédiction ===
|
| 15 |
+
def predire(image):
|
| 16 |
+
# Prétraitement
|
| 17 |
+
image_resized = tf.image.resize(image, (224, 224)) / 255.0
|
| 18 |
+
image_batch = tf.expand_dims(image_resized, axis=0)
|
| 19 |
+
|
| 20 |
+
# Prédictions des trois modèles
|
| 21 |
+
p_cubisme = float(model_cubisme.predict(image_batch)[0][0])
|
| 22 |
+
p_expr = float(model_expressionnisme.predict(image_batch)[0][0])
|
| 23 |
+
p_postimp = float(model_postimp.predict(image_batch)[0][0])
|
| 24 |
+
|
| 25 |
+
probs = [p_cubisme, p_expr, p_postimp]
|
| 26 |
+
|
| 27 |
+
# Tri (optionnel, pour classer les barres par probabilité décroissante)
|
| 28 |
+
sorted_indices = np.argsort(probs)[::-1]
|
| 29 |
+
sorted_classes = [classes[i] for i in sorted_indices]
|
| 30 |
+
sorted_probs = [probs[i] for i in sorted_indices]
|
| 31 |
+
colors = ['#2ecc71' if p >= 0.5 else '#bdc3c7' for p in sorted_probs]
|
| 32 |
+
|
| 33 |
+
# === Construction du graphique ===
|
| 34 |
+
fig = go.Figure(go.Bar(
|
| 35 |
+
x=sorted_classes,
|
| 36 |
+
y=sorted_probs,
|
| 37 |
+
marker=dict(color=colors, line=dict(color='black', width=1)),
|
| 38 |
+
text=[f"{p*100:.1f}%" for p in sorted_probs],
|
| 39 |
+
textposition='auto'
|
| 40 |
+
))
|
| 41 |
+
|
| 42 |
+
fig.update_layout(
|
| 43 |
+
xaxis=dict(fixedrange=True, tickangle=45, tickfont=dict(size=15), automargin=True),
|
| 44 |
+
yaxis=dict(fixedrange=True, range=[0, 1], title="Probabilité", tickfont=dict(size=14)),
|
| 45 |
+
title=dict(
|
| 46 |
+
text="Probabilités par mouvement pictural",
|
| 47 |
+
y=0.90,
|
| 48 |
+
pad=dict(b=30)
|
| 49 |
+
),
|
| 50 |
+
margin=dict(l=20, r=20, t=0, b=60),
|
| 51 |
+
height=600,
|
| 52 |
+
font=dict(size=13)
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
fig.data[0].textfont = dict(color='black', size=14, family="Arial")
|
| 56 |
+
return fig
|
| 57 |
+
|
| 58 |
+
# === Interface Gradio ===
|
| 59 |
+
demo = gr.Interface(
|
| 60 |
+
fn=predire,
|
| 61 |
+
inputs=gr.Image(type="numpy", label="Importer une œuvre"),
|
| 62 |
+
outputs=gr.Plot(label="Résultats de la classification"),
|
| 63 |
+
title="🎨 Classification de style pictural (3 CNN binaires)",
|
| 64 |
+
description="Chaque CNN évalue indépendamment la probabilité d’appartenance à un mouvement pictural. Les barres vertes indiquent une probabilité ≥ 50 %.",
|
| 65 |
+
theme=gr.themes.Soft()
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.launch()
|