Spaces:
Sleeping
Sleeping
File size: 1,397 Bytes
668deeb 2452110 668deeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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() |