Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,33 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
inputs=gr.inputs.Image(label="Upload hot dog candidate", type="filepath"),
|
| 13 |
-
outputs=gr.outputs.Label(num_top_classes=2),
|
| 14 |
-
title="Hot Dog? Or Not?",
|
| 15 |
-
).launch()
|
|
|
|
| 1 |
+
# Demo: (Image) -> (Label)
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import numpy as np
|
| 6 |
+
import json
|
| 7 |
+
from os.path import dirname, realpath, join
|
| 8 |
+
|
| 9 |
+
# Load human-readable labels for ImageNet.
|
| 10 |
+
current_dir = dirname(realpath(__file__))
|
| 11 |
+
with open(join(current_dir, "imagenet_labels.json")) as labels_file:
|
| 12 |
+
labels = json.load(labels_file)
|
| 13 |
|
| 14 |
+
mobile_net = tf.keras.applications.MobileNetV2()
|
| 15 |
+
def image_classifier(im):
|
| 16 |
+
arr = np.expand_dims(im, axis=0)
|
| 17 |
+
arr = tf.keras.applications.mobilenet.preprocess_input(arr)
|
| 18 |
+
prediction = mobile_net.predict(arr).flatten()
|
| 19 |
+
return {labels[i]: float(prediction[i]) for i in range(1000)}
|
| 20 |
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
image_classifier,
|
| 23 |
+
gr.inputs.Image(shape=(224, 224)),
|
| 24 |
+
gr.outputs.Label(num_top_classes=3),
|
| 25 |
+
capture_session=True,
|
| 26 |
+
interpretation="default",
|
| 27 |
+
examples=[
|
| 28 |
+
["images/cheetah1.jpg"],
|
| 29 |
+
["images/lion.jpg"]
|
| 30 |
+
])
|
| 31 |
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|