Spaces:
Runtime error
Runtime error
Update services/thermal_service.py
Browse files- services/thermal_service.py +6 -14
services/thermal_service.py
CHANGED
|
@@ -9,7 +9,7 @@ logging.basicConfig(
|
|
| 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:
|
|
@@ -18,41 +18,33 @@ def process_thermal(frame):
|
|
| 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
|
| 34 |
|
| 35 |
for contour in contours:
|
| 36 |
area = cv2.contourArea(contour)
|
| 37 |
-
if area < 200:
|
| 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 |
-
"
|
|
|
|
| 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.")
|
|
|
|
| 9 |
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 10 |
)
|
| 11 |
|
| 12 |
+
def process_thermal(frame: np.ndarray) -> Dict[str, Any]:
|
| 13 |
"""
|
| 14 |
Process a thermal (grayscale) frame to detect hot/cold spots.
|
| 15 |
Args:
|
|
|
|
| 18 |
dict: Results with detections and annotated frame
|
| 19 |
"""
|
| 20 |
try:
|
|
|
|
| 21 |
if len(frame.shape) == 3:
|
| 22 |
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 23 |
|
|
|
|
| 24 |
_, hot_mask = cv2.threshold(frame, 200, 255, cv2.THRESH_BINARY)
|
| 25 |
contours, _ = cv2.findContours(hot_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 26 |
|
|
|
|
| 27 |
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
|
|
|
|
| 28 |
detections = []
|
| 29 |
+
line_counter = 1
|
| 30 |
|
| 31 |
for contour in contours:
|
| 32 |
area = cv2.contourArea(contour)
|
| 33 |
+
if area < 200:
|
| 34 |
continue
|
| 35 |
x, y, w, h = cv2.boundingRect(contour)
|
| 36 |
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
|
|
|
|
|
|
| 37 |
detection_label = f"Line {line_counter} - Hot Spot"
|
| 38 |
detections.append({
|
| 39 |
"type": "hot_spot",
|
| 40 |
"label": detection_label,
|
| 41 |
+
"box": [x_min, y_min, x_max, y_max],
|
| 42 |
+
"severity": "Moderate"
|
| 43 |
})
|
| 44 |
+
color = (255, 69, 0)
|
|
|
|
|
|
|
| 45 |
cv2.rectangle(frame_bgr, (x_min, y_min), (x_max, y_max), color, 2)
|
| 46 |
cv2.putText(frame_bgr, detection_label, (x_min, y_min - 10),
|
| 47 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
|
|
|
| 48 |
line_counter += 1
|
| 49 |
|
| 50 |
logging.info(f"Detected {len(detections)} hot spots in thermal frame.")
|