-
Notifications
You must be signed in to change notification settings - Fork 8
/
seeed_python_ircamera.py
322 lines (289 loc) · 11.5 KB
/
seeed_python_ircamera.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import sys
import threading
import seeed_mlx90640
from serial import Serial
from PyQt5.QtWidgets import (
QApplication,
QGraphicsView,
QGraphicsScene,
QGraphicsPixmapItem,
QGraphicsTextItem,
QGraphicsEllipseItem,
QGraphicsLineItem,
QGraphicsBlurEffect
)
from PyQt5.QtGui import QPainter, QBrush, QColor, QFont, QPixmap
from PyQt5.QtCore import QThread, QObject, pyqtSignal, QPointF, Qt
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
def constrain(value, down, up):
value = up if value > up else value
value = down if value < down else value
return value
def isDigital(value):
try:
if value == "nan":
return False
else:
float(value)
return True
except ValueError:
return False
hetaData = []
lock = threading.Lock()
minHue = 180
maxHue = 360
class DataReader(QThread):
drawRequire = pyqtSignal()
I2C = 0,
SERIAL = 1
MODE = I2C
def __init__(self, port):
super(DataReader, self).__init__()
self.frameCount = 0
# i2c mode
if port is None:
self.dataHandle = seeed_mlx90640.grove_mxl90640()
self.dataHandle.refresh_rate = seeed_mlx90640.RefreshRate.REFRESH_0_5_HZ
self.readData = self.i2cRead
else:
self.MODE = DataReader.SERIAL
self.port = port
self.dataHandle = Serial(self.port, 2000000, timeout=5)
self.readData = self.serialRead
def i2cRead(self):
hetData = [0]*768
self.dataHandle.getFrame(hetData)
return hetData
def serialRead(self):
hetData = self.dataHandle.read_until(terminator=b'\r\n')
hetData = str(hetData, encoding="utf8").split(",")
hetData = hetData[:-1]
return hetData
def run(self):
# throw first frame
self.readData()
while True:
maxHet = 0
minHet = 500
tempData = []
nanCount = 0
hetData = self.readData()
if len(hetData) < 768 :
continue
for i in range(0, 768):
curCol = i % 32
newValueForNanPoint = 0
curData = None
if i < len(hetData) and 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:
continue
# For debug :
# if nanCount > 0 :
# print("____@@@@@@@ nanCount " ,nanCount , " @@@@@@@____")
lock.acquire()
hetaData.append(
{
"frame": tempData,
"maxHet": maxHet,
"minHet": minHet
}
)
lock.release()
self.drawRequire.emit()
self.frameCount = self.frameCount + 1
print("data->" + str(self.frameCount))
self.com.close()
class painter(QGraphicsView):
narrowRatio = int(sys.argv[4]) if len(sys.argv) >= 5 else 1
useBlur = sys.argv[5] != "False" if len(sys.argv) >= 6 else True
pixelSize = int(15 / narrowRatio)
width = int (480 / narrowRatio)
height = int(360 / narrowRatio)
fontSize = int(30 / narrowRatio)
anchorLineSize = int(100 / narrowRatio)
ellipseRadius = int(8 / narrowRatio)
textInterval = int(90 / narrowRatio)
col = width / pixelSize
line = height / pixelSize
centerIndex = int(round(((line / 2 - 1) * col) + col / 2))
frameCount = 0
baseZValue = 0
textLineHeight = fontSize + 10
blurRaduis = 50 # Smoother improvement
def __init__(self):
super(painter, self).__init__()
self.setFixedSize(self.width, self.height + self.textLineHeight)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scene = QGraphicsScene()
self.setScene(self.scene)
# center het text item
self.centerTextItem = QGraphicsTextItem()
self.centerTextItem.setPos(self.width / 2 - self.fontSize, 0)
self.centerTextItem.setZValue(self.baseZValue + 1)
self.scene.addItem(self.centerTextItem)
# center anchor item
centerX = self.width / 2
centerY = self.height / 2
self.ellipseItem = QGraphicsEllipseItem(
0, 0,
self.ellipseRadius * 2,
self.ellipseRadius * 2
)
self.horLineItem = QGraphicsLineItem(0, 0, self.anchorLineSize, 0)
self.verLineItem = QGraphicsLineItem(0, 0, 0, self.anchorLineSize)
self.ellipseItem.setPos(
centerX - self.ellipseRadius,
centerY - self.ellipseRadius
)
self.horLineItem.setPos(centerX - self.anchorLineSize / 2, centerY)
self.verLineItem.setPos(centerX, centerY - self.anchorLineSize / 2)
self.ellipseItem.setPen(QColor(Qt.white))
self.horLineItem.setPen(QColor(Qt.white))
self.verLineItem.setPen(QColor(Qt.white))
self.ellipseItem.setZValue(self.baseZValue + 1)
self.horLineItem.setZValue(self.baseZValue + 1)
self.verLineItem.setZValue(self.baseZValue + 1)
self.scene.addItem(self.ellipseItem)
self.scene.addItem(self.horLineItem)
self.scene.addItem(self.verLineItem)
# camera item
self.cameraBuffer = QPixmap(self.width, self.height + self.textLineHeight)
self.cameraItem = QGraphicsPixmapItem()
if self.useBlur:
self.gusBlurEffect = QGraphicsBlurEffect()
self.gusBlurEffect.setBlurRadius(self.blurRaduis)
self.cameraItem.setGraphicsEffect(self.gusBlurEffect)
self.cameraItem.setPos(0, 0)
self.cameraItem.setZValue(self.baseZValue)
self.scene.addItem(self.cameraItem)
# het text item
self.hetTextBuffer = QPixmap(self.width, self.textLineHeight)
self.hetTextItem = QGraphicsPixmapItem()
self.hetTextItem.setPos(0, self.height)
self.hetTextItem.setZValue(self.baseZValue)
self.scene.addItem(self.hetTextItem)
def draw(self):
if len(hetaData) == 0:
return
font = QFont()
color = QColor()
font.setPointSize(self.fontSize)
font.setFamily("Microsoft YaHei")
font.setLetterSpacing(QFont.AbsoluteSpacing, 0)
index = 0
lock.acquire()
frame = hetaData.pop(0)
lock.release()
maxHet = frame["maxHet"]
minHet = frame["minHet"]
frame = frame["frame"]
p = QPainter(self.cameraBuffer)
p.fillRect(
0, 0, self.width,
self.height + self.textLineHeight,
QBrush(QColor(Qt.black))
)
# draw camera
color = QColor()
for yIndex in range(int(self.height / self.pixelSize)):
for xIndex in range(int(self.width / self.pixelSize)):
tempData = constrain(mapValue(frame[index], minHet, maxHet, minHue, maxHue), minHue, maxHue)
color.setHsvF(tempData / 360, 1.0, 1.0)
p.fillRect(
xIndex * self.pixelSize,
yIndex * self.pixelSize,
self.pixelSize, self.pixelSize,
QBrush(color)
)
index = index + 1
self.cameraItem.setPixmap(self.cameraBuffer)
# draw text
p = QPainter(self.hetTextBuffer)
p.fillRect(
0, 0, self.width,
self.height + self.textLineHeight,
QBrush(QColor(Qt.black))
)
hetDiff = maxHet - minHet
bastNum = round(minHet)
interval = round(hetDiff / 5)
for i in range(5):
hue = constrain(mapValue((bastNum + (i * interval)), minHet, maxHet, minHue, maxHue), minHue, maxHue)
color.setHsvF(hue / 360, 1.0, 1.0)
p.setPen(color)
p.setFont(font)
p.drawText(i * self.textInterval, self.fontSize + 3, str(bastNum + (i * interval)) + "°")
self.hetTextItem.setPixmap(self.hetTextBuffer)
# draw center het text
cneter = round(frame[self.centerIndex], 1)
centerText = "<font color=white>%s</font>"
self.centerTextItem.setFont(font)
self.centerTextItem.setHtml(centerText % (str(cneter) + "°"))
self.frameCount = self.frameCount + 1
print("picture->"+str(self.frameCount))
def run():
global minHue
global maxHue
if len(sys.argv) >= 2 and sys.argv[1] == "-h":
print("Usage: %s [PortName] [minHue] [maxHue] [NarrowRatio] [UseBlur]" % sys.argv[0])
exit(0)
if len(sys.argv) >= 4:
minHue = int(sys.argv[2])
maxHue = int(sys.argv[3])
if len(sys.argv) >= 2:
port = sys.argv[1]
else:
port = None
app = QApplication(sys.argv)
window = painter()
dataThread = DataReader(port)
dataThread.drawRequire.connect(window.draw)
dataThread.start()
window.show()
app.exec_()
run()