Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Fonction convertissant l'image à la dimension exacte utilisée sur le training
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
def imageToArray(image_path):
|
| 5 |
+
|
| 6 |
+
width, height = 32, 32 #Remplacer par les dimensions requises par notre modèle
|
| 7 |
+
|
| 8 |
+
image = Image.open(image_path)
|
| 9 |
+
image = image.resize((width, height))
|
| 10 |
+
# Convertir l'image en numpy array et normaliser les valeurs des pixels (si nécessaire)
|
| 11 |
+
image_array = np.asarray(image)
|
| 12 |
+
image_array = image_array / 255.0 # Normaliser les valeurs des pixels entre 0 et 1
|
| 13 |
+
|
| 14 |
+
# Redimensionner la matrice image pour qu'elle correspond à la forme d'entrée de notre modèle
|
| 15 |
+
image_array = image_array.reshape(1, width, height, 3)
|
| 16 |
+
|
| 17 |
+
return image_array
|
| 18 |
+
|
| 19 |
+
import tensorflow as tf
|
| 20 |
+
import numpy as np
|
| 21 |
+
import gradio as gr
|
| 22 |
+
from PIL import Image
|
| 23 |
+
|
| 24 |
+
# 1. Charger le modèle entraîné (.h5)
|
| 25 |
+
model = tf.keras.models.load_model("CNN_model2.h5") # change le nom si besoin
|
| 26 |
+
|
| 27 |
+
# 2. Définir les classes CIFAR-10
|
| 28 |
+
class_names = [
|
| 29 |
+
'apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle', 'bicycle', 'bottle',
|
| 30 |
+
'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel', 'can', 'castle', 'caterpillar', 'cattle',
|
| 31 |
+
'chair', 'chimpanzee', 'clock', 'cloud', 'cockroach', 'couch', 'crab', 'crocodile', 'cup', 'dinosaur',
|
| 32 |
+
'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl', 'hamster', 'house', 'kangaroo', 'keyboard',
|
| 33 |
+
'lamp', 'lawn_mower', 'leopard', 'lion', 'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain',
|
| 34 |
+
'mouse', 'mushroom', 'oak_tree', 'orange', 'orchid', 'otter', 'palm_tree', 'pear', 'pickup_truck', 'pine_tree',
|
| 35 |
+
'plain', 'plate', 'poppy', 'porcupine', 'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket',
|
| 36 |
+
'rose', 'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake', 'spider',
|
| 37 |
+
'squirrel', 'streetcar', 'sunflower', 'sweet_pepper', 'table', 'tank', 'telephone', 'television', 'tiger', 'tractor',
|
| 38 |
+
'train', 'trout', 'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman', 'worm'
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# 3. Fonction de prétraitement + prédiction
|
| 42 |
+
def predict(image):
|
| 43 |
+
image = image.resize((32, 32)) # Redimensionner à 32x32
|
| 44 |
+
image_array = np.array(image) / 255.0 # Normaliser
|
| 45 |
+
image_array = image_array.reshape(1, 32, 32, 3) # Ajouter batch dimension
|
| 46 |
+
predictions = model.predict(image_array)[0]
|
| 47 |
+
result = {class_names[i]: float(predictions[i]) for i in range(100)}
|
| 48 |
+
return result
|
| 49 |
+
|
| 50 |
+
# 4. Interface Gradio
|
| 51 |
+
gr.Interface(
|
| 52 |
+
fn=predict,
|
| 53 |
+
inputs=gr.Image(type="pil"),
|
| 54 |
+
outputs=gr.Label(num_top_classes=3),
|
| 55 |
+
title="Classificateur CIFAR-100",
|
| 56 |
+
description="Téléverse une image pour prédire sa classe parmi les 100 catégories CIFAR-100.",
|
| 57 |
+
theme='JohnSmith9982/small_and_pretty'
|
| 58 |
+
).launch()
|