File size: 765 Bytes
056381f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input, decode_predictions
from PIL import Image
import numpy as np
import gradio as gr

# Load pre-trained MobileNetV2 model
model = MobileNetV2(weights='imagenet')

def classify_image(inp):
    inp = inp.reshape((-1, 224, 224, 3))
    inp = preprocess_input(inp)
    prediction = model.predict(inp).flatten()
    return decode_predictions(np.array([prediction]), top=5)[0]

# Define Gradio interface
image_input = gr.inputs.Image(shape=(224, 224))
label_output = gr.outputs.Label(num_top_classes=5)

interface = gr.Interface(fn=classify_image, inputs=image_input, outputs=label_output)

# Launch the application
if __name__ == "__main__":
    interface.launch()