-
Notifications
You must be signed in to change notification settings - Fork 126
/
run.py
291 lines (252 loc) · 12.4 KB
/
run.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
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
# -*- coding: UTF-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from pose.estimator import TfPoseEstimator
from pose.networks import get_graph_path
from utils.sort import Sort
from utils.actions import actionPredictor
from utils.joint_preprocess import *
import sys
import cv2
import numpy as np
import time
import settings
poseEstimator = None
def load_model():
global poseEstimator
poseEstimator = TfPoseEstimator(
get_graph_path('mobilenet_thin'), target_size=(432, 368))
class Ui_MainWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.tracker = Sort(settings.sort_max_age, settings.sort_min_hit)
self.timer_camera = QtCore.QTimer()
self.cap = cv2.VideoCapture()
self.CAM_NUM = 0
self.set_ui()
self.slot_init()
self.__flag_mode = 0
self.fps = 0.00
self.data = {}
self.memory = {}
self.joints = []
self.current = []
self.previous = []
def set_ui(self):
self.__layout_main = QtWidgets.QHBoxLayout()
self.__layout_fun_button = QtWidgets.QVBoxLayout()
self.__layout_data_show = QtWidgets.QVBoxLayout()
self.button_open_camera = QtWidgets.QPushButton(u'相机 OFF')
self.button_mode_1 = QtWidgets.QPushButton(u'姿态估计 OFF')
self.button_mode_2 = QtWidgets.QPushButton(u'多人跟踪 OFF')
self.button_mode_3 = QtWidgets.QPushButton(u'行为识别 OFF')
self.button_close = QtWidgets.QPushButton(u'退出')
self.button_open_camera.setMinimumHeight(50)
self.button_mode_1.setMinimumHeight(50)
self.button_mode_2.setMinimumHeight(50)
self.button_mode_3.setMinimumHeight(50)
self.button_close.setMinimumHeight(50)
self.button_close.move(10, 100)
self.infoBox = QtWidgets.QTextBrowser(self)
self.infoBox.setGeometry(QtCore.QRect(10, 300, 200, 180))
# 信息显示
self.label_show_camera = QtWidgets.QLabel()
self.label_move = QtWidgets.QLabel()
self.label_move.setFixedSize(200, 200)
self.label_show_camera.setFixedSize(settings.winWidth + 1, settings.winHeight + 1)
self.label_show_camera.setAutoFillBackground(True)
self.__layout_fun_button.addWidget(self.button_open_camera)
self.__layout_fun_button.addWidget(self.button_mode_1)
self.__layout_fun_button.addWidget(self.button_mode_2)
self.__layout_fun_button.addWidget(self.button_mode_3)
self.__layout_fun_button.addWidget(self.button_close)
self.__layout_fun_button.addWidget(self.label_move)
self.__layout_main.addLayout(self.__layout_fun_button)
self.__layout_main.addWidget(self.label_show_camera)
self.setLayout(self.__layout_main)
self.label_move.raise_()
self.setWindowTitle(u'实时多人姿态估计与行为识别系统')
def slot_init(self):
self.button_open_camera.clicked.connect(self.button_event)
self.timer_camera.timeout.connect(self.show_camera)
self.button_mode_1.clicked.connect(self.button_event)
self.button_mode_2.clicked.connect(self.button_event)
self.button_mode_3.clicked.connect(self.button_event)
self.button_close.clicked.connect(self.close)
def button_event(self):
sender = self.sender()
if sender == self.button_mode_1 and self.timer_camera.isActive():
if self.__flag_mode != 1:
self.__flag_mode = 1
self.button_mode_1.setText(u'姿态估计 ON')
self.button_mode_2.setText(u'多人跟踪 OFF')
self.button_mode_3.setText(u'行为识别 OFF')
else:
self.__flag_mode = 0
self.button_mode_1.setText(u'姿态估计 OFF')
self.infoBox.setText(u'相机已打开')
elif sender == self.button_mode_2 and self.timer_camera.isActive():
if self.__flag_mode != 2:
self.__flag_mode = 2
self.button_mode_1.setText(u'姿态估计 OFF')
self.button_mode_2.setText(u'多人跟踪 ON')
self.button_mode_3.setText(u'行为识别 OFF')
else:
self.__flag_mode = 0
self.button_mode_2.setText(u'多人跟踪 OFF')
self.infoBox.setText(u'相机已打开')
elif sender == self.button_mode_3 and self.timer_camera.isActive():
if self.__flag_mode != 3:
self.__flag_mode = 3
self.button_mode_1.setText(u'姿态估计 OFF')
self.button_mode_2.setText(u'多人跟踪 OFF')
self.button_mode_3.setText(u'行为识别 ON')
else:
self.__flag_mode = 0
self.button_mode_3.setText(u'行为识别 OFF')
self.infoBox.setText(u'相机已打开')
else:
self.__flag_mode = 0
self.button_mode_1.setText(u'姿态估计 OFF')
self.button_mode_2.setText(u'多人跟踪 OFF')
self.button_mode_3.setText(u'行为识别 OFF')
if self.timer_camera.isActive() == False:
flag = self.cap.open(self.CAM_NUM)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, settings.winWidth)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, settings.winHeight)
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera.start(1)
self.button_open_camera.setText(u'相机 ON')
self.infoBox.setText(u'相机已打开')
else:
self.timer_camera.stop()
self.cap.release()
self.label_show_camera.clear()
self.button_open_camera.setText(u'相机 OFF')
self.infoBox.setText(u'相机已关闭')
def show_camera(self):
start = time.time()
ret, frame = self.cap.read()
show = cv2.resize(frame, (settings.winWidth, settings.winHeight))
show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
if ret:
if self.__flag_mode == 1:
self.infoBox.setText(u'当前为人体姿态估计模式')
humans = poseEstimator.inference(show)
show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False)
elif self.__flag_mode == 2:
self.infoBox.setText(u'当前为多人跟踪模式')
humans = poseEstimator.inference(show)
show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton(show, humans, imgcopy=False)
height = show.shape[0]
width = show.shape[1]
if bboxes:
result = np.array(bboxes)
det = result[:, 0:5]
det[:, 0] = det[:, 0] * width
det[:, 1] = det[:, 1] * height
det[:, 2] = det[:, 2] * width
det[:, 3] = det[:, 3] * height
trackers = self.tracker.update(det)
for d in trackers:
xmin = int(d[0])
ymin = int(d[1])
xmax = int(d[2])
ymax = int(d[3])
label = int(d[4])
cv2.rectangle(show, (xmin, ymin), (xmax, ymax),
(int(settings.c[label % 32, 0]),
int(settings.c[label % 32, 1]),
int(settings.c[label % 32, 2])), 4)
elif self.__flag_mode == 3:
self.infoBox.setText(u'当前为人体行为识别模式')
humans = poseEstimator.inference(show)
ori = np.copy(show)
show, joints, bboxes, xcenter, sk= TfPoseEstimator.get_skeleton(show, humans, imgcopy=False)
height = show.shape[0]
width = show.shape[1]
if bboxes:
result = np.array(bboxes)
det = result[:, 0:5]
det[:, 0] = det[:, 0] * width
det[:, 1] = det[:, 1] * height
det[:, 2] = det[:, 2] * width
det[:, 3] = det[:, 3] * height
trackers = self.tracker.update(det)
self.current = [i[-1] for i in trackers]
if len(self.previous) > 0:
for item in self.previous:
if item not in self.current and item in self.data:
del self.data[item]
if item not in self.current and item in self.memory:
del self.memory[item]
self.previous = self.current
for d in trackers:
xmin = int(d[0])
ymin = int(d[1])
xmax = int(d[2])
ymax = int(d[3])
label = int(d[4])
try:
j = np.argmin(np.array([abs(i - (xmax + xmin) / 2.) for i in xcenter]))
except:
j = 0
if joint_filter(joints[j]):
joints[j] = joint_completion(joint_completion(joints[j]))
if label not in self.data:
self.data[label] = [joints[j]]
self.memory[label] = 0
else:
self.data[label].append(joints[j])
if len(self.data[label]) == settings.L:
pred = actionPredictor().move_status(self.data[label])
if pred == 0:
pred = self.memory[label]
else:
self.memory[label] = pred
self.data[label].pop(0)
location = self.data[label][-1][1]
if location[0] <= 30:
location = (51, location[1])
if location[1] <= 10:
location = (location[0], 31)
cv2.putText(show, settings.move_status[pred], (location[0] - 30, location[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8,
(0, 255, 0), 2)
cv2.rectangle(show, (xmin, ymin), (xmax, ymax),
(int(settings.c[label % 32, 0]),
int(settings.c[label % 32, 1]),
int(settings.c[label % 32, 2])), 4)
end = time.time()
self.fps = 1. / (end - start)
cv2.putText(show, 'FPS: %.2f' % self.fps, (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
def closeEvent(self, event):
ok = QtWidgets.QPushButton()
cancel = QtWidgets.QPushButton()
msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, u"关闭", u"是否关闭!")
msg.addButton(ok, QtWidgets.QMessageBox.ActionRole)
msg.addButton(cancel, QtWidgets.QMessageBox.RejectRole)
ok.setText(u'确定')
cancel.setText(u'取消')
if msg.exec_() == QtWidgets.QMessageBox.RejectRole:
event.ignore()
else:
if self.cap.isOpened():
self.cap.release()
if self.timer_camera.isActive():
self.timer_camera.stop()
event.accept()
print("System exited.")
if __name__ == '__main__':
load_model()
print("Load all models done!")
print("The system starts ro run.")
app = QtWidgets.QApplication(sys.argv)
ui = Ui_MainWindow()
ui.show()
sys.exit(app.exec_())