Spaces:
No application file
No application file
Create utils/fault_detection.py
Browse files- utils/fault_detection.py +57 -0
utils/fault_detection.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Dummy models – replace with your own
|
| 8 |
+
def load_model(path):
|
| 9 |
+
model = torch.load(path, map_location="cpu")
|
| 10 |
+
model.eval()
|
| 11 |
+
return model
|
| 12 |
+
|
| 13 |
+
thermal_model = load_model("models/thermal_fault_model.pt")
|
| 14 |
+
crack_model = load_model("models/crack_detector_model.pt")
|
| 15 |
+
dust_model = load_model("models/dust_detector_model.pt")
|
| 16 |
+
|
| 17 |
+
def detect_faults(image):
|
| 18 |
+
transform = transforms.Compose([
|
| 19 |
+
transforms.ToPILImage(),
|
| 20 |
+
transforms.Resize((224, 224)),
|
| 21 |
+
transforms.ToTensor(),
|
| 22 |
+
])
|
| 23 |
+
|
| 24 |
+
img_tensor = transform(image).unsqueeze(0)
|
| 25 |
+
|
| 26 |
+
results = []
|
| 27 |
+
output_img = image.copy()
|
| 28 |
+
|
| 29 |
+
# Simulated prediction
|
| 30 |
+
if torch.rand(1).item() > 0.5:
|
| 31 |
+
results.append({
|
| 32 |
+
"type": "Thermal Fault",
|
| 33 |
+
"location": "Panel 12-B",
|
| 34 |
+
"confidence": 0.93
|
| 35 |
+
})
|
| 36 |
+
cv2.putText(output_img, "Thermal Fault", (50, 50),
|
| 37 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
| 38 |
+
|
| 39 |
+
if torch.rand(1).item() > 0.5:
|
| 40 |
+
results.append({
|
| 41 |
+
"type": "Crack Detected",
|
| 42 |
+
"location": "Panel 9-C",
|
| 43 |
+
"confidence": 0.88
|
| 44 |
+
})
|
| 45 |
+
cv2.putText(output_img, "Crack", (150, 100),
|
| 46 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
| 47 |
+
|
| 48 |
+
if torch.rand(1).item() > 0.5:
|
| 49 |
+
results.append({
|
| 50 |
+
"type": "Dust Coverage",
|
| 51 |
+
"location": "Panel 3-A",
|
| 52 |
+
"confidence": 0.91
|
| 53 |
+
})
|
| 54 |
+
cv2.putText(output_img, "Dust", (200, 150),
|
| 55 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
|
| 56 |
+
|
| 57 |
+
return results, output_img
|