Spaces:
Sleeping
Sleeping
File size: 3,131 Bytes
d618a2a c1dd438 c3f03ee c1dd438 30b097d c1dd438 d618a2a 3abe170 c1dd438 3abe170 d2a3ed8 3abe170 47eb9f4 6032fa4 4cc329e 5ef8553 c1dd438 2c09734 c1dd438 d618a2a 5ef8553 a77b86d 5ef8553 a77b86d 5ef8553 2558f6f ad41d3e 2558f6f 5ef8553 47eb9f4 b8bf193 2558f6f ad41d3e 2558f6f d618a2a 0f619a1 4c7aee7 2558f6f d618a2a 2558f6f d618a2a 2558f6f |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import gradio as gr
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array,load_img
import numpy as np
from PIL import Image
import os
# Load your model and tokenizer
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']
}
hierarchical_models = {}
model_path = r"inceptionv3_class.h5"
hierarchical_models['class'] = load_model(model_path)
def load_and_preprocess_image(image, target_size=(224, 224)):
image = image.resize(target_size)
img_array = img_to_array(image)
img_array = np.expand_dims(img_array, axis=0)
img_array = tf.keras.applications.mobilenet_v2.preprocess_input(img_array)
return img_array
def predict(image):
results = {}
image_array = load_and_preprocess_image(image)
# Predict class level
class_preds = hierarchical_models['class'].predict(image_array)
print(class_preds)
class_idx = np.argmax(class_preds)
print(class_idx)
class_label = labels['class'][class_idx]
class_confidence = class_preds[0][class_idx]
class_level = f"{class_label} ({class_confidence*100:.2f}%)"
# Predict species level
hierarchical_models[class_label] = load_model(f"inceptionv3_{class_label}.h5")
species_preds = hierarchical_models[class_label].predict(image_array)
species_idx = np.argmax(species_preds)
species_label = labels[class_label][species_idx]
species_confidence = species_preds[0][species_idx]
species_level = f"{species_label} ({species_confidence*100:.2f}%)"
return class_level,species_level
# Sample images (you can add paths to images here)
# sample_images = [
# ("Sample Amphibia", "path/to/amphibia.jpg"),
# ("Sample Aves", "path/to/aves.jpg"),
# ("Sample Mammalia", "path/to/mammalia.jpg"),
# # Add more sample images as needed
# ]
# Create Gradio interface
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=[gr.Label(label="class_label"), gr.Label(label="species_label")],
# examples=sample_images,
title="Image Classification",
description="Upload an image to classify it into species and class level.",
)
# Launch the interface
iface.launch()
|