surveillance / services /plantation /missing_patch_check.py
lokesh341's picture
Update services/plantation/missing_patch_check.py
3efd80c verified
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