Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Fonction convertissant l'image à la dimension exacte utilisée sur le training
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
def imageToArray(image_path):
|
| 10 |
+
|
| 11 |
+
width, height = 32, 32 #Remplacer par les dimensions requises par notre modèle
|
| 12 |
+
|
| 13 |
+
image = Image.open(image_path)
|
| 14 |
+
image = image.resize((width, height))
|
| 15 |
+
# Convertir l'image en numpy array et normaliser les valeurs des pixels (si nécessaire)
|
| 16 |
+
image_array = np.asarray(image)
|
| 17 |
+
image_array = image_array / 255.0 # Normaliser les valeurs des pixels entre 0 et 1
|
| 18 |
+
|
| 19 |
+
# Redimensionner la matrice image pour qu'elle correspond à la forme d'entrée de notre modèle
|
| 20 |
+
image_array = image_array.reshape(1, width, height, 3)
|
| 21 |
+
|
| 22 |
+
return image_array
|
| 23 |
+
# 1. Charger le modèle entraîné (.h5)
|
| 24 |
+
model = tf.keras.models.load_model("CNN_model.h5") # change le nom si besoin
|
| 25 |
+
|
| 26 |
+
# 2. Définir les classes CIFAR-10
|
| 27 |
+
classes = [
|
| 28 |
+
"airplane", "automobile", "bird", "cat", "deer",
|
| 29 |
+
"dog", "frog", "horse", "ship", "truck"
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
# 3. Fonction de prétraitement + prédiction
|
| 33 |
+
def predict(image):
|
| 34 |
+
image = image.resize((32, 32)) # Redimensionner à 32x32
|
| 35 |
+
image_array = np.array(image) / 255.0 # Normaliser
|
| 36 |
+
image_array = image_array.reshape(1, 32, 32, 3) # Ajouter batch dimension
|
| 37 |
+
predictions = model.predict(image_array)[0]
|
| 38 |
+
result = {classes[i]: float(predictions[i]) for i in range(10)}
|
| 39 |
+
return result
|
| 40 |
+
|
| 41 |
+
# 4. Interface Gradio
|
| 42 |
+
gr.Interface(
|
| 43 |
+
fn=predict,
|
| 44 |
+
inputs=gr.Image(type="pil"),
|
| 45 |
+
outputs=gr.Label(num_top_classes=3),
|
| 46 |
+
title="Classificateur CIFAR-10",
|
| 47 |
+
theme='NoCrypt/miku',
|
| 48 |
+
description="Téléverse une image pour prédire sa classe parmi les 10 catégories CIFAR-10."
|
| 49 |
+
).launch()
|
| 50 |
+
|