Spaces:
Runtime error
Runtime error
Update services/thermal_service.py
Browse files- services/thermal_service.py +19 -21
services/thermal_service.py
CHANGED
|
@@ -1,31 +1,29 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
|
| 7 |
def detect_thermal_anomalies(image_path):
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
"""
|
|
|
|
| 12 |
image = cv2.imread(image_path)
|
| 13 |
-
results = object_detector(image)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
})
|
| 30 |
|
| 31 |
-
return
|
|
|
|
| 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
|