lokesh341 commited on
Commit
e96704f
·
verified ·
1 Parent(s): 725fc1c

Update services/under_construction/culvert_check.py

Browse files
services/under_construction/culvert_check.py CHANGED
@@ -1,23 +1,37 @@
1
- import numpy as np
2
  import cv2
 
 
3
 
4
- def process_culverts(frame: np.ndarray) -> tuple[list[dict[str, any]], np.ndarray]:
5
  """
6
- Placeholder function to detect culverts in the frame.
7
  Args:
8
- frame: Input frame as a NumPy array (BGR format).
9
  Returns:
10
- Tuple of (detections, processed_frame).
11
- Detections are a list of dictionaries with bounding boxes and labels.
12
  """
13
- # Placeholder: Simulate culvert detection with a single bounding box
14
- height, width = frame.shape[:2]
15
- detections = [
16
- {
17
- "box": [int(width * 0.3), int(height * 0.3), int(width * 0.5), int(height * 0.5)], # [x_min, y_min, x_max, y_max]
18
- "type": "culvert",
19
- "confidence": 0.9
20
- }
21
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  return detections, frame
 
 
1
  import cv2
2
+ import numpy as np
3
+ from typing import List, Tuple, Dict, Any
4
 
5
+ def process_culverts(frame: np.ndarray) -> Tuple[List[Dict[str, Any]], np.ndarray]:
6
  """
7
+ Detect culverts in the frame.
8
  Args:
9
+ frame: Input frame as a numpy array.
10
  Returns:
11
+ Tuple of (list of detections, annotated frame).
 
12
  """
13
+ # Convert to grayscale
14
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
15
+
16
+ # Apply edge detection
17
+ edges = cv2.Canny(gray, 50, 150)
18
+
19
+ # Find contours
20
+ contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
21
+
22
+ detections = []
23
+ for i, contour in enumerate(contours):
24
+ area = cv2.contourArea(contour)
25
+ if area < 300: # Ignore small contours
26
+ continue
27
+
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
+ detections.append({
32
+ "box": [x_min, y_min, x_max, y_max],
33
+ "label": f"Culvert {i+1}",
34
+ "type": "culvert"
35
+ })
36
 
37
  return detections, frame