Iralion's picture
Create app.py
ec008e4 verified
# Fonction convertissant l'image à la dimension exacte utilisée sur le training
from PIL import Image
def imageToArray(image_path):
width, height = 32, 32 #Remplacer par les dimensions requises par notre modèle
image = Image.open(image_path)
image = image.resize((width, height))
# Convertir l'image en numpy array et normaliser les valeurs des pixels (si nécessaire)
image_array = np.asarray(image)
image_array = image_array / 255.0 # Normaliser les valeurs des pixels entre 0 et 1
# Redimensionner la matrice image pour qu'elle correspond à la forme d'entrée de notre modèle
image_array = image_array.reshape(1, width, height, 3)
return image_array
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_model2.h5") # change le nom si besoin
# 2. Définir les classes CIFAR-10
class_names = [
'apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle', 'bicycle', 'bottle',
'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel', 'can', 'castle', 'caterpillar', 'cattle',
'chair', 'chimpanzee', 'clock', 'cloud', 'cockroach', 'couch', 'crab', 'crocodile', 'cup', 'dinosaur',
'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl', 'hamster', 'house', 'kangaroo', 'keyboard',
'lamp', 'lawn_mower', 'leopard', 'lion', 'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain',
'mouse', 'mushroom', 'oak_tree', 'orange', 'orchid', 'otter', 'palm_tree', 'pear', 'pickup_truck', 'pine_tree',
'plain', 'plate', 'poppy', 'porcupine', 'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket',
'rose', 'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake', 'spider',
'squirrel', 'streetcar', 'sunflower', 'sweet_pepper', 'table', 'tank', 'telephone', 'television', 'tiger', 'tractor',
'train', 'trout', 'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman', 'worm'
]
# 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 = {class_names[i]: float(predictions[i]) for i in range(100)}
return result
# 4. Interface Gradio
gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=3),
title="Classificateur CIFAR-100",
description="Téléverse une image pour prédire sa classe parmi les 100 catégories CIFAR-100.",
theme='JohnSmith9982/small_and_pretty'
).launch()