Update services/detection_service.py
Browse files- services/detection_service.py +17 -13
services/detection_service.py
CHANGED
|
@@ -2,37 +2,41 @@ import torch
|
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
def detect_faults_solar(model, image):
|
|
|
|
| 5 |
img_tensor = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0
|
| 6 |
img_tensor = img_tensor.unsqueeze(0)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
|
|
|
| 11 |
faults = []
|
| 12 |
-
for detection in results
|
| 13 |
-
class_id = int(detection
|
| 14 |
if class_id == 0:
|
| 15 |
-
faults.append({"type": "Crack", "location": (detection[0].item(), detection[1].item())})
|
| 16 |
elif class_id == 1:
|
| 17 |
-
faults.append({"type": "Damage", "location": (detection[0].item(), detection[1].item())})
|
| 18 |
elif class_id == 4:
|
| 19 |
-
faults.append({"type": "Hotspot", "location": (detection[0].item(), detection[1].item())})
|
| 20 |
|
| 21 |
return faults
|
| 22 |
|
| 23 |
def detect_faults_windmill(model, image):
|
|
|
|
| 24 |
img_tensor = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0
|
| 25 |
img_tensor = img_tensor.unsqueeze(0)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
|
|
|
| 30 |
faults = []
|
| 31 |
-
for detection in results
|
| 32 |
-
class_id = int(detection
|
| 33 |
if class_id == 2:
|
| 34 |
-
faults.append({"type": "Blade Damage", "location": (detection[0].item(), detection[1].item())})
|
| 35 |
elif class_id == 3:
|
| 36 |
-
faults.append({"type": "Motor Fault", "location": (detection[0].item(), detection[1].item())})
|
| 37 |
|
| 38 |
return faults
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
def detect_faults_solar(model, image):
|
| 5 |
+
# Preprocess image
|
| 6 |
img_tensor = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0
|
| 7 |
img_tensor = img_tensor.unsqueeze(0)
|
| 8 |
|
| 9 |
+
# Perform inference using ultralytics API
|
| 10 |
+
results = model.predict(img_tensor, verbose=False)
|
| 11 |
|
| 12 |
+
# Process results
|
| 13 |
faults = []
|
| 14 |
+
for detection in results[0].boxes: # ultralytics API
|
| 15 |
+
class_id = int(detection.cls)
|
| 16 |
if class_id == 0:
|
| 17 |
+
faults.append({"type": "Crack", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())})
|
| 18 |
elif class_id == 1:
|
| 19 |
+
faults.append({"type": "Damage", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())})
|
| 20 |
elif class_id == 4:
|
| 21 |
+
faults.append({"type": "Hotspot", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())})
|
| 22 |
|
| 23 |
return faults
|
| 24 |
|
| 25 |
def detect_faults_windmill(model, image):
|
| 26 |
+
# Preprocess image
|
| 27 |
img_tensor = torch.from_numpy(image).permute(2, 0, 1).float() / 255.0
|
| 28 |
img_tensor = img_tensor.unsqueeze(0)
|
| 29 |
|
| 30 |
+
# Perform inference using ultralytics API
|
| 31 |
+
results = model.predict(img_tensor, verbose=False)
|
| 32 |
|
| 33 |
+
# Process results
|
| 34 |
faults = []
|
| 35 |
+
for detection in results[0].boxes:
|
| 36 |
+
class_id = int(detection.cls)
|
| 37 |
if class_id == 2:
|
| 38 |
+
faults.append({"type": "Blade Damage", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())})
|
| 39 |
elif class_id == 3:
|
| 40 |
+
faults.append({"type": "Motor Fault", "location": (detection.xyxy[0][0].item(), detection.xyxy[0][1].item())})
|
| 41 |
|
| 42 |
return faults
|