hassonofer commited on
Commit
36d1d56
·
verified ·
1 Parent(s): 9cf3cac

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. app.py +93 -0
  3. requirements.txt +2 -0
  4. safari.jpeg +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ safari.jpeg filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import birder
2
+ from birder.inference.detection import infer_image
3
+ from huggingface_hub import HfApi
4
+
5
+ import gradio as gr
6
+
7
+
8
+ def get_birder_classification_models():
9
+ api = HfApi()
10
+ models = api.list_models(author="birder-project", tags="object-detection")
11
+ return [model.modelId.split("/")[-1] for model in models]
12
+
13
+
14
+ def get_selected_models():
15
+ return [
16
+ "deformable_detr_boxref_coco_convnext_v2_tiny_imagenet21k",
17
+ ]
18
+
19
+
20
+ def load_model_and_detect(image, model_name, min_score):
21
+ if len(birder.list_pretrained_models(model_name)) == 0:
22
+ model_name = birder.list_pretrained_models(model_name + "*")[0]
23
+
24
+ (net, (class_to_idx, signature, rgb_stats, *_)) = birder.load_pretrained_model(model_name, inference=True)
25
+
26
+ size = birder.get_size_from_signature(signature)
27
+ transform = birder.detection_transform(size, rgb_stats, dynamic_size=signature["dynamic"])
28
+ detections = infer_image(net, image, transform, score_threshold=min_score)
29
+
30
+ idx_to_class = dict(zip(class_to_idx.values(), class_to_idx.keys()))
31
+ label_names = [idx_to_class[i.item()] for i in detections["labels"]]
32
+
33
+ return (detections, label_names)
34
+
35
+
36
+ def predict(image, model_name, min_score):
37
+ (detections, label_names) = load_model_and_detect(image, model_name, min_score)
38
+ if detections is None:
39
+ return (image, [])
40
+
41
+ annotations = []
42
+ boxes = detections["boxes"]
43
+ scores = detections["scores"]
44
+
45
+ for box, score, label in zip(boxes, scores, label_names):
46
+ (x1, y1, x2, y2) = box.tolist()
47
+ annotation = ((int(x1), int(y1), int(x2), int(y2)), f"{label} ({score:.2f})")
48
+ annotations.append(annotation)
49
+
50
+ return (image, annotations)
51
+
52
+
53
+ def create_interface():
54
+ models = get_selected_models()
55
+
56
+ examples = [
57
+ ["safari.jpeg", "deformable_detr_boxref_coco_convnext_v2_tiny_imagenet21k", 0.45],
58
+ ]
59
+
60
+ # Create interface
61
+ iface = gr.Interface(
62
+ analytics_enabled=False,
63
+ deep_link=False,
64
+ fn=predict,
65
+ inputs=[
66
+ gr.Image(type="pil", label="Input Image"),
67
+ gr.Dropdown(
68
+ choices=models,
69
+ label="Select Model",
70
+ value=models[0] if models else None,
71
+ ),
72
+ gr.Slider(
73
+ minimum=0.0,
74
+ maximum=1.0,
75
+ step=0.05,
76
+ value=0.5,
77
+ label="Minimum Score Threshold",
78
+ info="Only detections with confidence above this threshold will be shown",
79
+ ),
80
+ ],
81
+ outputs=gr.AnnotatedImage(),
82
+ examples=examples,
83
+ title="Birder Object Detection",
84
+ description="Select a model and upload an image or use one of the examples to get bird detections.",
85
+ )
86
+
87
+ return iface
88
+
89
+
90
+ # Launch the app
91
+ if __name__ == "__main__":
92
+ demo = create_interface()
93
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ birder
2
+ huggingface_hub
safari.jpeg ADDED

Git LFS Details

  • SHA256: c6bb5ee1ea4de07b9058c7cdbcacd67f9c84c2daf0432d3d6cd0a73216c55048
  • Pointer size: 131 Bytes
  • Size of remote file: 654 kB