File size: 961 Bytes
feeed5b
 
 
 
 
 
 
9478d60
 
feeed5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9478d60
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
import numpy as np
import gradio as gr
import tensorflow as tf
from io import StringIO
from PIL import Image

labels = []
model = tf.keras.models.load_model('./models.h5')
with open("labels.txt") as f:
    for line in f:
        labels.append(line.replace('\n', ''))

def classify_image(inp):
    # Create a copy of the input array to avoid reference issues
    inp_copy = np.copy(inp)
    # Resize the input image to the expected shape (224, 224)
    inp_copy = Image.fromarray(inp_copy)
    inp_copy = inp_copy.resize((224, 224))
    inp_copy = np.array(inp_copy)
    inp_copy = inp_copy.reshape((-1, 224, 224, 3))
    inp_copy = tf.keras.applications.efficientnet.preprocess_input(inp_copy)
    prediction = model.predict(inp_copy).flatten()
    confidences = {labels[i]: float(prediction[i]) for i in range(90)}
    return confidences

demo = gr.Interface(classify_image, gr.Image(), gr.Label(num_top_classes=3))
if __name__ == "__main__":
    demo.launch()