Spaces:
Runtime error
Runtime error
Update services/fault_service.py
Browse files- services/fault_service.py +25 -7
services/fault_service.py
CHANGED
@@ -1,11 +1,29 @@
|
|
1 |
from ultralytics import YOLO
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def detect_pole_faults(image_path):
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from ultralytics import YOLO
|
2 |
+
import os
|
3 |
|
4 |
+
def load_fault_model():
|
5 |
+
model_path = "pole_fault_model.pt"
|
6 |
+
if os.path.exists(model_path):
|
7 |
+
print(f"Loading custom model: {model_path}")
|
8 |
+
return YOLO(model_path)
|
9 |
+
else:
|
10 |
+
print(f"Warning: {model_path} not found. Falling back to YOLOv8s.")
|
11 |
+
return YOLO("yolov8s.pt") # Fallback to pre-trained YOLOv8s
|
12 |
+
|
13 |
+
fault_model = load_fault_model()
|
14 |
|
15 |
def detect_pole_faults(image_path):
|
16 |
+
try:
|
17 |
+
results = fault_model(image_path)
|
18 |
+
flagged = []
|
19 |
+
for r in results:
|
20 |
+
# Check if model is custom-trained with fault_type (for custom models)
|
21 |
+
if hasattr(r, 'fault_type') and r.conf > 0.6:
|
22 |
+
flagged.append({"fault_type": r.fault_type, "confidence": r.conf})
|
23 |
+
# Fallback for generic YOLOv8 models (no fault_type)
|
24 |
+
elif r.names and r.conf > 0.6:
|
25 |
+
flagged.append({"fault_type": r.names[int(r.cls)], "confidence": r.conf})
|
26 |
+
return flagged
|
27 |
+
except Exception as e:
|
28 |
+
print(f"Error in fault detection: {e}")
|
29 |
+
return []
|