Bird-Classifier / app.py
fischjos's picture
Update app.py
d841722 verified
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "Bird_transfer_learning_NASNetLarge.keras"
model = tf.keras.models.load_model(model_path)
# Define the core prediction function
def predict_bird(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)
# No need to apply sigmoid, as the output layer already uses softmax
# Convert the probabilities to rounded values
prediction = np.round(prediction, 3)
# Separate the probabilities for each class
p_azure_jay = prediction[0][0] # Probability for class 'articuno'
p_bald_eagle = prediction[0][1] # Probability for class 'moltres'
p_jandaya_parakeet = prediction[0][2] # Probability for class 'zapdos'
p_shoebill = prediction[0][3] # Probability for class 'zapdos'
return {'Azure Jay': p_azure_jay, 'Bald Eagle': p_bald_eagle, 'Jandaya Parakeet': p_jandaya_parakeet, 'Shoebill': p_shoebill}
# Create the Gradio interface
input_image = gr.Image()
iface = gr.Interface(
fn=predict_bird,
inputs=input_image,
outputs=gr.Label(),
examples=["images/Azure_jay_1.jpg", "images/Azure_jay_2.jpg", "images/Azure_jay_3.jpg", "images/Azure_jay_4.jpg", "images/Azure_jay_5.jpg", "images/Bald_eagle_1.jpg", "images/Bald_eagle_2.jpg", "images/Bald_eagle_3.jpg", "images/Bald_eagle_4.jpg", "images/Bald_eagle_5.jpg", "images/Jandaya_parakeet_1.jpg", "images/Jandaya_parakeet_2.jpg", "images/Jandaya_parakeet_3.jpg", "images/Jandaya_parakeet_4.jpg", "images/Jandaya_parakeet_5.jpg", "images/Shoebill_1.jpg", "images/Shoebill_2.jpg", "images/Shoebill_3.jpg", "images/Shoebill_4.jpg", "images/Shoebill_4.jpg"],
description="Welcome to the Bird classifier. Upload an image of an Azure Jay, Bald Eagle, Jandaya Parakeet or a Shoebill to classify!")
iface.launch()