Spaces:
Runtime error
Runtime error
import cv2 | |
import numpy as np | |
from typing import List, Tuple, Dict, Any | |
def process_earthwork(frame: np.ndarray) -> Tuple[List[Dict[str, Any]], np.ndarray]: | |
""" | |
Detect earthwork activity 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, 100, 200) | |
# 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 < 500: # 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"Earthwork {i+1}", | |
"type": "earthwork" | |
}) | |
return detections, frame |