| import gradio as gr |
| import numpy as np |
| from keras.applications import MobileNetV2 |
| from keras.applications.mobilenet_v2 import preprocess_input, decode_predictions |
| from keras.utils import img_to_array |
| from PIL import Image |
|
|
| |
| model = MobileNetV2(weights="imagenet") |
|
|
| def predict(image): |
| |
| img = image.resize((224, 224)) |
| arr = img_to_array(img) |
| arr = np.expand_dims(arr, axis=0) |
| arr = preprocess_input(arr) |
|
|
| preds = model.predict(arr) |
| top5 = decode_predictions(preds, top=5)[0] |
|
|
| return {label: float(prob) for (_, label, prob) in top5} |
|
|
| demo = gr.Interface( |
| fn=predict, |
| inputs=gr.Image(type="pil"), |
| outputs=gr.Label(num_top_classes=5), |
| title="Image Classifier (MobileNetV2 Transfer Learning)", |
| description="Upload an image and the model predicts what it is using Keras MobileNetV2 pretrained on ImageNet.", |
| ) |
|
|
| demo.launch() |