lokesh341 commited on
Commit
146e714
·
1 Parent(s): 26b4122

Update services/plantation/plant_count.py

Browse files
Files changed (1) hide show
  1. services/plantation/plant_count.py +29 -19
services/plantation/plant_count.py CHANGED
@@ -2,25 +2,36 @@ import cv2
2
  import numpy as np
3
  from ultralytics import YOLO
4
  import os
 
 
 
 
 
 
 
5
 
6
- # Load YOLOv8n model (lighter for plant counting)
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_plants(frame):
12
- """
13
- Count plants in a frame.
14
- Args:
15
- frame: Input frame (numpy array)
16
- Returns:
17
- list: List of detected plants
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:
@@ -29,12 +40,11 @@ def process_plants(frame):
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
- # Add numbered label
38
  detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
39
  detections.append({
40
  "type": label,
@@ -43,12 +53,12 @@ def process_plants(frame):
43
  "coordinates": [x_min, y_min, x_max, y_max]
44
  })
45
 
46
- # Draw bounding box and label
47
- color = (34, 139, 34) # ForestGreen for plants
48
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
49
  cv2.putText(frame, detection_label, (x_min, y_min - 10),
50
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
51
 
52
  line_counter += 1
53
 
 
54
  return detections, frame
 
2
  import numpy as np
3
  from ultralytics import YOLO
4
  import os
5
+ import logging
6
+
7
+ logging.basicConfig(
8
+ filename="app.log",
9
+ level=logging.INFO,
10
+ format="%(asctime)s - %(levelname)s - %(message)s"
11
+ )
12
 
 
13
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
14
  MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8n.pt"))
15
+ try:
16
+ model = YOLO(MODEL_PATH)
17
+ logging.info("Loaded YOLOv8n model for plant counting.")
18
+ except Exception as e:
19
+ logging.error(f"Failed to load YOLOv8n model: {str(e)}")
20
+ model = None
21
 
22
  def process_plants(frame):
23
+ if model is None:
24
+ logging.error("YOLO model not loaded. Skipping plant counting.")
25
+ return [], frame
26
+
27
+ try:
28
+ results = model(frame)
29
+ except Exception as e:
30
+ logging.error(f"Error during YOLO inference: {str(e)}")
31
+ return [], frame
32
 
33
  detections = []
34
+ line_counter = 1
35
 
36
  for r in results:
37
  for box in r.boxes:
 
40
  continue
41
  cls = int(box.cls[0])
42
  label = model.names[cls]
43
+ if label != "plant":
44
  continue
45
+ xyxy = box.xyxy[0].cpu().numpy().astype(int)
46
+ x_min, y_min, x_max, y_max = xyxy
47
 
 
48
  detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
49
  detections.append({
50
  "type": label,
 
53
  "coordinates": [x_min, y_min, x_max, y_max]
54
  })
55
 
56
+ color = (255, 192, 203) # Pink as requested
 
57
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
58
  cv2.putText(frame, detection_label, (x_min, y_min - 10),
59
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
60
 
61
  line_counter += 1
62
 
63
+ logging.info(f"Detected {len(detections)} plants in plantation.")
64
  return detections, frame