File size: 1,958 Bytes
0f9b509
 
60a6bef
a865916
60a6bef
 
 
 
 
 
 
0f9b509
51c44a6
0f9b509
60a6bef
0f9b509
60a6bef
0f9b509
60a6bef
0f9b509
60a6bef
 
 
 
 
 
 
 
 
51c44a6
60a6bef
 
 
51c44a6
60a6bef
 
 
 
 
 
 
51c44a6
 
60a6bef
51c44a6
60a6bef
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
47
48
49
50
51
52
53
54
55
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}