Spaces:
Sleeping
Sleeping
File size: 13,958 Bytes
4c9ae97 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
"""
Farm Human Recognition API - Gradio Interface
YOLO and pose estimation models for farm worker detection
"""
import gradio as gr
import torch
import cv2
import numpy as np
from PIL import Image
import json
import base64
import io
import time
from typing import List, Dict, Any
# Import models
try:
from transformers import YolosImageProcessor, YolosForObjectDetection
from transformers import AutoImageProcessor, AutoModelForObjectDetection
MODELS_AVAILABLE = True
except ImportError:
MODELS_AVAILABLE = False
class HumanRecognitionAPI:
def __init__(self):
self.models = {}
self.processors = {}
self.model_configs = {
"yolos_tiny": "hustvl/yolos-tiny",
"yolos_small": "hustvl/yolos-small",
"yolos_base": "hustvl/yolos-base"
}
# Human activity classes relevant to farming
self.farm_activities = {
"harvesting": ["picking", "collecting", "gathering", "harvesting"],
"planting": ["sowing", "planting", "seeding"],
"maintenance": ["pruning", "watering", "fertilizing", "weeding"],
"inspection": ["examining", "checking", "monitoring", "inspecting"],
"operation": ["driving", "operating", "machinery", "equipment"],
"general": ["working", "standing", "walking", "person"]
}
if MODELS_AVAILABLE:
self.load_models()
def load_models(self):
"""Load human detection models"""
for model_key, model_name in self.model_configs.items():
try:
print(f"Loading {model_name}...")
processor = YolosImageProcessor.from_pretrained(model_name)
model = YolosForObjectDetection.from_pretrained(model_name)
self.processors[model_key] = processor
self.models[model_key] = model
print(f"β
{model_name} loaded successfully")
except Exception as e:
print(f"β Failed to load {model_name}: {e}")
def detect_humans(self, image: Image.Image, model_key: str = "yolos_small") -> Dict[str, Any]:
"""Detect humans and analyze farm activities"""
if not MODELS_AVAILABLE or model_key not in self.models:
return {"error": "Model not available"}
start_time = time.time()
try:
# Preprocess image
processor = self.processors[model_key]
model = self.models[model_key]
inputs = processor(images=image, return_tensors="pt")
# Run inference
with torch.no_grad():
outputs = model(**inputs)
# Post-process results
target_sizes = torch.tensor([image.size[::-1]])
results = processor.post_process_object_detection(
outputs, threshold=0.5, target_sizes=target_sizes
)[0]
# Filter for human detections
human_detections = []
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
class_name = model.config.id2label[label.item()].lower()
if "person" in class_name and score > 0.5:
human_detections.append({
"class": "person",
"confidence": float(score),
"bbox": [float(x) for x in box],
"area": float((box[2] - box[0]) * (box[3] - box[1])),
"activity": self.infer_activity(box, image.size)
})
# Analyze safety and productivity
safety_analysis = self.analyze_safety(human_detections, image.size)
productivity_metrics = self.calculate_productivity_metrics(human_detections)
processing_time = time.time() - start_time
return {
"humans_detected": len(human_detections),
"detections": human_detections,
"safety_analysis": safety_analysis,
"productivity_metrics": productivity_metrics,
"processing_time": round(processing_time, 2),
"model_used": model_key
}
except Exception as e:
return {"error": str(e)}
def infer_activity(self, bbox: List[float], image_size: tuple) -> str:
"""Infer farm activity from bounding box characteristics"""
x1, y1, x2, y2 = bbox
width = x2 - x1
height = y2 - y1
# Simple activity inference based on pose characteristics
aspect_ratio = width / height
relative_size = (width * height) / (image_size[0] * image_size[1])
if aspect_ratio > 1.2: # Wide bounding box
return "operating_equipment"
elif relative_size > 0.1: # Large person in frame
return "close_work"
elif y2 > image_size[1] * 0.8: # Person near bottom
return "ground_work"
else:
return "general_activity"
def analyze_safety(self, detections: List[Dict], image_size: tuple) -> Dict[str, Any]:
"""Analyze workplace safety factors"""
if not detections:
return {"status": "no_workers", "score": 1.0}
safety_score = 1.0
concerns = []
# Check worker density
workers_per_area = len(detections) / (image_size[0] * image_size[1] / 1000000) # per megapixel
if workers_per_area > 5:
safety_score -= 0.2
concerns.append("High worker density - ensure adequate spacing")
# Check for workers near equipment (simplified check)
for detection in detections:
if detection["activity"] == "operating_equipment":
# Check if other workers are nearby
nearby_workers = sum(1 for d in detections
if d != detection and self.calculate_distance(d["bbox"], detection["bbox"]) < 100)
if nearby_workers > 0:
safety_score -= 0.3
concerns.append("Workers detected near operating equipment")
return {
"status": "safe" if safety_score > 0.7 else "caution" if safety_score > 0.4 else "unsafe",
"score": max(0.0, safety_score),
"concerns": concerns,
"workers_detected": len(detections)
}
def calculate_distance(self, bbox1: List[float], bbox2: List[float]) -> float:
"""Calculate distance between bounding box centers"""
center1 = [(bbox1[0] + bbox1[2]) / 2, (bbox1[1] + bbox1[3]) / 2]
center2 = [(bbox2[0] + bbox2[2]) / 2, (bbox2[1] + bbox2[3]) / 2]
return ((center1[0] - center2[0]) ** 2 + (center1[1] - center2[1]) ** 2) ** 0.5
def calculate_productivity_metrics(self, detections: List[Dict]) -> Dict[str, Any]:
"""Calculate farm productivity metrics"""
if not detections:
return {"active_workers": 0, "productivity_score": 0.0}
activity_counts = {}
for detection in detections:
activity = detection["activity"]
activity_counts[activity] = activity_counts.get(activity, 0) + 1
# Simple productivity scoring
productive_activities = ["close_work", "ground_work", "operating_equipment"]
productive_workers = sum(activity_counts.get(activity, 0) for activity in productive_activities)
productivity_score = productive_workers / len(detections) if detections else 0
return {
"active_workers": len(detections),
"productivity_score": round(productivity_score, 2),
"activity_breakdown": activity_counts,
"recommendations": self.generate_productivity_recommendations(activity_counts)
}
def generate_productivity_recommendations(self, activity_counts: Dict[str, int]) -> List[str]:
"""Generate productivity improvement recommendations"""
recommendations = []
total_workers = sum(activity_counts.values())
if activity_counts.get("general_activity", 0) > total_workers * 0.3:
recommendations.append("Consider assigning specific tasks to idle workers")
if activity_counts.get("operating_equipment", 0) > 1:
recommendations.append("Multiple equipment operators detected - ensure coordination")
if total_workers > 10:
recommendations.append("Large workforce detected - consider team organization")
return recommendations[:3] # Limit to 3 recommendations
def draw_detections(self, image: Image.Image, detections: List[Dict]) -> Image.Image:
"""Draw bounding boxes and labels on image"""
img_array = np.array(image)
for detection in detections:
bbox = detection["bbox"]
x1, y1, x2, y2 = [int(coord) for coord in bbox]
# Draw bounding box
cv2.rectangle(img_array, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Draw label
label = f"Worker {detection['confidence']:.2f}"
cv2.putText(img_array, label, (x1, y1-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return Image.fromarray(img_array)
# Initialize API
api = HumanRecognitionAPI()
def predict_humans(image, model_choice):
"""Gradio prediction function"""
if image is None:
return None, "Please upload an image"
# Convert to PIL Image
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
# Run human detection
results = api.detect_humans(image, model_choice)
if "error" in results:
return None, f"Error: {results['error']}"
# Create visualization
annotated_image = api.draw_detections(image, results["detections"])
# Format results text
safety = results["safety_analysis"]
productivity = results["productivity_metrics"]
safety_emoji = "π’" if safety["status"] == "safe" else "π‘" if safety["status"] == "caution" else "π΄"
results_text = f"""
π₯ **Farm Worker Analysis**
{safety_emoji} **Safety Status**: {safety['status'].title()} (Score: {safety['score']:.1%})
π· **Workers Detected**: {results['humans_detected']}
π **Productivity Score**: {productivity['productivity_score']:.1%}
**π‘οΈ Safety Analysis**:
"""
if safety["concerns"]:
for concern in safety["concerns"]:
results_text += f"\nβ οΈ {concern}"
else:
results_text += "\nβ
No immediate safety concerns detected"
results_text += f"\n\n**π Productivity Metrics**:"
if productivity["activity_breakdown"]:
for activity, count in productivity["activity_breakdown"].items():
results_text += f"\nβ’ {activity.replace('_', ' ').title()}: {count} workers"
if productivity["recommendations"]:
results_text += f"\n\n**π‘ Recommendations**:"
for rec in productivity["recommendations"]:
results_text += f"\nβ’ {rec}"
return annotated_image, results_text
# Gradio Interface
with gr.Blocks(title="π₯ Farm Human Recognition API") as app:
gr.Markdown("# π₯ Farm Human Recognition API")
gr.Markdown("AI-powered farm worker detection, safety analysis, and productivity assessment")
with gr.Tab("π· Worker Detection"):
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Upload Farm Image")
model_choice = gr.Dropdown(
choices=["yolos_tiny", "yolos_small", "yolos_base"],
value="yolos_small",
label="Select Model"
)
detect_btn = gr.Button("π Detect Workers", variant="primary")
with gr.Column():
output_image = gr.Image(label="Worker Detection Results")
results_text = gr.Textbox(label="Analysis Results", lines=20)
detect_btn.click(
predict_humans,
inputs=[image_input, model_choice],
outputs=[output_image, results_text]
)
with gr.Tab("π‘ API Documentation"):
gr.Markdown("""
## π API Endpoint
**POST** `/api/predict`
### Request Format
```json
{
"data": ["<base64_image>", "<model_choice>"]
}
```
### Model Options
- **yolos_tiny**: Fastest processing, basic accuracy
- **yolos_small**: Balanced performance (recommended)
- **yolos_base**: Highest accuracy, slower processing
### Response Format
```json
{
"humans_detected": 3,
"detections": [
{
"class": "person",
"confidence": 0.92,
"bbox": [120, 45, 180, 200],
"activity": "ground_work"
}
],
"safety_analysis": {
"status": "safe",
"score": 0.85,
"concerns": []
},
"productivity_metrics": {
"active_workers": 3,
"productivity_score": 0.75,
"activity_breakdown": {
"ground_work": 2,
"operating_equipment": 1
}
}
}
```
### Activity Types
- **ground_work**: Workers performing field operations
- **close_work**: Detailed inspection or harvesting
- **operating_equipment**: Machinery operation
- **general_activity**: General farm activities
""")
if __name__ == "__main__":
app.launch() |