Spaces:
Runtime error
Runtime error
File size: 1,586 Bytes
601c47d 3efd80c f920ad9 3efd80c 4bf06b6 3efd80c 4bf06b6 3efd80c 4bf06b6 3efd80c 4bf06b6 3efd80c |
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 |
import cv2
import numpy as np
from typing import List, Tuple, Dict, Any
def process_missing_patches(frame: np.ndarray) -> Tuple[List[Dict[str, Any]], np.ndarray]:
"""
Detect missing patches in the plantation area.
Args:
frame: Input frame as a numpy array.
Returns:
Tuple of (list of detections, annotated frame).
"""
# Convert to HSV color space for soil detection
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Define range for soil color (brownish tones)
lower_soil = np.array([10, 50, 50])
upper_soil = np.array([30, 255, 255])
mask = cv2.inRange(hsv, lower_soil, upper_soil)
# Morphological operations to clean up the mask
kernel = np.ones((5, 5), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
# Find contours of missing patches
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
detections = []
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area < 200: # Ignore small patches
continue
x, y, w, h = cv2.boundingRect(contour)
x_min, y_min, x_max, y_max = x, y, x + w, y + h
# Determine severity based on area
severity = "Severe" if area > 1000 else "Moderate" if area > 500 else "Mild"
detections.append({
"box": [x_min, y_min, x_max, y_max],
"label": "Missing",
"type": "missing_patch",
"severity": severity
})
return detections, frame |