SuriRaja commited on
Commit
682cff4
·
1 Parent(s): 2827306

Update services/thermal_service.py

Browse files
Files changed (1) hide show
  1. services/thermal_service.py +25 -39
services/thermal_service.py CHANGED
@@ -1,45 +1,31 @@
1
- import os
2
- import torch
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
- # Register all necessary safe classes
10
- add_safe_globals({
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 load_yolo_model_safely():
19
  """
20
- Use direct pretrained YOLOv8n model from Ultralytics Hub (no local weights download needed).
 
21
  """
22
- try:
23
- model = YOLO('yolov8n.pt') # pretrained small model directly from Ultralytics hub
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
- thermal_model = load_yolo_model_safely()
 
 
 
 
 
 
31
 
32
- def detect_thermal_anomalies(image_path):
33
- """
34
- Detect anomalies in an image using the loaded YOLO model.
35
- """
36
- results = thermal_model(image_path)
37
- flagged = []
38
- for r in results:
39
- for box in r.boxes:
40
- if box.conf > 0.7:
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