CoolFace
Apppublic

soheiltp/Image-Classifier

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py37 linesDownload Raw Back to root
1# Demo: (Image) -> (Label)2 3import gradio as gr4import tensorflow as tf5import numpy as np6import json7from os.path import dirname, realpath, join8 9# Load human-readable labels for ImageNet.10current_dir = dirname(realpath(__file__))11with open(join(current_dir, "imagenet_labels.json")) as labels_file:12    labels = json.load(labels_file)13 14mobile_net = tf.keras.applications.MobileNetV2()15def 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 21iface = 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        ["cheetah1.jpg"],29        ["lion.jpg"],30        ["straw.png"],31        ["azadi.jpg"]32    ])33 34 35if __name__ == "__main__":36    iface.launch(share=True)37