| | |
| | from PIL import Image |
| |
|
| | def imageToArray(image_path): |
| |
|
| | width, height = 32, 32 |
| |
|
| | image = Image.open(image_path) |
| | image = image.resize((width, height)) |
| | |
| | image_array = np.asarray(image) |
| | image_array = image_array / 255.0 |
| |
|
| | |
| | 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 |
| |
|
| | |
| | model = tf.keras.models.load_model("CNN_model2.h5") |
| |
|
| | |
| | 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' |
| | ] |
| |
|
| | |
| | def predict(image): |
| | image = image.resize((32, 32)) |
| | image_array = np.array(image) / 255.0 |
| | image_array = image_array.reshape(1, 32, 32, 3) |
| | predictions = model.predict(image_array)[0] |
| | result = {class_names[i]: float(predictions[i]) for i in range(100)} |
| | return result |
| |
|
| | |
| | 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() |
| |
|