Spaces:
Sleeping
Sleeping
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) ->
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
Args:
|
| 8 |
-
frame: Input frame as a
|
| 9 |
Returns:
|
| 10 |
-
Tuple of (detections,
|
| 11 |
-
Detections are a list of dictionaries with bounding boxes and labels.
|
| 12 |
"""
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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
|