Spaces:
Runtime error
Runtime error
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
from PIL import Image | |
model_path = "pokemon-model_transferlearning.keras" | |
model = tf.keras.models.load_model(model_path) | |
# Define the core prediction function | |
def predict_pokemon(image): | |
# Preprocess image | |
print(type(image)) | |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image | |
image = image.resize((150, 150)) # Resize the image to 150x150 | |
image = np.array(image) | |
image = np.expand_dims(image, axis=0) # Expand dimensions to create batch size of 1 | |
# Predict | |
prediction = model.predict(image) | |
# Assuming the model's output layer uses softmax activation and there are three outputs | |
prediction = prediction.flatten() | |
predictions = np.round(prediction, 2) # Flatten the predictions and round them | |
# Separate the probabilities for each class | |
p_clefairy = predictions[0] # Probability for Clefairy | |
p_snorlax = predictions[1] # Probability for Snorlax | |
p_squirtle = predictions[2] # Probability for Squirtle | |
return { | |
'clefairy': p_clefairy, | |
'snorlax': p_snorlax, | |
'squirtle': p_squirtle | |
} | |
# Create the Gradio interface | |
input_image = gr.Image() | |
iface = gr.Interface( | |
fn=predict_pokemon, | |
inputs=input_image, | |
outputs=gr.Label(), | |
examples=[r"images\squirtle\0.png", r"images\squirtle\00000003.png", r"images\snorlax\00000003.png", | |
r"images\squirtle\4.png", r"images\snorlax\00000017.png", | |
r"images\snorlax\4eb284a359474f0cb43ffe82d03abbe9.jpg", | |
r"images\clefairy\5fb558d9c96e4e469100636eb6c8627e.jpg", | |
r"images\clefairy\00000024.png", r"images\clefairy\00000103.jpg"], | |
description="A simple mlp classification model for image classification using the mnist dataset.") | |
iface.launch() | |