File size: 2,739 Bytes
ec008e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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()