CNN_images_Deep / app.py
lamba66's picture
Create app.py
01e45b3 verified
import tensorflow as tf
import numpy as np
import gradio as gr
from PIL import Image
# 1. Charger le modèle entraîné (.h5)
model = tf.keras.models.load_model("CNN_model.h5") # change le nom si besoin
# 2. Définir les classes CIFAR-10
classes = [
"airplane", "automobile", "bird", "cat", "deer",
"dog", "frog", "horse", "ship", "truck"
]
# 3. Fonction de prétraitement + prédiction
def predict(image):
image = image.resize((32, 32)) # Redimensionner à 32x32
image_array = np.array(image) / 255.0 # Normaliser
image_array = image_array.reshape(1, 32, 32, 3) # Ajouter batch dimension
predictions = model.predict(image_array)[0]
result = {classes[i]: float(predictions[i]) for i in range(10)}
return result
# 4. Interface Gradio
gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=3),
title="Classificateur CIFAR-10",
description="Téléverse une image pour prédire sa classe parmi les 10 catégories CIFAR-10.",
theme='NoCrypt/miku'
).launch()