-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_window.py
More file actions
337 lines (251 loc) · 12.3 KB
/
main_window.py
File metadata and controls
337 lines (251 loc) · 12.3 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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from ui_main_window import Ui_MainWindow
import rc_ex_tr
from control_types import ExerciseType, PositionType, SwitchType
from detection_exercises import *
# In[ ]:
import sys
from PySide2 import QtCore, QtGui, QtWidgets
# In[ ]:
import cv2
import mediapipe as mp
import numpy as np
import math
# In[ ]:
import qimage2ndarray
# In[ ]:
class VideoDetectionThread(QtCore.QThread):
changeCount = QtCore.Signal(int)
changeExercise = QtCore.Signal(int)
changePixmap = QtCore.Signal(QtGui.QImage)
def __init__(self, exercise_list):
super(VideoDetectionThread, self).__init__()
self.exercise_list = exercise_list
self.exercise_index = 0
self.counter = 0
self.detection_show = False
self.video = cv2.VideoCapture()
self.mp_drawing = mp.solutions.drawing_utils
self.mp_pose = mp.solutions.pose
def run(self):
sec = 0
sw_type = None
current_position = None
first_position = None
second_position = None
positions = [PositionType.FIRST, PositionType.SECOND, PositionType.THIRD]
i_pos = 0
with self.mp_pose.Pose(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as pose:
while self.video.isOpened():
ret, frame = self.video.read()
if not ret:
continue
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = pose.process(image)
image.flags.writeable = True
try:
landmarks = results.pose_landmarks.landmark
l_index = [landmarks[self.mp_pose.PoseLandmark.LEFT_INDEX.value].x,
landmarks[self.mp_pose.PoseLandmark.LEFT_INDEX.value].y]
r_index = [landmarks[self.mp_pose.PoseLandmark.RIGHT_INDEX.value].x,
landmarks[self.mp_pose.PoseLandmark.RIGHT_INDEX.value].y]
l_ear = [landmarks[self.mp_pose.PoseLandmark.LEFT_EAR.value].x,
landmarks[self.mp_pose.PoseLandmark.LEFT_EAR.value].y]
r_ear = [landmarks[self.mp_pose.PoseLandmark.RIGHT_EAR.value].x,
landmarks[self.mp_pose.PoseLandmark.RIGHT_EAR.value].y]
l_wrist = [landmarks[self.mp_pose.PoseLandmark.LEFT_WRIST.value].x,
landmarks[self.mp_pose.PoseLandmark.LEFT_WRIST.value].y]
r_wrist = [landmarks[self.mp_pose.PoseLandmark.RIGHT_WRIST.value].x,
landmarks[self.mp_pose.PoseLandmark.RIGHT_WRIST.value].y]
l_shoulder = [landmarks[self.mp_pose.PoseLandmark.LEFT_SHOULDER.value].x,
landmarks[self.mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
r_shoulder = [landmarks[self.mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x,
landmarks[self.mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y]
l_hip = [landmarks[self.mp_pose.PoseLandmark.LEFT_HIP.value].x,
landmarks[self.mp_pose.PoseLandmark.LEFT_HIP.value].y]
r_hip = [landmarks[self.mp_pose.PoseLandmark.RIGHT_HIP.value].x,
landmarks[self.mp_pose.PoseLandmark.RIGHT_HIP.value].y]
r_sw_dist = [dist(r_index, r_ear), dist(l_index, l_hip)]
l_sw_dist = [dist(l_index, l_ear), dist(r_index, r_hip)]
if r_sw_dist[0] < 0.1 and r_sw_dist[1] < 0.2:
sw_type = SwitchType.RIGHT
sec += 1
elif l_sw_dist[0] < 0.1 and l_sw_dist[1] < 0.2:
sw_type = SwitchType.LEFT
sec += 1
else:
sec = 0
if sec == 45:
sec = 0
self.counter = 0
i_pos = 0
if sw_type == SwitchType.RIGHT:
self.exercise_index += 1
elif sw_type == SwitchType.LEFT:
self.exercise_index -= 1
if np.abs(self.exercise_index) == len(self.exercise_list):
self.exercise_index = 0
self.changeExercise.emit(self.exercise_index)
if self.exercise_list[self.exercise_index] == ExerciseType.ARMS:
arms = Arms(l_shoulder, r_shoulder, l_wrist, r_wrist, l_hip, r_hip)
current_position = arms.position()
if current_position:
if not first_position:
first_position = current_position
else:
if current_position == first_position and last_position != first_position:
self.counter += 1;
self.changeCount.emit(self.counter)
last_position = current_position
elif self.exercise_list[self.exercise_index] == ExerciseType.LEFT_ARM:
l_arm = Arm(l_shoulder, l_wrist, l_hip)
if positions[i_pos] == l_arm.position():
i_pos += 1
elif self.exercise_list[self.exercise_index] == ExerciseType.RIGHT_ARM:
r_arm = Arm(r_shoulder, r_wrist, r_hip)
if positions[i_pos] == r_arm.position():
i_pos += 1
if i_pos == len(positions):
i_pos = 0
self.counter += 1
self.changeCount.emit(self.counter)
if self.detection_show:
self.mp_drawing.draw_landmarks(image, results.pose_landmarks, self.mp_pose.POSE_CONNECTIONS,
self.mp_drawing.DrawingSpec(color=(14,66,160), thickness=2, circle_radius=2),
self.mp_drawing.DrawingSpec(color=(164,189,234), thickness=3, circle_radius=2))
except:
pass
q_image = qimage2ndarray.array2qimage(image)
self.changePixmap.emit(q_image)
@QtCore.Slot(int)
def chooseExercise(self, exercise_index):
self.exercise_index = exercise_index
self.counter = 0
@QtCore.Slot(int)
def openVideo(self, camera_index):
max_value_resolution = 10000
self.video.open(camera_index, cv2.CAP_DSHOW)
self.video.set(cv2.CAP_PROP_FRAME_WIDTH, max_value_resolution)
self.video.set(cv2.CAP_PROP_FRAME_HEIGHT, max_value_resolution)
@QtCore.Slot()
def closeVideo(self):
self.video.release()
cv2.destroyAllWindows()
@QtCore.Slot()
def manageDetection(self):
if self.detection_show:
self.detection_show = False
else:
self.detection_show = True
# In[ ]:
class MainWindow(QtWidgets.QMainWindow):
changeExercise = QtCore.Signal(int)
startDetection = QtCore.Signal(int)
stopDetection = QtCore.Signal()
def __init__(self):
super(MainWindow, self).__init__()
self.m_ui = Ui_MainWindow()
self.m_ui.setupUi(self)
self.setWindowIcon(QtGui.QIcon(":/ex_rep.ico"))
self.exercise_list = [ExerciseType.ARMS, ExerciseType.LEFT_ARM, ExerciseType.RIGHT_ARM]
self.exercise_index = 0
self.m_ui.list_ex.setCurrentRow(self.exercise_index)
self.m_ui.l_ex.setText(self.exercise_list[self.exercise_index].value)
self.m_ui.list_ex.addItems([ex.value for ex in self.exercise_list])
self.camera_active = False
self.video_thread = VideoDetectionThread(self.exercise_list)
self.video_resolution = []
self.video_thread.changeCount.connect(self.setCount)
self.video_thread.changeExercise.connect(self.chooseExercise)
self.video_thread.changePixmap.connect(self.setImage)
self.video_thread.finished.connect(self.m_ui.l_video.clear)
self.changeExercise.connect(self.video_thread.chooseExercise)
self.startDetection.connect(self.video_thread.openVideo)
self.stopDetection.connect(self.video_thread.closeVideo)
self.m_ui.list_ex.itemClicked.connect(self.chooseExerciseFromList)
self.m_ui.pb_next_ex.clicked.connect(self.nextExercise)
self.m_ui.pb_prev_ex.clicked.connect(self.prevExercise)
self.m_ui.pb_camera.clicked.connect(self.manageVideo)
self.m_ui.cb_detection.stateChanged.connect(self.video_thread.manageDetection)
def closeEvent(self, event):
if self.camera_active:
QtWidgets.QMessageBox.warning(self,
"Предупреждение",
"Перед закрытием приложения остановите видео!")
event.ignore()
else:
event.accept()
@QtCore.Slot()
def manageVideo(self):
if not self.camera_active:
try:
camera_index = int(self.m_ui.le_cam.text())
self.camera_active = True
self.startDetection.emit(camera_index)
self.video_thread.start()
self.m_ui.pb_camera.setText("Выключить камеру")
except:
QtWidgets.QMessageBox.critical(self,
"Ошибка",
"В качестве номера камеры указано не числовое значение!")
else:
self.camera_active = False
self.stopDetection.emit()
self.video_resolution = []
self.m_ui.pb_camera.setText("Включить камеру")
def setExercise(self):
row = self.exercise_index
if row < 0 :
row = len(self.exercise_list) + row
self.m_ui.list_ex.setCurrentRow(row)
self.m_ui.l_ex.setText(self.exercise_list[self.exercise_index].value)
self.m_ui.l_count.setText("0")
@QtCore.Slot(int)
def chooseExercise(self, exercise_index):
self.exercise_index = exercise_index
self.setExercise()
@QtCore.Slot(QtWidgets.QListWidgetItem)
def chooseExerciseFromList(self, item):
self.exercise_index = self.m_ui.list_ex.row(item)
self.setExercise()
self.changeExercise.emit(self.exercise_index)
@QtCore.Slot()
def nextExercise(self):
self.exercise_index += 1
if self.exercise_index == len(self.exercise_list):
self.exercise_index = 0
self.setExercise()
self.changeExercise.emit(self.exercise_index)
@QtCore.Slot()
def prevExercise(self):
self.exercise_index -= 1
if np.abs(self.exercise_index) == len(self.exercise_list):
self.exercise_index = 0
self.setExercise()
self.changeExercise.emit(self.exercise_index)
@QtCore.Slot(int)
def setCount(self, count):
self.m_ui.l_count.setText(str(count))
@QtCore.Slot(QtGui.QImage)
def setImage(self, image):
pixmap = QtGui.QPixmap.fromImage(image)
if not self.video_resolution:
nod = math.gcd(pixmap.size().width(), pixmap.size().height())
self.video_resolution = [pixmap.size().width() / nod, pixmap.size().height() / nod]
self.m_ui.l_video.setPixmap(pixmap.scaled(
self.m_ui.l_video.size().width(),
self.m_ui.l_video.size().width() / self.video_resolution[0] * self.video_resolution[1]))
# In[ ]:
if __name__ == "__main__":
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())