selfstudy / app.py
sonah5009's picture
Update app.py
bc53a46 verified
raw
history blame contribute delete
No virus
1.4 kB
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "Abra-vs-Pikachu-vs-Zubat-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))
# Convert numpy array to PIL image
image = Image.fromarray(image.astype('uint8'))
# resize the image to 28x28 and converts it to gray scale
image = image.resize((150, 150))
image = np.array(image)
image = np.expand_dims(image, axis=0) # same as image[None, ...]
# Predict
prediction = model.predict(image)
# Apply sigmoid to get probabilities
prediction_prob = tf.sigmoid(prediction).numpy()
# Extract probabilities for Abra and Pikachu and Zubat
p_Abra = round(prediction_prob[0][0], 2)
p_Pikachu = round(prediction_prob[0][1], 2)
p_Zubat = round(prediction_prob[0][2], 2)
return {'Abra': p_Abra, 'Pikachu': p_Pikachu, 'Zubat': p_Zubat}
# Create the Gradio interface
input_image = gr.Image()
iface = gr.Interface(
fn=predict_pokemon,
inputs=input_image,
outputs=gr.Label(),
examples=["Abra1.png", "Abra2.jpg", "Abra3.jpg", "Pikachu1.jpg",
"Pikachu2.png", "Pikachu3.jpg", "Zubat1.jpg", "Zubat2.jpg", "Zubat3.jpg"],
description="Image classification pokemon!!!πŸ˜ƒπŸ˜†.")
iface.launch()