pokemon / app.py
JJjdjka's picture
Update app.py
72c5802
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "pokemon_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 28x28 and converts it to gray scale
image = np.array(image)
image = np.expand_dims(image, axis=0) # same as image[None, ...]
# Predict
prediction = model.predict(image)
# No need to apply sigmoid, as the output layer already uses softmax
# Convert the probabilities to rounded values
prediction = np.round(prediction, 2)
# Separate the probabilities for each class
p_charmander = prediction[0][0] # Probability for class 'charmander'
p_mewto = prediction[0][1] # Probability for class 'mewto'
p_squirtle = prediction[0][2] # Probability for class 'squirtle'
return {'charmander': p_charmander, 'mewto': p_mewto, 'squirtle': p_squirtle}
# Create the Gradio interface
input_image = gr.Image()
iface = gr.Interface(
fn=predict_pokemon,
inputs=input_image,
outputs=gr.Label(),
examples=["images/charmander1.png", "images/charmander2.png", "images/charmander3.jpg", "images/mewto.jpg", "images/mewto2.jpg", "images/mewto4.png", "images/squirtle1.png", "images/squirtle2.jpg", "images/squirtle3.jpg"],
description="TEST.")
iface.launch()