|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
from PIL import Image |
|
|
import numpy as np |
|
|
|
|
|
model_path = "pokemon_classifier_model.keras" |
|
|
model = tf.keras.models.load_model(model_path) |
|
|
|
|
|
|
|
|
labels = ['Pikachu', 'Sandshrew', 'Squirtle'] |
|
|
|
|
|
def predict_image(image): |
|
|
|
|
|
image = Image.fromarray(image.astype('uint8')) |
|
|
image = image.resize((224, 224)) |
|
|
image = np.array(image) / 255.0 |
|
|
|
|
|
|
|
|
if image.ndim == 2: |
|
|
image = np.stack((image,)*3, axis=-1) |
|
|
|
|
|
prediction = model.predict(image[None, ...]) |
|
|
confidences = {labels[i]: float(prediction[0][i]) for i in range(len(labels))} |
|
|
return confidences |
|
|
|
|
|
|
|
|
input_image = gr.Image() |
|
|
output_text = gr.Textbox(label="Predicted Value") |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict_image, |
|
|
inputs=input_image, |
|
|
outputs=gr.Label(), |
|
|
title="Pokémon Classifier", |
|
|
examples=["images/pikachu.png", "images/squirtle.png", "images/sandshrew.png"], |
|
|
description="Upload an image of Pikachu, Sandshrew, or Squirtle and the classifier will predict which one it is." |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
iface.launch() |
|
|
|