Upload 5 files
Browse files- Common myna.jpeg +0 -0
- Eurasian hoopoe.jpeg +0 -0
- Grey heron.jpeg +0 -0
- app.py +69 -0
- requirements.txt +2 -0
Common myna.jpeg
ADDED
![]() |
Eurasian hoopoe.jpeg
ADDED
![]() |
Grey heron.jpeg
ADDED
![]() |
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import birder
|
2 |
+
import numpy as np
|
3 |
+
from birder.inference.classification import infer_image
|
4 |
+
from huggingface_hub import HfApi
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
|
9 |
+
def get_birder_classification_models():
|
10 |
+
api = HfApi()
|
11 |
+
models = api.list_models(author="birder-project", tags="image-classification")
|
12 |
+
return [model.modelId.split("/")[-1] for model in models]
|
13 |
+
|
14 |
+
|
15 |
+
def load_model_and_predict(image, model_name):
|
16 |
+
try:
|
17 |
+
(net, class_to_idx, signature, rgb_stats) = birder.load_pretrained_model(model_name, inference=True)
|
18 |
+
size = birder.get_size_from_signature(signature)
|
19 |
+
transform = birder.classification_transform(size, rgb_stats)
|
20 |
+
(out, _) = infer_image(net, image, transform)
|
21 |
+
|
22 |
+
idx_to_class = {v: k for k, v in class_to_idx.items()}
|
23 |
+
topk_idx = np.argsort(out[0])[-3:][::-1]
|
24 |
+
predictions = [(idx_to_class[idx], float(out[0][idx])) for idx in topk_idx]
|
25 |
+
|
26 |
+
return predictions
|
27 |
+
except Exception as e:
|
28 |
+
return [(f"Error: {str(e)}", 0.0)]
|
29 |
+
|
30 |
+
|
31 |
+
def predict(image, model_name):
|
32 |
+
predictions = load_model_and_predict(image, model_name)
|
33 |
+
return {f"{class_name} ({conf:.2%})": conf for class_name, conf in predictions}
|
34 |
+
|
35 |
+
|
36 |
+
def create_interface():
|
37 |
+
models = get_birder_classification_models()
|
38 |
+
|
39 |
+
example_images = [
|
40 |
+
"Common myna.jpeg",
|
41 |
+
"Eurasian hoopoe.jpeg",
|
42 |
+
"Grey heron.jpeg",
|
43 |
+
]
|
44 |
+
|
45 |
+
# Create interface
|
46 |
+
iface = gr.Interface(
|
47 |
+
analytics_enabled=False,
|
48 |
+
fn=predict,
|
49 |
+
inputs=[
|
50 |
+
gr.Image(type="pil", label="Input Image"),
|
51 |
+
gr.Dropdown(
|
52 |
+
choices=models,
|
53 |
+
label="Select Model",
|
54 |
+
value=models[0] if models else None,
|
55 |
+
),
|
56 |
+
],
|
57 |
+
outputs=gr.Label(num_top_classes=3),
|
58 |
+
examples=[[path] for path in example_images],
|
59 |
+
title="Birder Image Classification",
|
60 |
+
description="Select a model and upload an image or use one of the examples to get bird species predictions.",
|
61 |
+
)
|
62 |
+
|
63 |
+
return iface
|
64 |
+
|
65 |
+
|
66 |
+
# Launch the app
|
67 |
+
if __name__ == "__main__":
|
68 |
+
demo = create_interface()
|
69 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
birder
|
2 |
+
huggingface_hub
|