|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
|
|
|
|
model = tf.keras.applications.MobileNetV2(weights="imagenet") |
|
|
|
|
|
def preprocess_image(image): |
|
image = tf.image.resize(image, (224, 224)) |
|
image = tf.keras.applications.mobilenet_v2.preprocess_input(image) |
|
return np.expand_dims(image, axis=0) |
|
|
|
|
|
def predict(image): |
|
image = preprocess_image(image) |
|
predictions = model.predict(image) |
|
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=1) |
|
return decoded_predictions[0][0][1] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.components.Image(type="numpy"), |
|
outputs="text", |
|
live=True |
|
) |
|
|
|
|
|
iface.launch() |
|
|
|
|