Spaces:
Runtime error
Runtime error
Update services/plantation/plant_health.py
Browse files
services/plantation/plant_health.py
CHANGED
@@ -1,62 +1,70 @@
|
|
1 |
-
# services/plantation/plant_health.py
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
4 |
import os
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
7 |
"""
|
8 |
-
|
9 |
Args:
|
10 |
frame: Input frame (numpy array)
|
11 |
Returns:
|
12 |
-
|
13 |
-
numpy array: Annotated frame
|
14 |
"""
|
15 |
-
|
16 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
17 |
-
r, g, b = cv2.split(frame_rgb)
|
18 |
-
|
19 |
-
# Compute VARI: (Green - Red) / (Green + Red - Blue)
|
20 |
-
denominator = (g.astype(float) + r.astype(float) - b.astype(float))
|
21 |
-
denominator[denominator == 0] = 1e-10 # Avoid division by zero
|
22 |
-
vari = (g.astype(float) - r.astype(float)) / denominator
|
23 |
-
|
24 |
-
# Threshold to find unhealthy areas (VARI < 0 indicates poor health)
|
25 |
-
unhealthy_mask = vari < 0
|
26 |
-
contours, _ = cv2.findContours(unhealthy_mask.astype(np.uint8),
|
27 |
-
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
28 |
|
29 |
detections = []
|
30 |
line_counter = 1 # Initialize counter for numbered labels
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
"label": detection_label,
|
44 |
-
"coordinates": [x_min, y_min, x_max, y_max]
|
45 |
-
})
|
46 |
-
|
47 |
-
# Draw bounding box and label
|
48 |
-
color = (255, 0, 0) # Red for unhealthy areas
|
49 |
-
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
50 |
-
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
51 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
52 |
-
|
53 |
-
line_counter += 1
|
54 |
-
|
55 |
-
return {"detections": detections, "frame": frame}
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
from ultralytics import YOLO
|
4 |
import os
|
5 |
|
6 |
+
# Load YOLOv8n model
|
7 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
8 |
+
MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8n.pt"))
|
9 |
+
model = YOLO(MODEL_PATH)
|
10 |
+
|
11 |
+
def process_plant_health(frame):
|
12 |
"""
|
13 |
+
Assess plant health by color analysis (simplified as brightness).
|
14 |
Args:
|
15 |
frame: Input frame (numpy array)
|
16 |
Returns:
|
17 |
+
list: List of plants with their health status
|
18 |
+
numpy array: Annotated frame with numbered labels
|
19 |
"""
|
20 |
+
results = model(frame)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
detections = []
|
23 |
line_counter = 1 # Initialize counter for numbered labels
|
24 |
|
25 |
+
for r in results:
|
26 |
+
for box in r.boxes:
|
27 |
+
conf = float(box.conf[0])
|
28 |
+
if conf < 0.5:
|
29 |
+
continue
|
30 |
+
cls = int(box.cls[0])
|
31 |
+
label = model.names[cls]
|
32 |
+
if label != "plant": # Assuming "plant" class exists
|
33 |
+
continue
|
34 |
+
xyxy = box.xyxy[0].cpu().numpy()
|
35 |
+
x_min, y_min, x_max, y_max = map(int, xyxy)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Extract plant region
|
38 |
+
plant_region = frame[y_min:y_max, x_min:x_max]
|
39 |
+
if plant_region.size == 0:
|
40 |
+
continue
|
41 |
+
|
42 |
+
# Convert to HSV and analyze brightness (simplified health check)
|
43 |
+
hsv = cv2.cvtColor(plant_region, cv2.COLOR_BGR2HSV)
|
44 |
+
brightness = hsv[..., 2].mean()
|
45 |
+
if brightness > 150:
|
46 |
+
health = "healthy"
|
47 |
+
elif brightness > 100:
|
48 |
+
health = "moderate"
|
49 |
+
else:
|
50 |
+
health = "unhealthy"
|
51 |
+
|
52 |
+
# Add numbered label
|
53 |
+
detection_label = f"Line {line_counter} - Plant (Health: {health.capitalize()}, Conf: {conf:.2f})"
|
54 |
+
detections.append({
|
55 |
+
"type": "plant",
|
56 |
+
"label": detection_label,
|
57 |
+
"confidence": conf,
|
58 |
+
"coordinates": [x_min, y_min, x_max, y_max],
|
59 |
+
"health": health
|
60 |
+
})
|
61 |
+
|
62 |
+
# Draw bounding box and label
|
63 |
+
color = (34, 139, 34) if health == "healthy" else (255, 165, 0) if health == "moderate" else (255, 0, 0)
|
64 |
+
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
65 |
+
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
66 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
67 |
+
|
68 |
+
line_counter += 1
|
69 |
+
|
70 |
+
return detections, frame
|