Spaces:
Runtime error
Runtime error
Update services/plantation/missing_patch_check.py
Browse files
services/plantation/missing_patch_check.py
CHANGED
@@ -1,47 +1,46 @@
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def process_missing_patches(frame):
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
Returns:
|
11 |
-
list: List of missing patches
|
12 |
-
numpy array: Annotated frame with numbered labels
|
13 |
-
"""
|
14 |
-
# Convert to grayscale
|
15 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
16 |
-
# Apply thresholding to detect plant areas (simplified)
|
17 |
-
_, plant_mask = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)
|
18 |
-
contours, _ = cv2.findContours(plant_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
})
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
line_counter += 1
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import os
|
4 |
+
import logging
|
5 |
+
|
6 |
+
logging.basicConfig(
|
7 |
+
filename="app.log",
|
8 |
+
level=logging.INFO,
|
9 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
10 |
+
)
|
11 |
|
12 |
def process_missing_patches(frame):
|
13 |
+
try:
|
14 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
15 |
+
_, plant_mask = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)
|
16 |
+
contours, _ = cv2.findContours(plant_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
detections = []
|
19 |
+
line_counter = 1
|
20 |
|
21 |
+
for contour in contours:
|
22 |
+
area = cv2.contourArea(contour)
|
23 |
+
if area < 500:
|
24 |
+
continue
|
25 |
+
x, y, w, h = cv2.boundingRect(contour)
|
26 |
+
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
|
|
27 |
|
28 |
+
detection_label = f"Line {line_counter} - Missing Patch"
|
29 |
+
detections.append({
|
30 |
+
"type": "missing_patch",
|
31 |
+
"label": detection_label,
|
32 |
+
"coordinates": [x_min, y_min, x_max, y_max]
|
33 |
+
})
|
|
|
34 |
|
35 |
+
color = (139, 69, 19) # Brown as requested
|
36 |
+
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
37 |
+
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
38 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
39 |
+
|
40 |
+
line_counter += 1
|
|
|
41 |
|
42 |
+
logging.info(f"Detected {len(detections)} missing patches in plantation.")
|
43 |
+
return detections, frame
|
44 |
+
except Exception as e:
|
45 |
+
logging.error(f"Error detecting missing patches: {str(e)}")
|
46 |
+
return [], frame
|