Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
from PIL import Image | |
model_path = "transfer_learning_xception.keras" | |
model = tf.keras.models.load_model(model_path) | |
# Define the core prediction function | |
def predict_dog(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, 2) | |
# Separate the probabilities for each class | |
p_germanshepherd = prediction[0][0] | |
p_goldenretriever = prediction[0][1] | |
p_husky = prediction[0][2] | |
p_poodle = prediction[0][3] | |
p_rottwiler = prediction[0][4] | |
p_shibainu = prediction[0][5] | |
return {'german shepherd': p_germanshepherd, 'goldenretriever': p_goldenretriever, 'husky': p_husky, 'poodle': p_poodle, 'rottweiler': p_rottwiler, 'shiba inu': p_shibainu} | |
# Create the Gradio interface | |
input_image = gr.Image() | |
iface = gr.Interface( | |
fn=predict_dog, | |
inputs=input_image, | |
outputs=gr.Label(), | |
examples=["img/german shepherd_1.jpg", "img/german shepherd_24.jpg", "img/german shepherd_36.jpg", "img/german shepherd_84.jpg", "img/golden retriever_10.jpg", "img/golden retriever_23.jpg", "img/golden retriever_28.jpg", "img/golden retriever_75.jpg", "img/husky_4.jpg", "img/husky_12.jpg", "img/husky_21.jpg", "img/husky_77.jpg", "img/husky_123.jpg", "img/poodle13.jpg", "img/poodle32.jpg", "img/poodle36.jpg", "img/poodle100.jpg", "img/rottwiler_1.jpg", "img/rottwiler_12.jpg", "img/rottwiler_52.jpg", "img/rottwiler_55.jpg", "img/rottwiler_167.jpg", "img/shiba inu_2.jpg", "img/shiba inu_45.jpg", "img/shiba inu_115.jpg", "img/shiba inu_161.jpg"], | |
description="Dogs" | |
) | |
iface.launch() | |