-
Notifications
You must be signed in to change notification settings - Fork 73
/
flir_image_extractor.py
285 lines (232 loc) · 11.1 KB
/
flir_image_extractor.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import io
import json
import os
import os.path
import re
import csv
import subprocess
from PIL import Image
from math import sqrt, exp, log
from matplotlib import cm
from matplotlib import pyplot as plt
import numpy as np
class FlirImageExtractor:
def __init__(self, exiftool_path="exiftool", is_debug=False):
self.exiftool_path = exiftool_path
self.is_debug = is_debug
self.flir_img_filename = ""
self.image_suffix = "_rgb_image.jpg"
self.thumbnail_suffix = "_rgb_thumb.jpg"
self.thermal_suffix = "_thermal.png"
self.default_distance = 1.0
# valid for PNG thermal images
self.use_thumbnail = False
self.fix_endian = True
self.rgb_image_np = None
self.thermal_image_np = None
pass
def process_image(self, flir_img_filename):
"""
Given a valid image path, process the file: extract real thermal values
and a thumbnail for comparison (generally thumbnail is on the visible spectre)
:param flir_img_filename:
:return:
"""
if self.is_debug:
print("INFO Flir image filepath:{}".format(flir_img_filename))
if not os.path.isfile(flir_img_filename):
raise ValueError("Input file does not exist or this user don't have permission on this file")
self.flir_img_filename = flir_img_filename
if self.get_image_type().upper().strip() == "TIFF":
# valid for tiff images from Zenmuse XTR
self.use_thumbnail = True
self.fix_endian = False
self.rgb_image_np = self.extract_embedded_image()
self.thermal_image_np = self.extract_thermal_image()
def get_image_type(self):
"""
Get the embedded thermal image type, generally can be TIFF or PNG
:return:
"""
meta_json = subprocess.check_output(
[self.exiftool_path, '-RawThermalImageType', '-j', self.flir_img_filename])
meta = json.loads(meta_json.decode())[0]
return meta['RawThermalImageType']
def get_rgb_np(self):
"""
Return the last extracted rgb image
:return:
"""
return self.rgb_image_np
def get_thermal_np(self):
"""
Return the last extracted thermal image
:return:
"""
return self.thermal_image_np
def extract_embedded_image(self):
"""
extracts the visual image as 2D numpy array of RGB values
"""
image_tag = "-EmbeddedImage"
if self.use_thumbnail:
image_tag = "-ThumbnailImage"
visual_img_bytes = subprocess.check_output([self.exiftool_path, image_tag, "-b", self.flir_img_filename])
visual_img_stream = io.BytesIO(visual_img_bytes)
visual_img = Image.open(visual_img_stream)
visual_np = np.array(visual_img)
return visual_np
def extract_thermal_image(self):
"""
extracts the thermal image as 2D numpy array with temperatures in oC
"""
# read image metadata needed for conversion of the raw sensor values
# E=1,SD=1,RTemp=20,ATemp=RTemp,IRWTemp=RTemp,IRT=1,RH=50,PR1=21106.77,PB=1501,PF=1,PO=-7340,PR2=0.012545258
meta_json = subprocess.check_output(
[self.exiftool_path, self.flir_img_filename, '-Emissivity', '-SubjectDistance', '-AtmosphericTemperature',
'-ReflectedApparentTemperature', '-IRWindowTemperature', '-IRWindowTransmission', '-RelativeHumidity',
'-PlanckR1', '-PlanckB', '-PlanckF', '-PlanckO', '-PlanckR2', '-j'])
meta = json.loads(meta_json.decode())[0]
# exifread can't extract the embedded thermal image, use exiftool instead
thermal_img_bytes = subprocess.check_output([self.exiftool_path, "-RawThermalImage", "-b", self.flir_img_filename])
thermal_img_stream = io.BytesIO(thermal_img_bytes)
thermal_img = Image.open(thermal_img_stream)
thermal_np = np.array(thermal_img)
# raw values -> temperature
subject_distance = self.default_distance
if 'SubjectDistance' in meta:
subject_distance = FlirImageExtractor.extract_float(meta['SubjectDistance'])
if self.fix_endian:
# fix endianness, the bytes in the embedded png are in the wrong order
thermal_np = np.vectorize(lambda x: (x >> 8) + ((x & 0x00ff) << 8))(thermal_np)
raw2tempfunc = np.vectorize(lambda x: FlirImageExtractor.raw2temp(x, E=meta['Emissivity'], OD=subject_distance,
RTemp=FlirImageExtractor.extract_float(
meta['ReflectedApparentTemperature']),
ATemp=FlirImageExtractor.extract_float(
meta['AtmosphericTemperature']),
IRWTemp=FlirImageExtractor.extract_float(
meta['IRWindowTemperature']),
IRT=meta['IRWindowTransmission'],
RH=FlirImageExtractor.extract_float(
meta['RelativeHumidity']),
PR1=meta['PlanckR1'], PB=meta['PlanckB'],
PF=meta['PlanckF'],
PO=meta['PlanckO'], PR2=meta['PlanckR2']))
thermal_np = raw2tempfunc(thermal_np)
return thermal_np
@staticmethod
def raw2temp(raw, E=1, OD=1, RTemp=20, ATemp=20, IRWTemp=20, IRT=1, RH=50, PR1=21106.77, PB=1501, PF=1, PO=-7340,
PR2=0.012545258):
"""
convert raw values from the flir sensor to temperatures in C
# this calculation has been ported to python from
# https://github.com/gtatters/Thermimage/blob/master/R/raw2temp.R
# a detailed explanation of what is going on here can be found there
"""
# constants
ATA1 = 0.006569
ATA2 = 0.01262
ATB1 = -0.002276
ATB2 = -0.00667
ATX = 1.9
# transmission through window (calibrated)
emiss_wind = 1 - IRT
refl_wind = 0
# transmission through the air
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)))
# radiance from the environment
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)
# temperature from radiance
temp_celcius = PB / log(PR1 / (PR2 * (raw_obj + PO)) + PF) - 273.15
return temp_celcius
@staticmethod
def extract_float(dirtystr):
"""
Extract the float value of a string, helpful for parsing the exiftool data
:return:
"""
digits = re.findall(r"[-+]?\d*\.\d+|\d+", dirtystr)
return float(digits[0])
def plot(self):
"""
Plot the rgb + thermal image (easy to see the pixel values)
:return:
"""
rgb_np = self.get_rgb_np()
thermal_np = self.get_thermal_np()
plt.subplot(1, 2, 1)
plt.imshow(thermal_np, cmap='hot')
plt.subplot(1, 2, 2)
plt.imshow(rgb_np)
plt.show()
def save_images(self):
"""
Save the extracted images
:return:
"""
rgb_np = self.get_rgb_np()
thermal_np = self.extract_thermal_image()
img_visual = Image.fromarray(rgb_np)
thermal_normalized = (thermal_np - np.amin(thermal_np)) / (np.amax(thermal_np) - np.amin(thermal_np))
img_thermal = Image.fromarray(np.uint8(cm.inferno(thermal_normalized) * 255))
fn_prefix, _ = os.path.splitext(self.flir_img_filename)
thermal_filename = fn_prefix + self.thermal_suffix
image_filename = fn_prefix + self.image_suffix
if self.use_thumbnail:
image_filename = fn_prefix + self.thumbnail_suffix
if self.is_debug:
print("DEBUG Saving RGB image to:{}".format(image_filename))
print("DEBUG Saving Thermal image to:{}".format(thermal_filename))
img_visual.save(image_filename)
img_thermal.save(thermal_filename)
def export_thermal_to_csv(self, csv_filename):
"""
Convert thermal data in numpy to json
:return:
"""
with open(csv_filename, 'w') as fh:
writer = csv.writer(fh, delimiter=',')
writer.writerow(['x', 'y', 'temp (c)'])
pixel_values = []
for e in np.ndenumerate(self.thermal_image_np):
x, y = e[0]
c = e[1]
pixel_values.append([x, y, c])
writer.writerows(pixel_values)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Extract and visualize Flir Image data')
parser.add_argument('-i', '--input', type=str, help='Input image. Ex. img.jpg', required=True)
parser.add_argument('-p', '--plot', help='Generate a plot using matplotlib', required=False, action='store_true')
parser.add_argument('-exif', '--exiftool', type=str, help='Custom path to exiftool', required=False,
default='exiftool')
parser.add_argument('-csv', '--extractcsv', help='Export the thermal data per pixel encoded as csv file',
required=False)
parser.add_argument('-d', '--debug', help='Set the debug flag', required=False,
action='store_true')
args = parser.parse_args()
fie = FlirImageExtractor(exiftool_path=args.exiftool, is_debug=args.debug)
fie.process_image(args.input)
if args.plot:
fie.plot()
if args.extractcsv:
fie.export_thermal_to_csv(args.extractcsv)
fie.save_images()