-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
101 lines (82 loc) · 2.96 KB
/
main.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
'''
A simple player that mixes the Pi camera output with the mlx90640 thermal
camera output into one stream. Each capture is run in its own thread and
their outputs (received using Signal/Slot mechanism) are combined in the
player thread. The refersh happends on the receiver of the Pi camera since
it has a faster capture rate. A mutex is used to serialize access to thermal
image pixmap.
'''
import sys
import threading
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
from PyQt5.QtCore import QThread, Qt, pyqtSignal, pyqtSlot, QPoint
from PyQt5.QtGui import QPixmap, QPainter
from pixread import VidThread
from mlxread import MLXThread
class ThermalPlayer(QWidget):
def __init__(self):
super().__init__()
self.title = 'Thermal Cam'
self.left = 50
self.top = 50
self.leftmargin = 100
self.topmargin = 50
self.width = 640
self.height = 480
self.heatpix = QPixmap(640,480)
self.heatpix.fill(Qt.transparent)
self.campix = None
self.heatpixlock = threading.Lock()
self.initUI()
@pyqtSlot(QPixmap)
def setImage(self, pixmap):
# Currently campix isn't used, but if used
# it needs to be deep copied.
self.campix = pixmap.copy()
# Image compositing
dispix = pixmap
painter = QPainter(dispix)
painter.setRenderHint(QPainter.Antialiasing)
# painter.setCompositionMode(QPainter.CompositionMode_Plus)
painter.setCompositionMode(QPainter.CompositionMode_Screen)
# painter.setCompositionMode(QPainter.CompositionMode_HardLight)
# painter.setCompositionMode(QPainter.CompositionMode_SourceAtop)
# setHeat callback also accesses heatpix
with self.heatpixlock:
painter.drawPixmap(QPoint(), self.heatpix)
painter.end()
self.label.setPixmap(dispix)
@pyqtSlot(QPixmap)
def setHeat(self, pixmap):
# setImage callback also accesses heatpix
with self.heatpixlock:
self.heatpix = pixmap.copy()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.resize(self.width+2*self.leftmargin,self.height+2*self.topmargin)
self.label = QLabel(self)
self.label.move(self.leftmargin,self.topmargin)
self.label.resize(self.width, self.height)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = ThermalPlayer()
print("Video Thread")
vthread = QThread()
vthread.start()
vth = VidThread()
vth.width = myapp.width
vth.height = myapp.height
vth.mirror = True
vth.changePixmap.connect(myapp.setImage)
vth.moveToThread(vthread)
vth.go.emit()
print("MLX Thread")
mlxthread = QThread()
mlxthread.start()
mlxt = MLXThread()
mlxt.changeHeatmap.connect(myapp.setHeat)
mlxt.moveToThread(mlxthread)
mlxt.go.emit()
sys.exit(app.exec_())