lokesh341 commited on
Commit
f920ad9
·
1 Parent(s): 19da067

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
- 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
 
 
 
 
 
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