Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
from tensorflow.keras.models import load_model | |
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input | |
from tensorflow.keras.preprocessing.image import img_to_array | |
import numpy as np | |
from PIL import Image | |
from io import BytesIO | |
import os | |
labels = { | |
'class': ['amphibia', 'aves', 'invertebrates', 'lacertilia', 'mammalia', 'serpentes', 'testudines'], | |
'serpentes': ["Butler's Gartersnake", "Dekay's Brownsnake", 'Eastern Gartersnake', 'Eastern Hog-nosed snake', 'Eastern Massasauga', 'Eastern Milksnake', 'Eastern Racer Snake', 'Eastern Ribbonsnake', 'Gray Ratsnake', "Kirtland's Snake", 'Northern Watersnake', 'Plains Gartersnake', 'Red-bellied Snake', 'Smooth Greensnake'], | |
'mammalia': ['American Mink', 'Brown Rat', 'Eastern Chipmunk', 'Eastern Cottontail', 'Long-tailed Weasel', 'Masked Shrew', 'Meadow Jumping Mouse', 'Meadow Vole', 'N. Short-tailed Shrew', 'Raccoon', 'Star-nosed mole', 'Striped Skunk', 'Virginia Opossum', 'White-footed Mouse', 'Woodchuck', 'Woodland Jumping Mouse'], | |
'aves': ['Common Yellowthroat', 'Gray Catbird', 'Indigo Bunting', 'Northern House Wren', 'Song Sparrow', 'Sora'], | |
'amphibia': ['American Bullfrog', 'American Toad', 'Green Frog', 'Northern Leopard Frog'] | |
} | |
def preprocess_image(image): | |
img = image.resize((224, 224)) | |
img_array = img_to_array(img) | |
img_array = np.expand_dims(img_array, axis=0) | |
img_array = preprocess_input(img_array) | |
print(tf.__version__) | |
return img_array, img | |
def predict(img): | |
img_array, resized_img = preprocess_image(img) | |
model = load_model("inceptionv3_class.h5") | |
preds = model.predict(img_array) | |
decoded_preds = np.argmax(preds) | |
return resized_img, labels['class'][decoded_preds] | |
iface = gr.Interface( | |
fn=predict, | |
inputs=gr.Image(type="pil"), | |
outputs=[gr.Image(type="pil"), gr.Label(label="species_label")], | |
title="Animal Classifier" | |
) | |
iface.launch() | |