Repository navigation
Expand file tree
/
Copy pathMainWindow.py
More file actions
125 lines (92 loc) · 3.41 KB
/
Copy pathMainWindow.py
File metadata and controls
125 lines (92 loc) · 3.41 KB
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
from PyQt5.QtCore import * # core Qt functionality
from PyQt5.QtWidgets import *
from PyQt5.QtGui import * # extends QtCore with GUI functionality
from PyQt5 import uic
from TrafficLight import *
import cv2
#from detect import detector
from test_detect import detector
from collections import Counter
import sys
import os
DETECTION_TOLERANCE = 5
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs,)
# Load the UI
fileh = QFile(r'ui\main_window.ui')
fileh.open(QFile.ReadOnly)
uic.loadUi(fileh, self)
fileh.close()
# traffic light control
self.counter = 0
self.green = True
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.numPeople.connect(self.updateTrafficState)
th.start()
self.NumberofPedestrians.setFont(QFont('Arial', 16))
self.NumberCars.setFont(QFont('Arial', 16))
# Update views -----------------------------
timer = QTimer(self)
timer.timeout.connect(self.updateLights)
timer.start()
def updateLights(self):
if self.counter > DETECTION_TOLERANCE:
self.green = not(self.green)
if self.green:
self.PedestrianSignalEW.turnGreen()
self.TrafficLightEW.turnGreen()
self.PedestrianSignalNS.turnRed()
self.TrafficLightNS.turnRed()
else:
self.PedestrianSignalEW.turnRed()
self.TrafficLightEW.turnRed()
self.PedestrianSignalNS.turnGreen()
self.TrafficLightNS.turnGreen()
self.TrafficLightEW.update()
self.PedestrianSignalEW.update()
self.PedestrianSignalNS.update()
self.TrafficLightNS.update()
@pyqtSlot(QImage)
def setImage(self, image):
self.VideoLabel.setPixmap(QPixmap.fromImage(image))
@pyqtSlot(dict)
def updateTrafficState(self, data):
# REYNOLD ------------------------------------------
numPed = data[0]
numCars = data[2]
if (numPed > 0 and self.green) or (numPed == 0 and not(self.green)):
self.counter = 0
else:
self.counter += 1
self.NumberofPedestrians.setText('# of Pedestrians Currently Moving E/W: ' + str(numPed))
self.NumberCars.setText('# of Cars Currently Moving or in View: ' + str(numCars))
class Thread(QThread):
changePixmap = pyqtSignal(QImage)
numPeople = pyqtSignal(dict)
def run(self):
cap = cv2.VideoCapture('./testing/t33.mp4')
while (cap.isOpened()):
ret, frame = cap.read()
frame, classIDs = detector(frame)
# REYNOLD ------------------------------------------
self.numPeople.emit(Counter(classIDs))
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgbImage.shape
bytesPerLine = ch * w
convertToQtFormat = QImage(rgbImage.data, w, h, bytesPerLine, QImage.Format_RGB888)
p = convertToQtFormat.scaled(960, 720, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
if cv2.waitKey(1) == 13:
break
if frame is None:
break
def main():
app = QApplication(sys.argv)
main = MainWindow()
#main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()