lokesh341 commited on
Commit
e0d874b
·
1 Parent(s): ec9a4e9

Update services/plantation/plant_health.py

Browse files
Files changed (1) hide show
  1. services/plantation/plant_health.py +56 -48
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
- def compute_vari(frame):
 
 
 
 
 
7
  """
8
- Compute VARI (Visible Atmospherically Resistant Index) to assess plant health.
9
  Args:
10
  frame: Input frame (numpy array)
11
  Returns:
12
- dict: Detection results with numbered labels for unhealthy areas
13
- numpy array: Annotated frame
14
  """
15
- # Convert to RGB
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
- # Process unhealthy areas
33
- for contour in contours:
34
- area = cv2.contourArea(contour)
35
- if area < 50: # Filter small areas
36
- continue
37
- x, y, w, h = cv2.boundingRect(contour)
38
- x_min, y_min, x_max, y_max = x, y, x + w, y + h
39
-
40
- # Add numbered label
41
- detection_label = f"Line {line_counter} - Unhealthy Vegetation"
42
- detections.append({
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
- def process_plant_health(frame):
58
- """
59
- Wrapper function for integration with app.py.
60
- """
61
- result = compute_vari(frame)
62
- return result["detections"], result["frame"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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