Spaces:
Runtime error
Runtime error
labels = ['aircraft carrier', | |
'airplane', | |
'alarm clock', | |
'ambulance', | |
'angel', | |
'animal migration', | |
'ant', | |
'anvil', | |
'apple', | |
'arm', | |
'asparagus', | |
'axe', | |
'backpack', | |
'banana', | |
'bandage', | |
'barn', | |
'baseball bat', | |
'baseball', | |
'basket', | |
'basketball', | |
'bat', | |
'bathtub', | |
'beach', | |
'bear', | |
'beard', | |
'bed', | |
'bee', | |
'belt', | |
'bench', | |
'bicycle', | |
'binoculars', | |
'bird', | |
'birthday cake', | |
'blackberry', | |
'blueberry', | |
'book', | |
'boomerang', | |
'bottlecap', | |
'bowtie', | |
'bracelet', | |
'brain', | |
'bread', | |
'bridge', | |
'broccoli', | |
'broom', | |
'bucket', | |
'bulldozer', | |
'bus', | |
'bush', | |
'butterfly', | |
'cactus', | |
'cake', | |
'calculator', | |
'calendar', | |
'camel', | |
'camera', | |
'camouflage', | |
'campfire', | |
'candle', | |
'cannon', | |
'canoe', | |
'car', | |
'carrot', | |
'castle', | |
'cat', | |
'ceiling fan', | |
'cell phone', | |
'cello', | |
'chair', | |
'chandelier', | |
'church', | |
'circle', | |
'clarinet', | |
'clock', | |
'cloud', | |
'coffee cup', | |
'compass', | |
'computer', | |
'cookie', | |
'cooler', | |
'couch', | |
'cow', | |
'crab', | |
'crayon', | |
'crocodile', | |
'crown', | |
'cruise ship', | |
'cup', | |
'diamond', | |
'dishwasher', | |
'diving board', | |
'dog', | |
'dolphin', | |
'donut', | |
'door', | |
'dragon', | |
'dresser', | |
'drill', | |
'drums', | |
'duck', | |
'dumbbell', | |
'ear', | |
'elbow', | |
'elephant', | |
'envelope', | |
'eraser', | |
'eye', | |
'eyeglasses', | |
'face', | |
'fan', | |
'feather', | |
'fence', | |
'finger', | |
'fire hydrant', | |
'fireplace', | |
'firetruck', | |
'fish', | |
'flamingo', | |
'flashlight', | |
'flip flops', | |
'floor lamp', | |
'flower', | |
'flying saucer', | |
'foot', | |
'fork', | |
'frog', | |
'frying pan', | |
'garden hose', | |
'garden', | |
'giraffe', | |
'goatee', | |
'golf club', | |
'grapes', | |
'grass', | |
'guitar', | |
'hamburger', | |
'hammer', | |
'hand', | |
'harp', | |
'hat', | |
'headphones', | |
'hedgehog', | |
'helicopter', | |
'helmet', | |
'hexagon', | |
'hockey puck', | |
'hockey stick', | |
'horse', | |
'hospital', | |
'hot air balloon', | |
'hot dog', | |
'hot tub', | |
'hourglass', | |
'house plant', | |
'house', | |
'hurricane', | |
'ice cream', | |
'jacket', | |
'jail', | |
'kangaroo', | |
'key', | |
'keyboard', | |
'knee', | |
'knife', | |
'ladder', | |
'lantern', | |
'laptop', | |
'leaf', | |
'leg', | |
'light bulb', | |
'lighter', | |
'lighthouse', | |
'lightning', | |
'line', | |
'lion', | |
'lipstick', | |
'lobster', | |
'lollipop', | |
'mailbox', | |
'map', | |
'marker', | |
'matches', | |
'megaphone', | |
'mermaid', | |
'microphone', | |
'microwave', | |
'monkey', | |
'moon', | |
'mosquito', | |
'motorbike', | |
'mountain', | |
'mouse', | |
'moustache', | |
'mouth', | |
'mug', | |
'mushroom', | |
'nail', | |
'necklace', | |
'nose', | |
'ocean', | |
'octagon', | |
'octopus', | |
'onion', | |
'oven', | |
'owl', | |
'paint can', | |
'paintbrush', | |
'palm tree', | |
'panda', | |
'pants', | |
'paper clip', | |
'parachute', | |
'parrot', | |
'passport', | |
'peanut', | |
'pear', | |
'peas', | |
'pencil', | |
'penguin', | |
'piano', | |
'pickup truck', | |
'picture frame', | |
'pig', | |
'pillow', | |
'pineapple', | |
'pizza', | |
'pliers', | |
'police car', | |
'pond', | |
'pool', | |
'popsicle', | |
'postcard', | |
'potato', | |
'power outlet', | |
'purse', | |
'rabbit', | |
'raccoon', | |
'radio', | |
'rain', | |
'rainbow', | |
'rake', | |
'remote control', | |
'rhinoceros', | |
'rifle', | |
'river', | |
'roller coaster', | |
'rollerskates', | |
'sailboat', | |
'sandwich', | |
'saw', | |
'saxophone', | |
'school bus', | |
'scissors', | |
'scorpion', | |
'screwdriver', | |
'sea turtle', | |
'see saw', | |
'shark', | |
'sheep', | |
'shoe', | |
'shorts', | |
'shovel', | |
'sink', | |
'skateboard', | |
'skull', | |
'skyscraper', | |
'sleeping bag', | |
'smiley face', | |
'snail', | |
'snake', | |
'snorkel', | |
'snowflake', | |
'snowman', | |
'soccer ball', | |
'sock', | |
'speedboat', | |
'spider', | |
'spoon', | |
'spreadsheet', | |
'square', | |
'squiggle', | |
'squirrel', | |
'stairs', | |
'star', | |
'steak', | |
'stereo', | |
'stethoscope', | |
'stitches', | |
'stop sign', | |
'stove', | |
'strawberry', | |
'streetlight', | |
'string bean', | |
'submarine', | |
'suitcase', | |
'sun', | |
'swan', | |
'sweater', | |
'swing set', | |
'sword', | |
'syringe', | |
't-shirt', | |
'table', | |
'teapot', | |
'teddy-bear', | |
'telephone', | |
'television', | |
'tennis racquet', | |
'tent', | |
'The Eiffel Tower', | |
'The Great Wall of China', | |
'The Mona Lisa', | |
'tiger', | |
'toaster', | |
'toe', | |
'toilet', | |
'tooth', | |
'toothbrush', | |
'toothpaste', | |
'tornado', | |
'tractor', | |
'traffic light', | |
'train', | |
'tree', | |
'triangle', | |
'trombone', | |
'truck', | |
'trumpet', | |
'umbrella', | |
'underwear', | |
'van', | |
'vase', | |
'violin', | |
'washing machine', | |
'watermelon', | |
'waterslide', | |
'whale', | |
'wheel', | |
'windmill', | |
'wine bottle', | |
'wine glass', | |
'wristwatch', | |
'yoga', | |
'zebra', | |
'zigzag'] | |
from PIL import Image | |
import numpy as np | |
import tensorflow as tf | |
imgs = [] | |
def predict(img): | |
img = img['layers'][0][:,:,3] | |
imgs.append(img) | |
image_pil = Image.fromarray(img) | |
# Resize the image using PIL | |
new_size = (28, 28) # Set the new size (e.g., 100x100) | |
resized_image_pil = image_pil.resize(new_size) | |
# Convert the resized PIL image back to a NumPy array | |
resized_image_np = np.array(resized_image_pil) | |
# imgs.append(img) | |
img = np.array(resized_image_np).reshape(1, 28, 28,1) | |
out = model.predict(img); | |
top5 = np.argsort(out[0])[::-1][:5] | |
label = labels | |
# Create a dictionary with label-probability pairs for the top 5 predictions | |
probabilities = {label[i]: float(out[0][i]) for i in top5} | |
return probabilities | |
model = tf.keras.models.load_model('quickdraw.h5', compile=False) | |
import gradio as gr | |
gr.Interface(fn=predict, | |
inputs=gr.Sketchpad(), | |
outputs="label", | |
live=True).launch() |