Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +83 -12
- app.py +229 -0
- requirements.txt +8 -0
README.md
CHANGED
|
@@ -1,12 +1,83 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Farm Object Detection API
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.28.3
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
short_description: Object detection for farm equipment, crops, and workers
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# π Farm Object Detection API
|
| 15 |
+
|
| 16 |
+
High-performance object detection for agricultural environments using RT-DETR models.
|
| 17 |
+
|
| 18 |
+
## π― Capabilities
|
| 19 |
+
- **Farm Equipment Detection**: Tractors, harvesters, tools
|
| 20 |
+
- **Crop Counting**: Automated inventory of plants and produce
|
| 21 |
+
- **Worker Safety**: Personnel detection and activity monitoring
|
| 22 |
+
- **Animal Detection**: Livestock and wildlife identification
|
| 23 |
+
|
| 24 |
+
## π€ Models
|
| 25 |
+
- **RT-DETR R18VD**: Lightweight, fast inference (15-30 FPS)
|
| 26 |
+
- **RT-DETR R34VD**: Balanced performance and accuracy
|
| 27 |
+
- **RT-DETR R50VD**: High accuracy for detailed analysis
|
| 28 |
+
|
| 29 |
+
## π‘ API Usage
|
| 30 |
+
|
| 31 |
+
### Python
|
| 32 |
+
```python
|
| 33 |
+
import requests
|
| 34 |
+
import base64
|
| 35 |
+
|
| 36 |
+
def detect_objects(image_path, model="r50vd"):
|
| 37 |
+
with open(image_path, "rb") as f:
|
| 38 |
+
image_b64 = base64.b64encode(f.read()).decode()
|
| 39 |
+
|
| 40 |
+
response = requests.post(
|
| 41 |
+
"https://YOUR-USERNAME-farm-object-detection.hf.space/api/predict",
|
| 42 |
+
json={"data": [image_b64, model]}
|
| 43 |
+
)
|
| 44 |
+
return response.json()
|
| 45 |
+
|
| 46 |
+
result = detect_objects("farm_image.jpg")
|
| 47 |
+
print(result)
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
### JavaScript
|
| 51 |
+
```javascript
|
| 52 |
+
async function detectObjects(imageFile, model = 'r50vd') {
|
| 53 |
+
const base64 = await fileToBase64(imageFile);
|
| 54 |
+
|
| 55 |
+
const response = await fetch(
|
| 56 |
+
'https://YOUR-USERNAME-farm-object-detection.hf.space/api/predict',
|
| 57 |
+
{
|
| 58 |
+
method: 'POST',
|
| 59 |
+
headers: { 'Content-Type': 'application/json' },
|
| 60 |
+
body: JSON.stringify({ data: [base64, model] })
|
| 61 |
+
}
|
| 62 |
+
);
|
| 63 |
+
|
| 64 |
+
return await response.json();
|
| 65 |
+
}
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
## π Response Format
|
| 69 |
+
```json
|
| 70 |
+
{
|
| 71 |
+
"objects_detected": 12,
|
| 72 |
+
"detections": [
|
| 73 |
+
{
|
| 74 |
+
"class": "tractor",
|
| 75 |
+
"confidence": 0.95,
|
| 76 |
+
"bbox": [100, 150, 400, 350],
|
| 77 |
+
"area": 75000
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
"processing_time": 0.8,
|
| 81 |
+
"model_used": "rtdetr_r50vd"
|
| 82 |
+
}
|
| 83 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Farm Object Detection API - Gradio Interface
|
| 3 |
+
RT-DETR models for agricultural object detection
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
import cv2
|
| 9 |
+
import numpy as np
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import json
|
| 12 |
+
import base64
|
| 13 |
+
import io
|
| 14 |
+
import time
|
| 15 |
+
from typing import List, Dict, Any
|
| 16 |
+
|
| 17 |
+
# Import RT-DETR
|
| 18 |
+
try:
|
| 19 |
+
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor
|
| 20 |
+
MODELS_AVAILABLE = True
|
| 21 |
+
except ImportError:
|
| 22 |
+
MODELS_AVAILABLE = False
|
| 23 |
+
|
| 24 |
+
class ObjectDetectionAPI:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
self.models = {}
|
| 27 |
+
self.processors = {}
|
| 28 |
+
self.model_configs = {
|
| 29 |
+
"r18vd": "PekingU/rtdetr_r18vd",
|
| 30 |
+
"r34vd": "PekingU/rtdetr_r34vd",
|
| 31 |
+
"r50vd": "PekingU/rtdetr_r50vd"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
if MODELS_AVAILABLE:
|
| 35 |
+
self.load_models()
|
| 36 |
+
|
| 37 |
+
def load_models(self):
|
| 38 |
+
"""Load RT-DETR models"""
|
| 39 |
+
for model_key, model_name in self.model_configs.items():
|
| 40 |
+
try:
|
| 41 |
+
print(f"Loading {model_name}...")
|
| 42 |
+
processor = RTDetrImageProcessor.from_pretrained(model_name)
|
| 43 |
+
model = RTDetrForObjectDetection.from_pretrained(model_name)
|
| 44 |
+
|
| 45 |
+
self.processors[model_key] = processor
|
| 46 |
+
self.models[model_key] = model
|
| 47 |
+
print(f"β
{model_name} loaded successfully")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"β Failed to load {model_name}: {e}")
|
| 50 |
+
|
| 51 |
+
def detect_objects(self, image: Image.Image, model_key: str = "r50vd") -> Dict[str, Any]:
|
| 52 |
+
"""Detect objects in image using RT-DETR"""
|
| 53 |
+
if not MODELS_AVAILABLE or model_key not in self.models:
|
| 54 |
+
return {"error": "Model not available"}
|
| 55 |
+
|
| 56 |
+
start_time = time.time()
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
# Preprocess image
|
| 60 |
+
processor = self.processors[model_key]
|
| 61 |
+
model = self.models[model_key]
|
| 62 |
+
|
| 63 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 64 |
+
|
| 65 |
+
# Run inference
|
| 66 |
+
with torch.no_grad():
|
| 67 |
+
outputs = model(**inputs)
|
| 68 |
+
|
| 69 |
+
# Post-process results
|
| 70 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 71 |
+
results = processor.post_process_object_detection(
|
| 72 |
+
outputs, threshold=0.3, target_sizes=target_sizes
|
| 73 |
+
)[0]
|
| 74 |
+
|
| 75 |
+
# Format detections
|
| 76 |
+
detections = []
|
| 77 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 78 |
+
if score > 0.3: # Confidence threshold
|
| 79 |
+
detections.append({
|
| 80 |
+
"class": model.config.id2label[label.item()],
|
| 81 |
+
"confidence": float(score),
|
| 82 |
+
"bbox": [float(x) for x in box],
|
| 83 |
+
"area": float((box[2] - box[0]) * (box[3] - box[1]))
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
processing_time = time.time() - start_time
|
| 87 |
+
|
| 88 |
+
return {
|
| 89 |
+
"objects_detected": len(detections),
|
| 90 |
+
"detections": detections,
|
| 91 |
+
"processing_time": round(processing_time, 2),
|
| 92 |
+
"model_used": f"rtdetr_{model_key}"
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return {"error": str(e)}
|
| 97 |
+
|
| 98 |
+
def draw_detections(self, image: Image.Image, detections: List[Dict]) -> Image.Image:
|
| 99 |
+
"""Draw bounding boxes on image"""
|
| 100 |
+
img_array = np.array(image)
|
| 101 |
+
|
| 102 |
+
for det in detections:
|
| 103 |
+
bbox = det["bbox"]
|
| 104 |
+
x1, y1, x2, y2 = map(int, bbox)
|
| 105 |
+
|
| 106 |
+
# Draw bounding box
|
| 107 |
+
cv2.rectangle(img_array, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 108 |
+
|
| 109 |
+
# Draw label
|
| 110 |
+
label = f"{det['class']}: {det['confidence']:.2f}"
|
| 111 |
+
cv2.putText(img_array, label, (x1, y1-10),
|
| 112 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 113 |
+
|
| 114 |
+
return Image.fromarray(img_array)
|
| 115 |
+
|
| 116 |
+
# Initialize API
|
| 117 |
+
api = ObjectDetectionAPI()
|
| 118 |
+
|
| 119 |
+
def predict_objects(image, model_choice):
|
| 120 |
+
"""Gradio prediction function"""
|
| 121 |
+
if image is None:
|
| 122 |
+
return None, "Please upload an image"
|
| 123 |
+
|
| 124 |
+
# Convert to PIL Image
|
| 125 |
+
if isinstance(image, np.ndarray):
|
| 126 |
+
image = Image.fromarray(image)
|
| 127 |
+
|
| 128 |
+
# Run detection
|
| 129 |
+
results = api.detect_objects(image, model_choice)
|
| 130 |
+
|
| 131 |
+
if "error" in results:
|
| 132 |
+
return None, f"Error: {results['error']}"
|
| 133 |
+
|
| 134 |
+
# Draw detections
|
| 135 |
+
annotated_image = api.draw_detections(image, results["detections"])
|
| 136 |
+
|
| 137 |
+
# Format results text
|
| 138 |
+
results_text = f"""
|
| 139 |
+
π **Detection Results**
|
| 140 |
+
- **Objects detected**: {results['objects_detected']}
|
| 141 |
+
- **Processing time**: {results['processing_time']}s
|
| 142 |
+
- **Model used**: {results['model_used']}
|
| 143 |
+
|
| 144 |
+
**Detections**:
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
for i, det in enumerate(results["detections"][:10], 1): # Show top 10
|
| 148 |
+
results_text += f"\n{i}. **{det['class']}** (confidence: {det['confidence']:.2f})"
|
| 149 |
+
|
| 150 |
+
return annotated_image, results_text
|
| 151 |
+
|
| 152 |
+
def predict_api(image_b64, model_choice):
|
| 153 |
+
"""API endpoint function"""
|
| 154 |
+
try:
|
| 155 |
+
# Decode base64 image
|
| 156 |
+
image_data = base64.b64decode(image_b64)
|
| 157 |
+
image = Image.open(io.BytesIO(image_data))
|
| 158 |
+
|
| 159 |
+
# Run detection
|
| 160 |
+
results = api.detect_objects(image, model_choice)
|
| 161 |
+
return results
|
| 162 |
+
|
| 163 |
+
except Exception as e:
|
| 164 |
+
return {"error": str(e)}
|
| 165 |
+
|
| 166 |
+
# Gradio Interface
|
| 167 |
+
with gr.Blocks(title="π Farm Object Detection API") as app:
|
| 168 |
+
gr.Markdown("# π Farm Object Detection API")
|
| 169 |
+
gr.Markdown("Detect farm equipment, crops, workers, and animals using RT-DETR models")
|
| 170 |
+
|
| 171 |
+
with gr.Tab("πΌοΈ Image Analysis"):
|
| 172 |
+
with gr.Row():
|
| 173 |
+
with gr.Column():
|
| 174 |
+
image_input = gr.Image(type="pil", label="Upload Farm Image")
|
| 175 |
+
model_choice = gr.Dropdown(
|
| 176 |
+
choices=["r18vd", "r34vd", "r50vd"],
|
| 177 |
+
value="r50vd",
|
| 178 |
+
label="Select Model"
|
| 179 |
+
)
|
| 180 |
+
detect_btn = gr.Button("π Detect Objects", variant="primary")
|
| 181 |
+
|
| 182 |
+
with gr.Column():
|
| 183 |
+
output_image = gr.Image(label="Detected Objects")
|
| 184 |
+
results_text = gr.Textbox(label="Detection Results", lines=10)
|
| 185 |
+
|
| 186 |
+
detect_btn.click(
|
| 187 |
+
predict_objects,
|
| 188 |
+
inputs=[image_input, model_choice],
|
| 189 |
+
outputs=[output_image, results_text]
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
with gr.Tab("π‘ API Usage"):
|
| 193 |
+
gr.Markdown("""
|
| 194 |
+
## π API Endpoint
|
| 195 |
+
|
| 196 |
+
**POST** `/api/predict`
|
| 197 |
+
|
| 198 |
+
### Request Format
|
| 199 |
+
```json
|
| 200 |
+
{
|
| 201 |
+
"data": ["<base64_image>", "<model_choice>"]
|
| 202 |
+
}
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
### Response Format
|
| 206 |
+
```json
|
| 207 |
+
{
|
| 208 |
+
"objects_detected": 5,
|
| 209 |
+
"detections": [
|
| 210 |
+
{
|
| 211 |
+
"class": "tractor",
|
| 212 |
+
"confidence": 0.95,
|
| 213 |
+
"bbox": [100, 150, 400, 350],
|
| 214 |
+
"area": 75000
|
| 215 |
+
}
|
| 216 |
+
],
|
| 217 |
+
"processing_time": 0.8,
|
| 218 |
+
"model_used": "rtdetr_r50vd"
|
| 219 |
+
}
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
### Model Options
|
| 223 |
+
- **r18vd**: Fast inference (recommended for real-time)
|
| 224 |
+
- **r34vd**: Balanced performance
|
| 225 |
+
- **r50vd**: High accuracy (recommended for analysis)
|
| 226 |
+
""")
|
| 227 |
+
|
| 228 |
+
if __name__ == "__main__":
|
| 229 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
torchvision>=0.15.0
|
| 3 |
+
transformers>=4.30.0
|
| 4 |
+
gradio>=4.28.3
|
| 5 |
+
Pillow>=9.0.0
|
| 6 |
+
opencv-python>=4.8.0
|
| 7 |
+
numpy>=1.21.0
|
| 8 |
+
huggingface-hub>=0.15.0
|