task2 / app.py
meixnan's picture
Upload app.py
03df8e7 verified
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "pokemons-model_transferlearning.keras"
model = tf.keras.models.load_model(model_path)
def predict_pokemons(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, ...]
prediction = model.predict(image)
# Convert the probabilities to rounded values
prediction = np.round(prediction, 2)
# Separate the probabilities for each class
p_bulbasaur = prediction[0][0]
p_dratini = prediction[0][1]
p_gengar = prediction[0][2]
return {'Bulbasaur': p_bulbasaur, 'Dratini': p_dratini, 'Gengar': p_gengar}
input_image = gr.Image()
iface = gr.Interface(
fn=predict_pokemons,
inputs=input_image,
outputs=gr.Label(),
examples=["images/bulbasaur1.png", "images/bulbasaur2.png", "images/dratini1.png", "images/dratini2.png", "images/dratini3.png", "images/gengar1.png", "images/gengar2.png", "images/gengar3.png"],
description="TEST.")
iface.launch()