Pokemon / app.py
xeniabuerli's picture
Upload app.py
be8a2f0 verified
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=["test/Squirtle1.png", "test/Squirtle2.jpg", "test/Squirtle3.jpg",
"test/Snorlax1.jpg", "test/Snorlax2.jpg", "test/Snorlax3.png",
"test/Clefairy1.png", "test/Clefairy2.png", "test/Clefairy3.png"],
description="A simple mlp classification model for image classification using the mnist dataset.")
iface.launch()