selfstudy / my_app.py
sonah5009's picture
Upload 4 files
33c01df verified
raw
history blame contribute delete
No virus
1.51 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=["images/Abra1.png", "images/Abra2.jpg", "images/Abra3.jpg", "images/Pikachu1.jpg",
"images/Pikachu2.png", "images/Pikachu3.jpg", "images/Zubat1.jpg", "images/Zubat2.jpg", "images/Zubat3.jpg"],
description="A simple mlp classification model for image classification using the mnist dataset.")
iface.launch()