File size: 1,084 Bytes
9c4ef19
4c8c110
9c4ef19
4c8c110
 
 
 
 
 
 
 
 
 
9c4ef19
 
4c8c110
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from ultralytics import YOLO
import os

def load_fault_model():
    model_path = "pole_fault_model.pt"
    if os.path.exists(model_path):
        print(f"Loading custom model: {model_path}")
        return YOLO(model_path)
    else:
        print(f"Warning: {model_path} not found. Falling back to YOLOv8s.")
        return YOLO("yolov8s.pt")  # Fallback to pre-trained YOLOv8s

fault_model = load_fault_model()

def detect_pole_faults(image_path):
    try:
        results = fault_model(image_path)
        flagged = []
        for r in results:
            # Check if model is custom-trained with fault_type (for custom models)
            if hasattr(r, 'fault_type') and r.conf > 0.6:
                flagged.append({"fault_type": r.fault_type, "confidence": r.conf})
            # Fallback for generic YOLOv8 models (no fault_type)
            elif r.names and r.conf > 0.6:
                flagged.append({"fault_type": r.names[int(r.cls)], "confidence": r.conf})
        return flagged
    except Exception as e:
        print(f"Error in fault detection: {e}")
        return []