/
App.py
executable file
·101 lines (79 loc) · 2.89 KB
/
App.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
# coding = utf-8
import sys
from PyQt5.QtCore import *
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from Controller import Controller
from UiWindow import MyWindow, MyMainWindow
import queue
from PIL import Image
class App(MyMainWindow):
__CACHE_QUEUE_LENGTH = 40
__CACHE_INTERVAL = 20
__REFRESH_INTERVAL = 40
def __init__(self, *args, **kwargs):
super(App, self).__init__(*args, **kwargs)
self.controller = Controller()
self.establishConnections()
def establishConnections(self):
self.myWindow.buttonLabel.connect_customized_slot(self.start)
self.showPoseAct.triggered.connect(self.showPoseActTrigger)
self.showBboxAct.triggered.connect(self.showBboxActTrigger)
def showPoseActTrigger(self, state):
self.controller.showPose = True if state else False
def showBboxActTrigger(self, state):
self.controller.showBox = True if state else False
def refresh(self, imageInfos):
for imageInfo in imageInfos:
self.__refreshScreen(imageInfo)
def __refreshScreen(self, info: dict):
screen = self.myWindow.screenByCamera[info["camera"]]
screen.setActionLabel(info.get('label'))
screen.setImage(info['image'])
def __startRefresh(self):
self.refreshTimer = QTimer()
self.refreshTimer.timeout.connect(self.process)
self.refreshTimer.start(App.__REFRESH_INTERVAL)
def __startCache(self):
self.cacheTimer = QTimer()
self.cacheTimer.timeout.connect(self.writeToCacheQueue)
self.cacheQueue = queue.Queue()
self.isFirst = True
self.cacheTimer.start(App.__CACHE_INTERVAL)
def start(self):
self.controller.start()
self.__startCache()
self.__startRefresh()
def receiveData(self):
infodict = self.controller.recv()['data']
for _infodict in infodict:
image = self.ndarrayToQPixmap(_infodict["image"])
_infodict.update({'image': image})
return infodict
def process(self):
if self.cacheQueue.qsize() >= App.__CACHE_QUEUE_LENGTH and self.isFirst:
self.send()
self.isFirst = False
elif not self.isFirst:
self.send()
def send(self):
try:
data = self.cacheQueue.get(block=False)
except queue.Empty:
data = []
self.refresh(data)
def writeToCacheQueue(self):
data = self.receiveData()
if data:
self.cacheQueue.put(data)
print('queue size ', self.cacheQueue.qsize())
def ndarrayToQPixmap(self, image):
return Image.fromarray(image).toqpixmap()
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
self.controller.terminalProcesses()
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
application = App()
application.show()
sys.exit(app.exec_())