File size: 1,127 Bytes
f7dfb0f
9c52842
259984c
f7dfb0f
9c52842
cc1b998
 
f7dfb0f
d995000
259984c
 
9c52842
ae16aa7
9c52842
 
 
ae16aa7
9c52842
 
 
 
 
 
ae16aa7
 
 
 
 
 
 
9c52842
abbfcd2
9c52842
ae16aa7
 
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
import gradio as gr
import tensorflow as tf
import numpy as np

num_classes = 200
IMG_HEIGHT = 300
IMG_WIDTH = 300

with open("classlabel.txt", 'r') as file:
    CLASS_LABEL = [x.strip() for x in file.readlines()]

def normalize_image(img):
    img = tf.cast(img, tf.float32) / 255.
    img = tf.image.resize(img, (IMG_HEIGHT, IMG_WIDTH), method='bilinear')
    return img

def predict_top_classes(img, num_top_classes=5):
    img = img.convert('RGB')
    img_data = normalize_image(img)
    x = np.array(img_data)
    x = np.expand_dims(x, axis=0)
    temp = model.predict(x)

    top_class_indices = np.argpartition(temp, -num_top_classes)[-num_top_classes:]
    top_class_indices = top_class_indices[np.argsort(temp[0, top_class_indices])[::-1]]

    top_classes = [CLASS_LABEL[i] for i in top_class_indices]
    top_probabilities = [temp[0, i] for i in top_class_indices]

    return dict(zip(top_classes, top_probabilities))

model = tf.keras.models.load_model("Xception.h5")

interface = gr.Interface(predict_top_classes, gr.inputs.Image(type='pil'), outputs='dictionary', args={'num_top_classes': 5})
interface.launch()