SuriRaja commited on
Commit
d41b506
·
1 Parent(s): b5a8947

Update services/thermal_service.py

Browse files
Files changed (1) hide show
  1. services/thermal_service.py +19 -21
services/thermal_service.py CHANGED
@@ -1,31 +1,29 @@
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
 
1
+ # services/thermal_service.py
2
+
3
+ from ultralytics import YOLO
4
  import cv2
5
 
6
+ # Load a pre-trained YOLO model (small model)
7
+ thermal_model = YOLO('yolov8n.pt') # assuming model auto-download
8
 
9
  def detect_thermal_anomalies(image_path):
10
  """
11
+ Detect thermal anomalies using YOLOv8 model.
12
+ - Accepts a file path to an image.
13
+ - Processes it through YOLO.
14
+ - Flags boxes above a confidence threshold.
15
  """
16
+ # ✅ Read image properly using OpenCV
17
  image = cv2.imread(image_path)
 
18
 
19
+ # Perform detection
20
+ results = thermal_model.predict(source=image, save=False, conf=0.5, imgsz=640)
 
 
 
 
 
21
 
22
+ flagged = []
23
+ for r in results:
24
+ if hasattr(r, 'boxes'):
25
+ for box in r.boxes:
26
+ if box.conf > 0.7:
27
+ flagged.append(box)
 
28
 
29
+ return flagged