Spaces:
Runtime error
Runtime error
Update services/thermal_service.py
Browse files- services/thermal_service.py +54 -38
services/thermal_service.py
CHANGED
@@ -1,46 +1,62 @@
|
|
1 |
-
# services/thermal_service.py
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def process_thermal(frame):
|
6 |
"""
|
7 |
-
Process thermal
|
8 |
Args:
|
9 |
-
frame: Input frame (grayscale
|
10 |
Returns:
|
11 |
-
dict:
|
12 |
-
numpy array: Annotated frame
|
13 |
"""
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
import logging
|
4 |
+
|
5 |
+
# Setup logging
|
6 |
+
logging.basicConfig(
|
7 |
+
filename="app.log",
|
8 |
+
level=logging.INFO,
|
9 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
10 |
+
)
|
11 |
|
12 |
def process_thermal(frame):
|
13 |
"""
|
14 |
+
Process a thermal (grayscale) frame to detect hot/cold spots.
|
15 |
Args:
|
16 |
+
frame: Input frame (grayscale numpy array)
|
17 |
Returns:
|
18 |
+
dict: Results with detections and annotated frame
|
|
|
19 |
"""
|
20 |
+
try:
|
21 |
+
# Ensure frame is grayscale
|
22 |
+
if len(frame.shape) == 3:
|
23 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
24 |
+
|
25 |
+
# Threshold to detect hot spots (high intensity)
|
26 |
+
_, hot_mask = cv2.threshold(frame, 200, 255, cv2.THRESH_BINARY)
|
27 |
+
contours, _ = cv2.findContours(hot_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
28 |
+
|
29 |
+
# Convert grayscale to BGR for annotation
|
30 |
+
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
|
31 |
+
|
32 |
+
detections = []
|
33 |
+
line_counter = 1 # Initialize counter for numbered labels
|
34 |
+
|
35 |
+
for contour in contours:
|
36 |
+
area = cv2.contourArea(contour)
|
37 |
+
if area < 200: # Filter small areas
|
38 |
+
continue
|
39 |
+
x, y, w, h = cv2.boundingRect(contour)
|
40 |
+
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
41 |
+
|
42 |
+
# Add numbered label
|
43 |
+
detection_label = f"Line {line_counter} - Hot Spot"
|
44 |
+
detections.append({
|
45 |
+
"type": "hot_spot",
|
46 |
+
"label": detection_label,
|
47 |
+
"coordinates": [x_min, y_min, x_max, y_max]
|
48 |
+
})
|
49 |
+
|
50 |
+
# Draw bounding box and label
|
51 |
+
color = (255, 69, 0) # OrangeRed for hot spots
|
52 |
+
cv2.rectangle(frame_bgr, (x_min, y_min), (x_max, y_max), color, 2)
|
53 |
+
cv2.putText(frame_bgr, detection_label, (x_min, y_min - 10),
|
54 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
55 |
+
|
56 |
+
line_counter += 1
|
57 |
+
|
58 |
+
logging.info(f"Detected {len(detections)} hot spots in thermal frame.")
|
59 |
+
return {"detections": detections, "frame": frame_bgr}
|
60 |
+
except Exception as e:
|
61 |
+
logging.error(f"Error processing thermal frame: {str(e)}")
|
62 |
+
return {"detections": [], "frame": frame}
|