|
import tensorflow as tf |
|
import numpy as np |
|
import gradio as gr |
|
from PIL import Image |
|
|
|
|
|
model = tf.keras.models.load_model('cifar10_cnn_model.keras') |
|
|
|
|
|
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] |
|
|
|
|
|
def preprocess_image(image): |
|
image = image.resize((32, 32)) |
|
image = np.array(image) / 255.0 |
|
image = np.expand_dims(image, axis=0) |
|
return image |
|
|
|
|
|
def classify_image(image): |
|
preprocessed_image = preprocess_image(image) |
|
predictions = model.predict(preprocessed_image) |
|
predicted_class = class_names[np.argmax(predictions)] |
|
confidence = np.max(predictions) |
|
return f"Prediction: {predicted_class} (Confidence: {confidence:.2f})" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=classify_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text", |
|
title="CIFAR-10 Image Classifier", |
|
description="Upload an image of a CIFAR-10 category (airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck), and the model will classify it." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|