Spaces:
Runtime error
Runtime error
Update services/thermal_service.py
Browse files- services/thermal_service.py +25 -39
services/thermal_service.py
CHANGED
|
@@ -1,45 +1,31 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from ultralytics import YOLO
|
| 4 |
-
from torch.serialization import add_safe_globals
|
| 5 |
-
import torch.nn.modules.container as container
|
| 6 |
-
from ultralytics.nn.tasks import DetectionModel
|
| 7 |
-
from ultralytics.nn.modules import Conv
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
container.Sequential: "torch.nn.modules.container.Sequential",
|
| 12 |
-
container.ModuleList: "torch.nn.modules.container.ModuleList",
|
| 13 |
-
container.ModuleDict: "torch.nn.modules.container.ModuleDict",
|
| 14 |
-
DetectionModel: "ultralytics.nn.tasks.DetectionModel",
|
| 15 |
-
Conv: "ultralytics.nn.modules.Conv"
|
| 16 |
-
})
|
| 17 |
|
| 18 |
-
def
|
| 19 |
"""
|
| 20 |
-
|
|
|
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
print("[INFO] YOLOv8 model loaded successfully.")
|
| 25 |
-
return model
|
| 26 |
-
except Exception as e:
|
| 27 |
-
print(f"[ERROR] Failed to load YOLO model: {e}")
|
| 28 |
-
raise
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
flagged.append({
|
| 42 |
-
"confidence": float(box.conf),
|
| 43 |
-
"bbox": box.xyxy.tolist() if hasattr(box, 'xyxy') else []
|
| 44 |
-
})
|
| 45 |
-
return flagged
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Load DETR model from Hugging Face
|
| 5 |
+
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def detect_thermal_anomalies(image_path):
|
| 8 |
"""
|
| 9 |
+
Simulate thermal anomalies using DETR object detection.
|
| 10 |
+
If an object has a high confidence and occupies a big bounding box, simulate a "hotspot."
|
| 11 |
"""
|
| 12 |
+
image = cv2.imread(image_path)
|
| 13 |
+
results = object_detector(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
anomalies = []
|
| 16 |
+
for result in results:
|
| 17 |
+
score = result.get('score', 0)
|
| 18 |
+
box = result.get('box', {})
|
| 19 |
+
width = box.get('width', 0)
|
| 20 |
+
height = box.get('height', 0)
|
| 21 |
+
area = width * height
|
| 22 |
|
| 23 |
+
# Simulate: large bounding boxes + high score = thermal anomaly
|
| 24 |
+
if score > 0.7 and area > 5000: # adjust thresholds if needed
|
| 25 |
+
anomalies.append({
|
| 26 |
+
"label": result.get('label', ''),
|
| 27 |
+
"score": score,
|
| 28 |
+
"bbox": box
|
| 29 |
+
})
|
| 30 |
+
|
| 31 |
+
return anomalies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|