File size: 834 Bytes
d67d257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e524508
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)