Spaces:
Sleeping
Sleeping
File size: 957 Bytes
3a6e26f c6000df cc40d38 3a6e26f 291455c 3a6e26f 3df1d3f 3a6e26f 52a4c27 3a6e26f 0e71c42 3a6e26f 99a6c89 524b97e 1f3d0a5 1a9f609 3a6e26f 0f5f257 | 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 27 28 29 | import gradio as gr
import numpy as np
import tensorflow as tf
import json
import cv2
from os.path import dirname, realpath, join
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):
img = cv2.resize(img, (224,224))
arr = np.expand_dims(img, axis=0)
arr = tf.keras.applications.mobilenet.preprocess_input(arr)
prediction = mobile_net.predict(arr).flatten()
return {labels[i]:float(prediction[i]) for i in range(1000)}
iface = gr.Interface(
image_classifier,
gr.Image(height=224, width=224),
gr.Label(num_top_classes = 3),
examples=[
['Komodo_dragon.jpg'],['tiger_shark.jpg'],['tench.jpg'],['hair_slide.jpg']
],
example_labels = ['Komodo_dragon','tiger_shark','tench','hair_slide']
)
if __name__ == '__main__':
iface.launch(share=True) |