-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
352 lines (304 loc) · 12.5 KB
/
gui.py
File metadata and controls
352 lines (304 loc) · 12.5 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import cv2
import numpy as np
import sys
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import (QWidget, QHBoxLayout,QPushButton,QFileDialog,
QLabel, QApplication, QMainWindow, QAction, qApp,QVBoxLayout,QCheckBox,QTextEdit,QTableView)
from PyQt5.QtGui import QPixmap,QIcon
from PyQt5.QtCore import (Qt,QFile)
from PyQt5 import QtSql
import time
from datetime import datetime
import xlwt
import report
from io import StringIO,BytesIO
sys.path.append('./Abandoned-Object-Detection')
#from AbandonedObjectDetection import *
sys.path.append('./TrackingPeople')
from TrackingPeople import *
maxHeight=520;
maxWidth=600;
class Gui(QMainWindow):
tracking = "Tracking"
luggage = "Luggage"
actions = "Action Recognition"
falling = "Falling"
log_msg = ""
tracking_flag = False
luggage_flag = False
luggage_coordinates = []
def __init__(self):
super().__init__()
self.initUI()
self.engine = Engine()
self.logger = Logger()
def initUI(self):
"""Initialize GUI user interface elements"""
self.db_view = self.create_DB()
self.sessionID=self.get_SessionId()
global maxHeight
global maxWidth
self.maxHeight=maxHeight
self.maxWidth=maxWidth
show_logs = QPushButton("Open Database",self)
show_logs.clicked.connect(lambda:self.show_logs(self.db_view))
export_db = QPushButton("Export Database",self)
export_db.clicked.connect(lambda:self.savefile())
pixmap = QPixmap("back.jpg")
pixmap=pixmap.scaledToHeight(self.maxHeight)
self.lbl = QLabel(self)
self.lbl.setFixedHeight(self.maxHeight)
self.lbl.setPixmap(pixmap)
hbox = QVBoxLayout(self)
hbox.setAlignment(Qt.AlignCenter);
hbox.addWidget(self.lbl)
parentVideoBox=QWidget(self)
parentVideoBox.setStyleSheet("background-color:#121A21");
parentVideoBox.setLayout(hbox)
vbox = QHBoxLayout(self)
tracking = QCheckBox(self.tracking)
luggage= QCheckBox(self.luggage)
actions = QCheckBox(self.actions)
falling = QCheckBox(self.falling)
vbox.addWidget(tracking)
vbox.addWidget(luggage)
vbox.addWidget(actions)
vbox.addWidget(falling)
vbox.addWidget(show_logs)
vbox.addWidget(export_db)
self.logs = QTextEdit(self)
self.logs.setReadOnly(True)
self.logs.setLineWrapMode(QTextEdit.NoWrap)
self.logs.setMaximumHeight(200)
vbox2 = QVBoxLayout(self)
vbox2.addWidget(parentVideoBox)
vbox2.addLayout(vbox)
vbox2.addWidget(self.logs)
parentBox=QWidget(self)
parentBox.setLayout(vbox2)
self.setCentralWidget(parentBox)
tracking.stateChanged.connect(lambda:self.button_Pressed(tracking))
luggage.stateChanged.connect(lambda:self.button_Pressed(luggage))
actions.stateChanged.connect(lambda:self.button_Pressed(actions))
falling.stateChanged.connect(lambda:self.button_Pressed(falling))
openFile = QAction(QIcon('open.png'), 'Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open new File')
openFile.triggered.connect(self.open_video)
showReport = QAction(QIcon('open.png'), 'Report', self)
showReport.setShortcut('Ctrl+R')
showReport.setStatusTip('Show Report')
showReport.triggered.connect(self.showReport)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(showReport)
self.setGeometry(300, 20, 600, 700)
self.setWindowTitle('Horas Surveillance System')
self.show()
def show_logs(self,tableview):
"""Presents tableview as a pop up"""
tableview.horizontalHeader().setStretchLastSection(True)
tableview.setGeometry(300, 300, 1000, 1000)
tableview.show()
def create_DB(self):
"""Creates SQLite database or uses already created one"""
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("surveillance.db")
db.open()
if not db.open():
QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"),
QtGui.qApp.tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information "
"how to build it.\n\n" "Click Cancel to exit."),
QtGui.QMessageBox.Cancel)
return False
model = QtSql.QSqlTableModel(self,db)
model.setTable("logs")
model.select()
self.model = model
tableview = QTableView()
tableview.setModel(model)
query = QtSql.QSqlQuery()
query.exec_("create table logs(date text , time text ,sessionID number, log text ,stype text,svalue number)")
return tableview
def get_SessionId(self):
"""returns session ID from database"""
query = QtSql.QSqlQuery()
query.exec("SELECT sessionID FROM logs");
if(query.last()):
return(query.value(0)+1)
else:
return(1)
def open_video(self):
"""Choose any video from dataset to be opened"""
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
if fname[0]:
f = open(fname[0], 'r')
with f:
self.changeFileSrc(fname[0],self.engine)
def changeim(self,src):
"""Converts image and embeds it in PyQt interface"""
pixmap = QPixmap(src)
self.lbl.setPixmap(pixmap)
def savefile(self):
"""Export database to an excel file"""
wbk = xlwt.Workbook()
sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
self.add2(sheet)
wbk.save("exportedDB.xls")
def add2(self, sheet):
"""helper method for exporting database to an excel file"""
for currentColumn in range(3):
for currentRow in range(self.model.rowCount()):
try:
teext = str(self.model.data(self.model.index(currentRow, currentColumn)))
sheet.write(currentRow, currentColumn, teext)
except AttributeError:
pass
def showReport(self):
"""displays report"""
self.report=report.report(self.db_view)
self.report.show()
def changeFileSrc(self,src,engine):
"""calls engine to play the choosen video"""
engine.play(src)
def button_Pressed(self,btn):
"""triggers flags to set modules on/off,showing any actions in the logs
it's called when any checkbox is pressed"""
if btn.isChecked() == True:
log_msg = btn.text() + " Started ..."
self.logger.save_log(log_msg)
if btn.text() == self.tracking:
self.tracking_flag = True
pass
elif btn.text() == self.luggage:
self.luggage_flag = True
pass
elif btn.text() == self.actions:
pass
elif btn.text() == self.falling:
pass
else:
log_msg = btn.text() + " Turned Off ..."
self.logger.save_log(log_msg)
if btn.text() == self.tracking:
self.tracking_flag = False
pass
elif btn.text() == self.luggage:
self.luggage_flag = False
pass
elif btn.text() == self.actions:
pass
elif btn.text() == self.falling:
pass
class Logger:
def save_log(self,text, stype='',svalue=0):
"""display text in the gui logs, and saves it offline"""
gui.logs.append(text)
self.insert_DB(text)
def insert_DB(self,text,stype='',svalue=0):
"""creates a query to insert values to database"""
query = QtSql.QSqlQuery()
query.prepare("INSERT INTO logs(date,time,log,stype,svalue,sessionID) VALUES (?,?,?,?,?,?)")
date = datetime.now().date()
time = datetime.now().time()
date = date.strftime('%m/%d/%Y')
time = time.strftime('%H:%M:%S')
query.addBindValue(date)
query.addBindValue(time)
query.addBindValue(text)
query.addBindValue(stype)
query.addBindValue(svalue)
query.addBindValue(gui.sessionID)
if not query.exec_():
print(query.lastError().text())
class Drawer:
def draw(self,frame,coordinates):
"""draws boxes around any abandoned luggage"""
for coordinate in coordinates:
if len(coordinate) == 4:
x,y,w,h = coordinate
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0), 2)
elif len(coordinate) == 5:
x,y,w,h,_ = coordinate
cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 2)
if coordinate not in gui.luggage_coordinates:
gui.logs.append("Unattended Luggage Detected !")
gui.logger.insert_DB("Unattended Luggage Detected !","luggage")
gui.luggage_coordinates.append(coordinate)
def drawPedesterian(self,frame,track_bbs_ids,detections,count):
"""draws boxes around people currently being tracked"""
colours = np.array([[255,0,0],[0,255,0],[0,0,255],[255,255,0],[255,0,255],[0,255,255],[255,255,255],[0,0,0],[128,0,0],[0,128,0],[0,0,128],[128,128,0],[0,128,128],[128,0,128]])
font = cv2.FONT_HERSHEY_SIMPLEX
maxID=0
lastCount=0
lastMax=0
global lastCount
global lastMax
global maxID
reductFramSize=15
for(tracker)in detections:
x1,y1,x2,y2=int(tracker[0]),int(tracker[1]),int(tracker[2]),int(tracker[3]);
for(tracker)in track_bbs_ids:
x1,y1,x2,y2,n=int(tracker[0]),int(tracker[1]),int(tracker[2]),int(tracker[3]),int(tracker[4]),;
if(n>maxID):
maxID=n
color=colours[n%len(colours),:]
B=int(color[0])
R=int(color[1])
G=int(color[2])
cv2.putText(frame,str(n),(x1,y1), font, 1, (R,G,B), 2, cv2.LINE_AA)
cv2.rectangle(frame,(x1+reductFramSize,y1+reductFramSize),(x2-reductFramSize, y2-reductFramSize),(R,G,B),2)
cv2.putText(frame,str(count),(0,50), font, 2, (0,255,0), 2, cv2.LINE_AA)
cv2.putText(frame,str(len(track_bbs_ids)),(0,100), font, 2, (0,0,255), 2, cv2.LINE_AA)
if(lastCount!=count):
lastCount=count
gui.logs.append("people tracked now "+str(len(track_bbs_ids)))
gui.logger.insert_DB("people tracked now "+str(len(track_bbs_ids)),"count",str(len(track_bbs_ids)))
if(lastMax!=maxID):
lastMax=maxID
gui.logs.append("people count from start "+str(count))
gui.logger.insert_DB("people count from start "+str(count),"tracking",str(count))
def cv2_to_qimage(self,cv_img):
global maxWidth
global maxHeight
height, width, bytesPerComponent = cv_img.shape
bgra = np.zeros([height, width, 4], dtype=np.uint8)
bgra[:, :, 0:3] = cv_img
pixmap=QImage(bgra.data, width, height, QImage.Format_RGB32)
pixmap = pixmap.scaledToHeight(maxHeight)
return pixmap
class Engine:
def play(self,src):
"""takes video path as an input, plays it,
calling any needed module to perform specific action on video frames during playing """
vid = cv2.VideoCapture(src)
BG = cv2.imread('./Abandoned-Object-Detection/bg.jpg')
#aod = AbandonedObjectDetection(vid, BG)
tracker=TrackingPeople()
drawer = Drawer()
start_time = time.time()
end_time = time.time()
while (1):
start_time = time.time()
if(start_time-end_time<0.04):
while(time.time()-end_time<0.04):
x=1
ret, frame = vid.read()
if(not(ret)):return
if gui.tracking_flag == True:
trackers,detections,peopleCount=tracker.get_frame(frame)
drawer.drawPedesterian(frame,trackers,detections,peopleCount)
if gui.luggage_flag == True:
objs = aod.get_abandoned_objs(frame)
drawer.draw(frame,objs)
src = drawer.cv2_to_qimage(frame)
gui.changeim(src)
end_time = time.time()
cv2.waitKey(1)
app = QApplication(sys.argv)
app.setStyle('Fusion')
gui = Gui()
sys.exit(app.exec_())