File size: 1,740 Bytes
5c24364
 
c240caa
185810d
5c24364
c240caa
 
 
 
 
 
 
185810d
5c24364
185810d
5c24364
 
 
185810d
5c24364
c240caa
185810d
c240caa
5c24364
185810d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import cv2
import numpy as np
import logging
from ultralytics import YOLO

# Setup logging
logging.basicConfig(
    filename="app.log",
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)

def process_frame(frame: np.ndarray) -> tuple[list[dict[str, any]], np.ndarray]:
    """
    Process a frame using YOLO model to detect cracks and holes.
    Args:
        frame: Input frame (numpy array)
    Returns:
        tuple: List of detected items and annotated frame
    """
    try:
        model = YOLO("models/yolov8m.pt")
        results = model(frame)

        detections = []
        line_counter = 1

        for result in results:
            boxes = result.boxes.xyxy.cpu().numpy()
            confidences = result.boxes.conf.cpu().numpy()
            classes = result.boxes.cls.cpu().numpy()

            for box, conf, cls in zip(boxes, confidences, classes):
                x_min, y_min, x_max, y_max = map(int, box)
                type_ = "crack" if cls == 0 else "hole"  # Assume class 0=crack, 1=hole
                severity = "Severe" if conf > 0.8 else "Moderate" if conf > 0.5 else "Minor"
                label = f"Line {line_counter} - {type_.capitalize()} ({severity})"
                detections.append({
                    "type": type_,
                    "label": label,
                    "box": [x_min, y_min, x_max, y_max],
                    "severity": severity,
                    "confidence": float(conf)
                })
                line_counter += 1

        logging.info(f"Detected {len(detections)} objects in frame.")
        return detections, frame
    except Exception as e:
        logging.error(f"Error processing frame: {str(e)}")
        return [], frame