dec6B / app.py
ProfessorPeter's picture
Create app.py
c581066
raw
history blame contribute delete
765 Bytes
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()