Spaces:
Sleeping
Sleeping
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) |