ConservationDrones commited on
Commit
55e3afe
·
verified ·
1 Parent(s): baf706f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +127 -0
README.md CHANGED
@@ -1,3 +1,130 @@
1
  ---
2
  license: mit
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ library_name: transformers
4
+ tags:
5
+ - object-detection
6
+ - vision
7
+ - conservation
8
+ - aerial-imagery
9
+ - drones
10
+ - pytorch
11
  ---
12
+
13
+ # DroneMegaDetector (Transformers Version)
14
+
15
+ This repository hosts the Hugging Face `transformers`-compatible version of **DroneMegaDetector**, an object detection model optimized for conservation drone imagery. The model has been converted from its original format to seamlessly integrate with the Hugging Face ecosystem, allowing for easy loading, inference, and deployment using standard `AutoImageProcessor` and `AutoModelForObjectDetection` classes.
16
+
17
+ **Original GitHub Repository:** [ConservationDronesAI/DroneMegaDetector](https://github.com/ConservationDronesAI/DroneMegaDetector?tab=readme-ov-file)
18
+
19
+ ## Intended Use
20
+
21
+ * **Primary Use Case:** Detecting objects of interest (e.g., wildlife, humans, vehicles) in aerial imagery captured by drones for conservation, ecology, and anti-poaching efforts.
22
+ * **Input:** High-resolution RGB images.
23
+ * **Output:** Bounding boxes, confidence scores, and class labels for detected objects.
24
+
25
+ ## Installation
26
+
27
+ Ensure you have the required dependencies installed to run the model. You can install the exact environment tested for this model using the repository's `requirements.txt`:
28
+
29
+ ```bash
30
+ pip install torch==2.14.0 torchvision==0.29.0 transformers==5.18.0 Pillow==12.3.0
31
+
32
+ ```
33
+
34
+ ## Quickstart & Inference
35
+
36
+ You can run inference using the standard Hugging Face pipeline.
37
+
38
+ The following example code demonstrates how to load the model, process a source image, filter for detections with a confidence score of `>= 0.3`, and save an annotated output image with bounding boxes.
39
+
40
+ ```python
41
+ import os
42
+ import sys
43
+ import torch
44
+ from PIL import Image, ImageDraw, ImageFont
45
+ from transformers import AutoImageProcessor, AutoModelForObjectDetection
46
+
47
+ # Hugging Face repository
48
+ repo_id = "ConservationDrones/DroneMegaDetector"
49
+
50
+ # Load the demo image (defaults to "example.png" if no argument is provided)
51
+ image_path = sys.argv[1] if len(sys.argv) > 1 else "example.png"
52
+ image = Image.open(image_path).convert("RGB")
53
+
54
+ # Load the processor and model from Hugging Face
55
+ processor = AutoImageProcessor.from_pretrained(repo_id)
56
+ model = AutoModelForObjectDetection.from_pretrained(repo_id).eval()
57
+
58
+ # Optimize CPU threading for local inference
59
+ torch.set_num_threads(4)
60
+
61
+ # Preprocess the image and run inference
62
+ encoded = processor(images=image, return_tensors="pt")
63
+
64
+ with torch.inference_mode():
65
+ outputs = model(**encoded)
66
+
67
+ # Keep scores above 0.3 and map boxes back to the original image dimensions
68
+ results = processor.post_process_object_detection(
69
+ outputs,
70
+ threshold=0.3,
71
+ target_sizes=[(image.height, image.width)],
72
+ )[0]
73
+
74
+ # Print detection results
75
+ print(f"{image_path}: {len(results['scores'])} detections (score >= 0.3)")
76
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
77
+ print(
78
+ model.config.id2label[label.item()],
79
+ f"{score.item():.3f}",
80
+ [round(v, 1) for v in box.tolist()],
81
+ )
82
+
83
+ # Draw the detections on the original image
84
+ draw = ImageDraw.Draw(image)
85
+ font = ImageFont.load_default(size=18)
86
+
87
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
88
+ bounds = box.tolist()
89
+ text = f"{model.config.id2label[label.item()]} {score.item():.2f}"
90
+
91
+ # Draw bounding box
92
+ draw.rectangle(bounds, outline="red", width=3)
93
+
94
+ # Position and draw text label
95
+ text_x = min(max(2, bounds[0]), image.width - draw.textlength(text, font=font) - 2)
96
+ draw.text(
97
+ (text_x, max(0, bounds[1] - 20)),
98
+ text,
99
+ font=font,
100
+ fill="white",
101
+ stroke_width=2,
102
+ stroke_fill="black",
103
+ )
104
+
105
+ # Save the annotated image
106
+ output_path = "example_annotated.jpg"
107
+ image.save(output_path, quality=95)
108
+ print(f"Saved {output_path}")
109
+
110
+ ```
111
+
112
+ ## Limitations & Best Practices
113
+
114
+ * **Confidence Thresholding:** The default demonstration filters out predictions below a `0.3` confidence score. You may need to tune this threshold up or down based on your specific drone altitude, camera resolution, and environment.
115
+
116
+
117
+ * **Inference Speed:** For optimal performance on large batches of aerial images, running inference on a CUDA-enabled GPU is highly recommended, though CPU inference is supported (and configured to use 4 threads in the demo script).
118
+
119
+
120
+ * **Image Dimensions:** The `AutoImageProcessor` handles automatic resizing and normalization. However, for extremely high-resolution drone panoramas, consider splitting the image into tiles before passing them to the model to avoid missing small objects (like distant animals).
121
+
122
+ ## Citation
123
+
124
+ If you use this model in your research or conservation project, please consider linking back to the original GitHub repository:
125
+
126
+ > `https://github.com/ConservationDronesAI/DroneMegaDetector`
127
+
128
+ ```
129
+
130
+ ```