-
Notifications
You must be signed in to change notification settings - Fork 0
/
csq.py
244 lines (176 loc) · 6.23 KB
/
csq.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import re
import tempfile
import subprocess
import exiftool
from numpy import exp, sqrt, log
from libjpeg import decode
MAGIC_SEQ = re.compile(b"\x46\x46\x46\x00\x52\x54")
class CSQReader:
def __init__(self, filename, blocksize=1000000):
self.reader = open(filename, "rb")
self.blocksize = blocksize
self.leftover = b""
self.imgs = []
self.index = 0
self.nframes = None
self.et = exiftool.ExifTool()
self.etHelper = exiftool.ExifToolHelper()
self.et.run()
def _populate_list(self):
self.imgs = []
self.index = 0
x = self.reader.read(self.blocksize)
if len(x) == 0:
return
matches = list(MAGIC_SEQ.finditer(x))
if matches == []:
return
start = matches[0].start()
if self.leftover != b"":
self.imgs.append(self.leftover + x[:start])
if matches[1:] == []:
return
for m1, m2 in zip(matches, matches[1:]):
start = m1.start()
end = m2.start()
self.imgs.append(x[start:end])
self.leftover = x[end:]
def next_frame(self):
if self.index >= len(self.imgs):
self._populate_list()
if len(self.imgs) == 0:
return None
im = self.imgs[self.index]
raw, metadata = extract_data(im, self.etHelper)
thermal_im = raw2temp(raw, metadata[0])
self.index += 1
return thermal_im
def skip_frame(self):
if self.index >= len(self.imgs):
self._populate_list()
if len(self.imgs) == 0:
return False
self.index += 1
return True
def count_frames(self):
self.nframes = 0
while self.skip_frame():
self.nframes += 1
self.reset()
return self.nframes
def frame_at(self, pos: int):
if self.nframes == None:
self.count_frames()
if pos > self.nframes:
print(f"File only has {self.nframes} frames.")
return
self.reset()
fnum = 0
while fnum < pos - 1:
self.skip_frame()
fnum += 1
return self.next_frame()
def frames(self):
for im in self.imgs:
self.index += 1
if self.index >= len(self.imgs):
self._populate_list()
yield from self.frames()
raw, metadata = extract_data(im, self.etHelper)
thermal_im = raw2temp(raw, metadata[0])
yield thermal_im
def get_metadata(self):
if self.index >= len(self.imgs):
self._populate_list()
if len(self.imgs) == 0:
return None
im = self.imgs[self.index]
_, metadata = extract_data(im, self.etHelper)
return metadata
def reset(self):
self.reader.seek(0)
def close(self):
self.reader.close()
def extract_data(bin, etHelper): # binary to raw image
with tempfile.NamedTemporaryFile() as fp:
fp.write(bin)
fp.flush()
fname = fp.name
metadata = etHelper.get_metadata(fname)
binary = subprocess.check_output(["exiftool", "-b", "-RawThermalImage", fname])
raw = decode(binary)
return raw, metadata
def raw2temp(raw, metadata):
E = metadata["FLIR:Emissivity"]
OD = metadata["FLIR:ObjectDistance"]
RTemp = metadata["FLIR:ReflectedApparentTemperature"]
ATemp = metadata["FLIR:AtmosphericTemperature"]
IRWTemp = metadata["FLIR:IRWindowTemperature"]
IRT = metadata["FLIR:IRWindowTransmission"]
RH = metadata["FLIR:RelativeHumidity"]
PR1 = metadata["FLIR:PlanckR1"]
PB = metadata["FLIR:PlanckB"]
PF = metadata["FLIR:PlanckF"]
PO = metadata["FLIR:PlanckO"]
PR2 = metadata["FLIR:PlanckR2"]
ATA1 = float(metadata["FLIR:AtmosphericTransAlpha1"])
ATA2 = float(metadata["FLIR:AtmosphericTransAlpha2"])
ATB1 = float(metadata["FLIR:AtmosphericTransBeta1"])
ATB2 = float(metadata["FLIR:AtmosphericTransBeta2"])
ATX = metadata["FLIR:AtmosphericTransX"]
emiss_wind = 1 - IRT
refl_wind = 0
h2o = (RH / 100) * exp(
1.5587
+ 0.06939 * (ATemp)
- 0.00027816 * (ATemp) ** 2
+ 0.00000068455 * (ATemp) ** 3
)
tau1 = ATX * exp(-sqrt(OD / 2) * (ATA1 + ATB1 * sqrt(h2o))) + (1 - ATX) * exp(
-sqrt(OD / 2) * (ATA2 + ATB2 * sqrt(h2o))
)
tau2 = ATX * exp(-sqrt(OD / 2) * (ATA1 + ATB1 * sqrt(h2o))) + (1 - ATX) * exp(
-sqrt(OD / 2) * (ATA2 + ATB2 * sqrt(h2o))
)
# Note: for this script, we assume the thermal window is at the mid-point (OD/2) between the source
# and the camera sensor
raw_refl1 = PR1 / (PR2 * (exp(PB / (RTemp + 273.15)) - PF)) - PO
raw_refl1_attn = (1 - E) / E * raw_refl1
raw_atm1 = PR1 / (PR2 * (exp(PB / (ATemp + 273.15)) - PF)) - PO
raw_atm1_attn = (1 - tau1) / E / tau1 * raw_atm1
raw_wind = PR1 / (PR2 * (exp(PB / (IRWTemp + 273.15)) - PF)) - PO
raw_wind_attn = emiss_wind / E / tau1 / IRT * raw_wind
raw_refl2 = PR1 / (PR2 * (exp(PB / (RTemp + 273.15)) - PF)) - PO
raw_refl2_attn = refl_wind / E / tau1 / IRT * raw_refl2
raw_atm2 = PR1 / (PR2 * (exp(PB / (ATemp + 273.15)) - PF)) - PO
raw_atm2_attn = (1 - tau2) / E / tau1 / IRT / tau2 * raw_atm2
raw_obj = (
raw / E / tau1 / IRT / tau2
- raw_atm1_attn
- raw_atm2_attn
- raw_wind_attn
- raw_refl1_attn
- raw_refl2_attn
)
temp_C = PB / log(PR1 / (PR2 * (raw_obj + PO)) + PF) - 273.15
return temp_C
if __name__ == "__main__":
from sys import argv
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
def plot_thermal(frame):
sns.set_style("ticks")
fig = plt.figure()
ax = plt.gca()
plt.axis("off")
im = plt.imshow(frame, cmap="hot")
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(im, cax=cax)
cbar.ax.set_ylabel("Temperature ($^{\circ}$C)", fontsize=14)
sns.despine()
plt.show()
reader = CSQReader(argv[1])
frame = reader.next_frame()
plot_thermal(frame)