File size: 1,561 Bytes
bc70419
a4e6f8e
bc70419
 
 
 
 
 
 
 
f3ccd30
a4e6f8e
bc70419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0935054
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
42
43
44
45
46
47
48
49
50
import tensorflow as tf
from tensorflow import keras
import gradio as gr
import numpy as np
import cv2
import os

classes = ["Abyssinian", "Bengal", "Birman", "Bombay", "British Shorthair", "Egyptian Mau", "Maine Coon", "Persian", "Ragdoll", "Russian Blue", "Siamese", "Sphynx"]
example_images = ["examples/" + f for f in os.listdir("examples")]
img_size = 400

model = tf.keras.models.load_model("CatClassifier")

def model_predict(image):
    image = cv2.resize(image, (img_size, img_size))
    image = np.expand_dims(image, axis=0)

    predictions = model.predict(image)
    predictions = predictions[0]

    predicted_class_index = np.argmax(predictions)
    predicted_class = classes[predicted_class_index]
    pred_dict = {}

    for i in range(len(classes)):
        pred_dict[classes[i]] = predictions[i]

    return predicted_class, pred_dict


def predict_breed(image):
    if image is None:
        return "Please attach an image first!", None
    
    return model_predict(image)

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
          image_input = gr.Image(label="Cat Image")
          run_button = gr.Button(variant="primary")
          examples = gr.Examples(example_images,inputs=image_input)
        with gr.Column():
          breed_output = gr.Text(label="Predicted Breed", interactive=False)
          predict_labels = gr.Label(label="Class Probabilties")  
          
    run_button.click(fn=predict_breed, inputs=image_input, outputs=[breed_output, predict_labels])

if __name__ == "__main__":
    demo.launch()