lokesh341 commited on
Commit
60a6bef
·
1 Parent(s): 80193dd

Update services/thermal_service.py

Browse files
Files changed (1) hide show
  1. 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 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}
 
 
 
 
 
 
 
 
 
 
 
 
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}