File size: 2,089 Bytes
3757755 |
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 |
import birder
import numpy as np
from birder.inference.classification import infer_image
from huggingface_hub import HfApi
import gradio as gr
def get_birder_classification_models():
api = HfApi()
models = api.list_models(author="birder-project", tags="image-classification")
return [model.modelId.split("/")[-1] for model in models]
def load_model_and_predict(image, model_name):
try:
(net, class_to_idx, signature, rgb_stats) = birder.load_pretrained_model(model_name, inference=True)
size = birder.get_size_from_signature(signature)
transform = birder.classification_transform(size, rgb_stats)
(out, _) = infer_image(net, image, transform)
idx_to_class = {v: k for k, v in class_to_idx.items()}
topk_idx = np.argsort(out[0])[-3:][::-1]
predictions = [(idx_to_class[idx], float(out[0][idx])) for idx in topk_idx]
return predictions
except Exception as e:
return [(f"Error: {str(e)}", 0.0)]
def predict(image, model_name):
predictions = load_model_and_predict(image, model_name)
return {f"{class_name} ({conf:.2%})": conf for class_name, conf in predictions}
def create_interface():
models = get_birder_classification_models()
example_images = [
"Common myna.jpeg",
"Eurasian hoopoe.jpeg",
"Grey heron.jpeg",
]
# Create interface
iface = gr.Interface(
analytics_enabled=False,
fn=predict,
inputs=[
gr.Image(type="pil", label="Input Image"),
gr.Dropdown(
choices=models,
label="Select Model",
value=models[0] if models else None,
),
],
outputs=gr.Label(num_top_classes=3),
examples=[[path] for path in example_images],
title="Birder Image Classification",
description="Select a model and upload an image or use one of the examples to get bird species predictions.",
)
return iface
# Launch the app
if __name__ == "__main__":
demo = create_interface()
demo.launch()
|