plants / app.py
besar00's picture
Upload app.py
336e7e4 verified
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "Xception.keras"
model = tf.keras.models.load_model(model_path)
# Define the core prediction function
def predict_pokemon(image):
# Preprocess image
print(type(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, ...]
# Predict
prediction = model.predict(image)
# Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability
# we could also change the output layer to dense(1, activation='sigmoid')
prediction = np.round(prediction, 2)
# Separate the probabilities for each class
P_aloevera = prediction[0][0] # Probability for class 'abra'
P_curcuma = prediction[0][1] # Probability for class 'beedrill'
p_guava = prediction[0][2] # Probability for class 'sandshrew'
return {'aloevera': P_aloevera, 'curcuma': P_curcuma, 'guava': p_guava}
# Create the Gradio interface
input_image = gr.Image()
iface = gr.Interface(
fn=predict_pokemon,
inputs=input_image,
outputs=gr.Label(),
examples=["images/aloevera0.jpg", "images/curcuma51.jpg", "images/guava10.jpg"],
description="A simple mlp classification model for image classification using the mnist dataset.")
iface.launch(share=True)