Spaces:
Runtime error
Runtime error
Update services/plantation/missing_patch_check.py
Browse files
services/plantation/missing_patch_check.py
CHANGED
@@ -1,81 +1,47 @@
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
from typing import List, Dict, Tuple
|
5 |
|
6 |
-
|
7 |
-
logging.basicConfig(
|
8 |
-
filename="app.log",
|
9 |
-
level=logging.INFO,
|
10 |
-
format="%(asctime)s - %(levelname)s - %(message)s"
|
11 |
-
)
|
12 |
-
|
13 |
-
def process_missing_patches(frame: np.ndarray) -> Tuple[List[Dict], np.ndarray]:
|
14 |
"""
|
15 |
-
|
16 |
Args:
|
17 |
-
frame: Input frame
|
18 |
Returns:
|
19 |
-
Tuple
|
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 |
-
# Check if there's a tree within this grid cell
|
57 |
-
has_tree = False
|
58 |
-
for tx, ty in tree_positions:
|
59 |
-
if (x - grid_step // 2 <= tx <= x + grid_step // 2 and
|
60 |
-
y - grid_step // 2 <= ty <= y + grid_step // 2):
|
61 |
-
has_tree = True
|
62 |
-
break
|
63 |
-
if not has_tree:
|
64 |
-
# Mark as missing patch
|
65 |
-
x_min, y_min = x - grid_step // 2, y - grid_step // 2
|
66 |
-
x_max, y_max = x + grid_step // 2, y + grid_step // 2
|
67 |
-
detections.append({
|
68 |
-
"type": "missing_patch",
|
69 |
-
"label": "Missing Patch",
|
70 |
-
"box": [x_min, y_min, x_max, y_max]
|
71 |
-
})
|
72 |
-
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 0, 255), 2)
|
73 |
-
cv2.putText(frame, "Missing", (x_min, y_min - 10),
|
74 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
75 |
-
|
76 |
-
logging.info(f"Detected {len(detections)} missing patches in frame.")
|
77 |
-
return detections, frame
|
78 |
-
|
79 |
-
except Exception as e:
|
80 |
-
logging.error(f"Error in missing patch check: {str(e)}")
|
81 |
-
return [], frame
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
from typing import List, Tuple, Dict, Any
|
|
|
4 |
|
5 |
+
def process_missing_patches(frame: np.ndarray) -> Tuple[List[Dict[str, Any]], np.ndarray]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
+
Detect missing patches in the plantation area.
|
8 |
Args:
|
9 |
+
frame: Input frame as a numpy array.
|
10 |
Returns:
|
11 |
+
Tuple of (list of detections, annotated frame).
|
12 |
"""
|
13 |
+
# Convert to HSV color space for soil detection
|
14 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
15 |
+
|
16 |
+
# Define range for soil color (brownish tones)
|
17 |
+
lower_soil = np.array([10, 50, 50])
|
18 |
+
upper_soil = np.array([30, 255, 255])
|
19 |
+
mask = cv2.inRange(hsv, lower_soil, upper_soil)
|
20 |
+
|
21 |
+
# Morphological operations to clean up the mask
|
22 |
+
kernel = np.ones((5, 5), np.uint8)
|
23 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
|
24 |
+
|
25 |
+
# Find contours of missing patches
|
26 |
+
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
27 |
+
|
28 |
+
detections = []
|
29 |
+
for i, contour in enumerate(contours):
|
30 |
+
area = cv2.contourArea(contour)
|
31 |
+
if area < 200: # Ignore small patches
|
32 |
+
continue
|
33 |
+
|
34 |
+
x, y, w, h = cv2.boundingRect(contour)
|
35 |
+
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
36 |
+
|
37 |
+
# Determine severity based on area
|
38 |
+
severity = "Severe" if area > 1000 else "Moderate" if area > 500 else "Mild"
|
39 |
+
|
40 |
+
detections.append({
|
41 |
+
"box": [x_min, y_min, x_max, y_max],
|
42 |
+
"label": "Missing",
|
43 |
+
"type": "missing_patch",
|
44 |
+
"severity": severity
|
45 |
+
})
|
46 |
+
|
47 |
+
return detections, frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|