-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththermal_reader.py
More file actions
116 lines (93 loc) · 3.25 KB
/
Copy paththermal_reader.py
File metadata and controls
116 lines (93 loc) · 3.25 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import sys
import os
import signal
import time
import logging
import serial
import numpy as np
try:
import cv2 as cv
except:
print("Please install OpenCV (or link existing installation)"
" to see the thermal image")
exit(1)
from senxor.mi48 import MI48, format_header, format_framestats
from senxor.utils import data_to_frame, remap, cv_filter,\
cv_render, RollingAverageFilter,\
connect_senxor
# This will enable mi48 logging debug messages
logger = logging.getLogger(__name__)
logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG"))
# Make the a global variable and use it as an instance of the mi48.
# This allows it to be used directly in a signal_handler.
global mi48
# define a signal handler to ensure clean closure upon CTRL+C
# or kill from terminal
def signal_handler(sig, frame):
"""Ensure clean exit in case of SIGINT or SIGTERM"""
logger.info("Exiting due to SIGINT or SIGTERM")
mi48.stop()
cv.destroyAllWindows()
logger.info("Done.")
sys.exit(0)
# Define the signals that should be handled to ensure clean exit
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Make an instance of the MI48, attaching USB for
# both control and data interface.
# can try connect_senxor(src='/dev/ttyS3') or similar if default cannot be found
mi48, connected_port, port_names = connect_senxor()
# print out camera info
logger.info('Camera info:')
logger.info(mi48.camera_info)
# set desired FPS
if len(sys.argv) == 2:
STREAM_FPS = int(sys.argv[1])
else:
STREAM_FPS = 15
mi48.set_fps(STREAM_FPS)
# see if filtering is available in MI48 and set it up
mi48.disable_filter(f1=True, f2=True, f3=True)
mi48.set_filter_1(85)
mi48.enable_filter(f1=True, f2=False, f3=False, f3_ks_5=False)
mi48.set_offset_corr(0.0)
mi48.set_sens_factor(100)
mi48.get_sens_factor()
# initiate continuous frame acquisition
with_header = True
mi48.start(stream=True, with_header=with_header)
# change this to false if not interested in the image
GUI = True
# set cv_filter parameters
par = {'blur_ks':3, 'd':5, 'sigmaColor': 27, 'sigmaSpace': 27}
dminav = RollingAverageFilter(N=10)
dmaxav = RollingAverageFilter(N=10)
while True:
data, header = mi48.read()
if data is None:
logger.critical('NONE data received instead of GFRA')
mi48.stop()
sys.exit(1)
min_temp = dminav(data.min()) # + 1.5
max_temp = dmaxav(data.max()) # - 1.5
frame = data_to_frame(data, (80,62), hflip=False);
frame = np.clip(frame, min_temp, max_temp)
filt_uint8 = cv_filter(remap(frame), par, use_median=True,
use_bilat=True, use_nlm=False)
#
if header is not None:
logger.debug(' '.join([format_header(header),
format_framestats(data)]))
else:
logger.debug(format_framestats(data))
if GUI:
# cv_render(filt_uint8, resize=(400,310), colormap='ironbow')
cv_render(filt_uint8, resize=(400,310), colormap='rainbow2')
# cv_render(remap(frame), resize=(400,310), colormap='rainbow2')
key = cv.waitKey(1) # & 0xFF
if key == ord("q"):
break
# time.sleep(1)
# stop capture and quit
mi48.stop()
cv.destroyAllWindows()