Spaces:
Sleeping
Sleeping
File size: 1,077 Bytes
31bbdc7 6e6cf7a 31bbdc7 89bc749 6e6cf7a 90d5e3e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import cv2
import numpy as np
from typing import List, Tuple, Dict, Any
def process_culverts(frame: np.ndarray) -> Tuple[List[Dict[str, Any]], np.ndarray]:
"""
Detect culverts in the frame.
Args:
frame: Input frame as a numpy array.
Returns:
Tuple of (list of detections, annotated frame).
"""
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Apply edge detection
edges = cv2.Canny(gray, 50, 150)
# Find contours
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
detections = []
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area < 300: # Ignore small contours
continue
x, y, w, h = cv2.boundingRect(contour)
x_min, y_min, x_max, y_max = x, y, x + w, y + h
detections.append({
"box": [x_min, y_min, x_max, y_max],
"label": f"Culvert {i+1}",
"type": "culvert"
})
return detections, frame |