| import gradio as gr |
| import numpy as np |
| import tensorflow as tf |
| import json |
| from os.path import dirname, realpath, join |
| import matplotlib.pyplot as plt |
|
|
| current_dir = dirname(realpath(__file__)) |
| with open(join(current_dir), 'image_labels.json') as labels_file: |
| labels=json.load(labels_file) |
|
|
| mobile_net = tf.keras.applications.MobileNetV2() |
| def image_classifier(img): |
| arr = np.expand_dims(img, axis=0) |
| arr = tf.keras.applications.mobilenet.preprocess_input(arr) |
| prediction = mobile_net.prediction(arr).flatten() |
| return {labels[i]:float(prediction[i]) for i in range(1000)} |
| iface = gr.Interface( |
| image_classifier, |
| gr.inputs.Image(height=224, width=224), |
| gr.outputs.Label(num_top_classes=3), |
| capture_session=True, |
| interpretation='default', |
| ) |
| if __name__ == '__main__': |
| iface.launch(share=True) |