File size: 1,504 Bytes
c71f4db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np

# Load the pre-trained Fashion MNIST model
model_path = "kia_fashion_mnist_keras_model.h5"
model = tf.keras.models.load_model(model_path)

# Labels for Fashion MNIST
labels = [
    'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
    'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'
]

def predict_fashion(image):
    # Convert image to grayscale if not already and resize
    image = Image.fromarray(image).convert('L').resize((28, 28))
    # Normalize the image
    image = np.array(image) / 255.0
    # Reshape for model input
    image = image.reshape(1, 28, 28, 1)

    # Make a prediction
    predictions = model.predict(image)
    prediction = np.argmax(predictions, axis=1)[0]
    confidence = np.max(predictions)

    # Prepare the output
    result = f"Predicted category: {labels[prediction]} with confidence: {confidence:.2f}"
    return result

# Create Gradio interface
input_image = gr.Image(image_mode='L')
output_label = gr.Label()
interface = gr.Interface(fn=predict_fashion,
                         inputs=input_image,
                         outputs=output_label,
                         examples=["images/0.png", "images/1.png", "images/2.png", "images/3.png"],
                         title="Fashion MNIST Classifier",
                         description="Drag and drop an image or select an example below to predict the fashion category.")

# Launch the interface
interface.launch()