-
Notifications
You must be signed in to change notification settings - Fork 1
/
viewer.py
63 lines (49 loc) · 1.7 KB
/
viewer.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
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
from torch.multiprocessing import Queue
import time
class Viewer(QtGui.QMainWindow):
"""
A high performance viewer that supports remote rendering (X-forwarding)
"""
def __init__(self, h, w, buffer: Queue, parent=None):
super(Viewer, self).__init__(parent)
self.buffer = buffer
#### Create Gui Elements ###########
self.mainbox = QtGui.QWidget()
self.setCentralWidget(self.mainbox)
self.mainbox.setLayout(QtGui.QVBoxLayout())
self.canvas = pg.GraphicsLayoutWidget()
self.mainbox.layout().addWidget(self.canvas)
self.label = QtGui.QLabel()
self.mainbox.layout().addWidget(self.label)
self.view = self.canvas.addViewBox()
self.view.setAspectLocked(True)
self.view.setRange(QtCore.QRectF(0, 0, h, w))
# Image plot
self.img = pg.ImageItem(border='w')
self.view.addItem(self.img)
self.counter = 0
self.fps = 0.
self.lastupdate = time.time()
#### Start #####################
self._update()
def _update(self):
# Get image
data = self.buffer.get(timeout=10)
if data is None:
return
self.img.setImage(data)
# Calculate FPS
now = time.time()
dt = (now-self.lastupdate)
if dt <= 0:
dt = 0.000000000001
fps2 = 1.0 / dt
self.lastupdate = now
self.fps = self.fps * 0.9 + fps2 * 0.1
tx = 'Mean Frame Rate: {fps:.3f} FPS'.format(fps=self.fps)
self.label.setText(tx)
# Next frame
QtCore.QTimer.singleShot(1, self._update)
self.counter += 1