-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermal_data.py
166 lines (135 loc) · 5.22 KB
/
thermal_data.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
"""Image data processing for the thermal camera.
The methods below were copied from https://github.com/gobuyun/seeed_ircamera/blob/master/seeed_python_ircamera.py. A
copy of this code can be found in this repo in the file: `seeed_python_ircamera.py`.
"""
import colorsys
from PIL import Image
class ThermalData:
def __init__(self):
pass
@staticmethod
def mapValue(value, curMin, curMax, desMin, desMax):
curDistance = value - curMax
if curDistance == 0:
return desMax
curRange = curMax - curMin
direction = 1 if curDistance > 0 else -1
ratio = curRange / curDistance
desRange = desMax - desMin
value = desMax + (desRange / ratio)
return value
@staticmethod
def constrain(value, down, up):
value = up if value > up else value
value = down if value < down else value
return value
@staticmethod
def isDigital(value):
try:
if value == "nan":
return False
else:
float(value)
return True
except ValueError:
return False
def process_data(self, hetData):
maxHet = 0
minHet = 500
tempData = []
nanCount = 0
if len(hetData) < 768:
return
for i in range(0, 768):
curCol = i % 32
newValueForNanPoint = 0
curData = None
if i < len(hetData) and self.isDigital(hetData[i]):
curData = float(hetData[i])
else:
interpolationPointCount = 0
sumValue = 0
# print("curCol",curCol,"i",i)
abovePointIndex = i - 32
if (abovePointIndex > 0):
if hetData[abovePointIndex] is not "nan":
interpolationPointCount += 1
sumValue += float(hetData[abovePointIndex])
belowPointIndex = i + 32
if (belowPointIndex < 768):
print(" ")
if hetData[belowPointIndex] is not "nan":
interpolationPointCount += 1
sumValue += float(hetData[belowPointIndex])
leftPointIndex = i - 1
if (curCol != 31):
if hetData[leftPointIndex] is not "nan":
interpolationPointCount += 1
sumValue += float(hetData[leftPointIndex])
rightPointIndex = i + 1
if (belowPointIndex < 768):
if (curCol != 0):
if hetData[rightPointIndex] is not "nan":
interpolationPointCount += 1
sumValue += float(hetData[rightPointIndex])
curData = sumValue / interpolationPointCount
# For debug :
# print(abovePointIndex,belowPointIndex,leftPointIndex,rightPointIndex)
# print("newValueForNanPoint",newValueForNanPoint," interpolationPointCount" , interpolationPointCount ,"sumValue",sumValue)
nanCount += 1
tempData.append(curData)
maxHet = tempData[i] if tempData[i] > maxHet else maxHet
minHet = tempData[i] if tempData[i] < minHet else minHet
if maxHet == 0 or minHet == 500:
return
# For debug :
# if nanCount > 0 :
# print("____@@@@@@@ nanCount " ,nanCount , " @@@@@@@____")
return {
"frame": tempData,
"maxHet": maxHet,
"minHet": minHet
}
def get_data(self, data):
# with open('thermal2.csv') as csvfile:
# data = list(csv.reader(csvfile))[0] # 768 values
# Make some RGB values.
# # Cycle through hue vertically & saturation horizontally
# colors = []
# for i in data:
# # Convert color from HSV to RGB
# rgb = colorsys.hsv_to_rgb(float(i) / 360, 1, 1)
# rgb = [int(0.5 + 255 * u) for u in rgb]
# colors.extend(rgb)
#
# # Convert list to bytes
# colors = bytes(colors)
# img = Image.frombytes('RGB', (32, 24), colors)
# img.show()
# img.save('hues.png')
frame = self.process_data(data)
maxHet = frame["maxHet"]
minHet = frame["minHet"]
frame = frame["frame"]
pixelSize = 15
width = 480
height = 360
minHue = 180
maxHue = 360
# tempData = constrain(mapValue(frame[index], minHet, maxHet, minHue, maxHue), minHue, maxHue)
colors = []
for i in frame:
# Convert color from HSV to RGB
tempData = self.constrain(self.mapValue(i, minHet, maxHet, minHue, maxHue), minHue, maxHue)
rgb = colorsys.hsv_to_rgb(tempData / 360, 1.0, 1.0)
rgb = [int(0.5 + 255 * u) for u in rgb]
colors.extend(rgb)
# Convert list to bytes
colors = bytes(colors)
img = Image.frombytes('RGB', (32, 24), colors)
img = img.rotate(90)
img = img.transpose(Image.FLIP_LEFT_RIGHT)
img = img.resize((width, height))
return img
# img.show()
# img.save('hues2.png')