Spaces:
Sleeping
Sleeping
Create thermal_service.py
Browse files- services/thermal_service.py +46 -0
services/thermal_service.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# services/thermal_service.py
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
def process_thermal(frame):
|
6 |
+
"""
|
7 |
+
Process thermal camera data to detect hot spots.
|
8 |
+
Args:
|
9 |
+
frame: Input frame (grayscale, representing temperature)
|
10 |
+
Returns:
|
11 |
+
dict: Hot spot detection results with numbered labels
|
12 |
+
numpy array: Annotated frame
|
13 |
+
"""
|
14 |
+
# Apply thresholding to detect hot spots (high temperature areas)
|
15 |
+
_, hot_mask = cv2.threshold(frame, 200, 255, cv2.THRESH_BINARY)
|
16 |
+
contours, _ = cv2.findContours(hot_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
17 |
+
|
18 |
+
detections = []
|
19 |
+
line_counter = 1 # Initialize counter for numbered labels
|
20 |
+
|
21 |
+
# Convert frame to BGR for annotations
|
22 |
+
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
|
23 |
+
|
24 |
+
for contour in contours:
|
25 |
+
area = cv2.contourArea(contour)
|
26 |
+
if area < 50: # Filter small areas
|
27 |
+
continue
|
28 |
+
x, y, w, h = cv2.boundingRect(contour)
|
29 |
+
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
30 |
+
|
31 |
+
# Add numbered label
|
32 |
+
detection_label = f"Line {line_counter} - Hot Spot"
|
33 |
+
detections.append({
|
34 |
+
"label": detection_label,
|
35 |
+
"coordinates": [x_min, y_min, x_max, y_max]
|
36 |
+
})
|
37 |
+
|
38 |
+
# Draw bounding box and label
|
39 |
+
color = (255, 0, 0) # Red for hot spots
|
40 |
+
cv2.rectangle(frame_bgr, (x_min, y_min), (x_max, y_max), color, 2)
|
41 |
+
cv2.putText(frame_bgr, detection_label, (x_min, y_min - 10),
|
42 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
43 |
+
|
44 |
+
line_counter += 1
|
45 |
+
|
46 |
+
return {"detections": detections, "frame": frame_bgr}
|