pokemon / app.py
zhawtranjoh's picture
Update app.py
2452110 verified
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
# Load your custom classification model
model_path = "kia_mlp_classification_pokemon2.weights.h5"
model_path = "kia_mlp_classification_pokemon2.keras"
model = tf.keras.models.load_model(model_path)
labels = ['Pikachu', 'Psyduck', 'Pidgey']
# Define classification function
def predict_classification(image):
# Preprocess image
image = Image.fromarray(image.astype('uint8'), 'RGB') # Convert numpy array to RGB PIL image
image = image.resize((224, 224)) # Resize the image to 224x224
image = np.array(image) / 255.0 # Scale pixel values to [0, 1]
# Predict
prediction = model.predict(np.array([image])) # Make sure to add a batch dimension
confidence = {labels[i]: float(np.round(prediction[0][i], 2)) for i in range(3)}
return confidence
# Create Gradio interface
input_image = gr.Image()
output_text = gr.Textbox(label="Predicted Value")
interface = gr.Interface(
fn=predict_classification,
inputs=input_image,
outputs=gr.Label(),
examples=["./pokemon/pikachu/i1.png", "./pokemon/psyduck/p2.png", "./pokemon/pidgey/pi1.png", "./pokemon/pikachu/i2.png", "./pokemon/psyduck/p1.jpg", "./pokemon/pidgey/pi2.jpg","./pokemon/psyduck/p3.jpg"],
description="Upload or select an image to classify as Pikachu, Psyduck, or Pidgey."
)
interface.launch()