pokemon / app.py
hasvio01's picture
Update app.py
a8499ca verified
raw
history blame
1.91 kB
import tensorflow as tf
import gradio as gr
import numpy as np
import os
from PIL import Image
print(tf.__version__)
print(f"Current working directory: {os.getcwd()}")
print(f"Contents of model directory: {os.listdir('model')}")
model_path = 'model/transferlearning_pokemon.h5'
# Check if the model exists
if os.path.exists(model_path):
print(f"Model found at {model_path}")
try:
# Load the trained model
model = tf.keras.models.load_model(model_path)
print("Model loaded successfully.")
except Exception as e:
print(f"Error loading model: {e}")
else:
print(f"Model not found at {model_path}. Please check the path.")
# Define class names (make sure this matches the classes used during training)
class_names = ['Machamp', 'Raichu', 'Vulpix']
# Define the prediction function
def predict(image):
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
image = np.array(image)
image = np.expand_dims(image, axis=0) # same as image[None, ...]
predictions = model.predict(image)
predicted_class = np.argmax(predictions, axis=1)[0]
confidence = np.max(predictions)
return {class_names[predicted_class]: float(confidence)}
# Create a Gradio interface
input_image = gr.Image()
output_text = gr.Textbox(label="Predicted Value")
interface = gr.Interface(fn=predict,
inputs=input_image,
outputs=gr.Label(),
examples=["images/00000000.jpg", "images/00000001.jpg", "images/00000010.png", "images/00000017.jpg", "images/00000021.jpg", "images/00000067.jpg"],
description="A simple mlp classification model for image classification using the mnist dataset.")
# Launch the Gradio interface
interface.launch()