Spaces:
Sleeping
Sleeping
import cv2 | |
import numpy as np | |
import logging | |
from typing import Dict, Any | |
# Setup logging | |
logging.basicConfig( | |
filename="app.log", | |
level=logging.INFO, | |
format="%(asctime)s - %(levelname)s - %(message)s" | |
) | |
def process_thermal(frame: np.ndarray) -> Dict[str, Any]: | |
""" | |
Process a thermal (grayscale) frame to detect hot/cold spots. | |
Args: | |
frame: Input frame (grayscale numpy array) | |
Returns: | |
dict: Results with detections and annotated frame | |
""" | |
try: | |
if len(frame.shape) == 3: | |
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
_, hot_mask = cv2.threshold(frame, 200, 255, cv2.THRESH_BINARY) | |
contours, _ = cv2.findContours(hot_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) | |
detections = [] | |
line_counter = 1 | |
for contour in contours: | |
area = cv2.contourArea(contour) | |
if area < 200: | |
continue | |
x, y, w, h = cv2.boundingRect(contour) | |
x_min, y_min, x_max, y_max = x, y, x + w, y + h | |
detection_label = f"Line {line_counter} - Hot Spot" | |
detections.append({ | |
"type": "hot_spot", | |
"label": detection_label, | |
"box": [x_min, y_min, x_max, y_max], | |
"severity": "Moderate" | |
}) | |
color = (255, 69, 0) | |
cv2.rectangle(frame_bgr, (x_min, y_min), (x_max, y_max), color, 2) | |
cv2.putText(frame_bgr, detection_label, (x_min, y_min - 10), | |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) | |
line_counter += 1 | |
logging.info(f"Detected {len(detections)} hot spots in thermal frame.") | |
return {"detections": detections, "frame": frame_bgr} | |
except Exception as e: | |
logging.error(f"Error processing thermal frame: {str(e)}") | |
return {"detections": [], "frame": frame} |