File size: 2,782 Bytes
04afb2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb99efc
04afb2e
 
b2beb6c
 
 
 
 
04afb2e
c6ad28e
 
f6ad416
 
 
04afb2e
 
bb99efc
c6ad28e
bb99efc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# 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/wolfes.jpg","images/scared-Cat.jpg","images/one-Does-Not-Simply.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()