Spaces:
Sleeping
Sleeping
Update services/plantation/missing_patch_check.py
Browse files
services/plantation/missing_patch_check.py
CHANGED
|
@@ -1,63 +1,47 @@
|
|
| 1 |
-
# services/plantation/missing_patch_check.py
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from ultralytics import YOLO
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
| 8 |
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
-
MODEL_PATH = os.path.join(BASE_DIR, "../../models/yolov8n.pt")
|
| 10 |
-
model = YOLO(MODEL_PATH)
|
| 11 |
-
|
| 12 |
-
def detect_missing_patches(frame):
|
| 13 |
"""
|
| 14 |
-
Detect missing
|
| 15 |
Args:
|
| 16 |
frame: Input frame (numpy array)
|
| 17 |
Returns:
|
| 18 |
-
|
| 19 |
-
numpy array: Annotated frame
|
| 20 |
"""
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
detections = []
|
| 25 |
line_counter = 1 # Initialize counter for numbered labels
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
for
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 53 |
-
|
| 54 |
-
line_counter += 1
|
| 55 |
-
|
| 56 |
-
return {"detections": detections, "frame": frame}
|
| 57 |
-
|
| 58 |
-
def process_missing_patches(frame):
|
| 59 |
-
"""
|
| 60 |
-
Wrapper function for integration with app.py.
|
| 61 |
-
"""
|
| 62 |
-
result = detect_missing_patches(frame)
|
| 63 |
-
return result["detections"], result["frame"]
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
def process_missing_patches(frame):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
+
Detect missing patches of plants using contour analysis.
|
| 8 |
Args:
|
| 9 |
frame: Input frame (numpy array)
|
| 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 |
detections = []
|
| 21 |
line_counter = 1 # Initialize counter for numbered labels
|
| 22 |
|
| 23 |
+
# Identify gaps (missing patches) by analyzing contour areas
|
| 24 |
+
for contour in contours:
|
| 25 |
+
area = cv2.contourArea(contour)
|
| 26 |
+
if area < 500: # Filter small gaps
|
| 27 |
+
continue
|
| 28 |
+
x, y, w, h = cv2.boundingRect(contour)
|
| 29 |
+
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
| 30 |
+
|
| 31 |
+
# Add numbered label
|
| 32 |
+
detection_label = f"Line {line_counter} - Missing Patch"
|
| 33 |
+
detections.append({
|
| 34 |
+
"type": "missing_patch",
|
| 35 |
+
"label": detection_label,
|
| 36 |
+
"coordinates": [x_min, y_min, x_max, y_max]
|
| 37 |
+
})
|
| 38 |
+
|
| 39 |
+
# Draw bounding box and label
|
| 40 |
+
color = (139, 69, 19) # Brown for missing patches
|
| 41 |
+
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
| 42 |
+
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
| 43 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 44 |
+
|
| 45 |
+
line_counter += 1
|
| 46 |
+
|
| 47 |
+
return detections, frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|