Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
from PIL import Image | |
import numpy as np | |
# Load your custom regression model | |
model_path = "mySpiritualAnimalResNet50V2.keras" | |
model = tf.keras.models.load_model(model_path) | |
model.summary() # Check if the model architecture loaded matches the expected one | |
labels = ['antelope', 'badger', 'bat', 'bear', 'bee', 'beetle', 'bison', 'boar', 'butterfly', 'cat', 'caterpillar', 'chimpanzee', 'cockroach', 'cow', 'coyote', 'crab', 'crow', 'deer', 'dog', 'dolphin', 'donkey', 'dragonfly', 'duck', 'eagle', 'elephant', 'flamingo', 'fly', 'fox', 'goat', 'goldfish', 'goose', 'gorilla', 'grasshopper', 'hamster', 'hare', 'hedgehog', 'hippopotamus', 'hornbill', 'horse', 'hummingbird', 'hyena', 'jellyfish', 'kangaroo', 'koala', 'ladybugs', 'leopard', 'lion', 'lizard', 'lobster', 'mosquito', 'moth', 'mouse', 'octopus', 'okapi', 'orangutan', 'otter', 'owl', 'ox', 'oyster', 'panda', 'parrot', 'pelecaniformes', 'penguin', 'pig', 'pigeon', 'porcupine', 'possum', 'raccoon', 'rat', 'red_panda', 'reindeer', 'rhinoceros', 'sandpiper', 'seahorse', 'seal', 'shark', 'sheep', 'snake', 'sparrow', 'squid', 'squirrel', 'starfish', 'swan', 'tiger', 'turkey', 'turtle', 'whale', 'wolf', 'wombat', 'woodpecker', 'zebra'] | |
# Define regression function | |
def predict_regression(image): | |
# Preprocess image | |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image | |
image = image.resize((150, 150)) | |
image = image.convert('RGB') # Ensure image is in RGB format | |
image = np.array(image) | |
image = np.expand_dims(image, axis=0) # Expand dimensions to match model input | |
print(image.shape) | |
# Predict | |
prediction = model.predict(image) # Get predictions | |
top_1_idx = np.argmax(prediction[0]) # Get the index of the highest confidence score | |
top_1_label = labels[top_1_idx] # Get the corresponding label | |
top_1_confidence = np.round(float(prediction[0][top_1_idx]), 2) # Get the confidence score | |
return {top_1_label: top_1_confidence} # Return only the top-1 result | |
# Create Gradio interface | |
input_image = gr.Image() | |
output_text = gr.Label() # Use gr.Label to show top-1 result | |
interface = gr.Interface(fn=predict_regression, | |
inputs=input_image, | |
outputs=output_text, # Change output to Label to display single label and confidence | |
examples=["images/bond.jpg", "images/cat.jpg", "images/kronk.jpg", "images/zebra.jpg", "images/dog.jpg", "images/johnson.jpg", "images/panda.jpg"], | |
description="You ever wonder what your spiritual Animal is? Well here's the answer.") | |
interface.launch() | |