-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
332 lines (268 loc) · 13.4 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
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
#!/usr/bin/env python
# coding:utf-8
"""
Author : Alessandro Delmonte
Contact : [email protected]
"""
import os
import sys
import cv2
import time
# import gtts
import numpy as np
import pandas as pd
import mediapipe as mp
import tensorflow as tf
from datetime import datetime
from playsound import playsound
from goprocam import GoProCamera, constants
from PySide6.QtCore import QThread, Signal, Slot, QAbstractTableModel, Qt, QModelIndex
from PySide6.QtGui import QAction, QImage, QKeySequence, QPixmap
from PySide6.QtWidgets import (QApplication, QComboBox, QGroupBox, QHBoxLayout, QLabel, QMainWindow, QPushButton,
QSizePolicy, QVBoxLayout, QWidget, QStatusBar, QToolBar, QTableView, QHeaderView)
class KeepAliveThread(QThread):
def __init__(self, parent=None):
QThread.__init__(self, parent)
self.gopro = GoProCamera.GoPro()
def run(self):
self.gopro.stream("udp://127.0.0.1:10000")
class InferThread(QThread):
updateFrame = Signal(QImage)
updateText = Signal(str)
updateTable = Signal(dict)
def __init__(self, parent=None):
QThread.__init__(self, parent)
self.interpreter = None
self.signatures = None
self.input_index = None
self.output_index = None
self.status = True
self.cap = True
self.actions = {0: 'None', 1: 'Start', 2: 'Stop'}
self.goproCamera = GoProCamera.GoPro()
self.tracking = False
# tts = gtts.gTTS("Enregistrement", lang="fr")
# tts.save("tts.mp3")
def print_config(self):
info = self.goproCamera.infoCamera()
self.updateText.emit("Streaming {} ({})".format(info['model_name'], info['serial_number']))
def print_message(self, message):
self.updateText.emit(message)
def set_file(self, fname):
self.interpreter = tf.lite.Interpreter(model_path=fname)
self.interpreter.allocate_tensors()
self.signatures = self.interpreter.get_signature_list()
self.input_index = self.interpreter.get_input_details()[0]["index"]
self.output_index = self.interpreter.get_output_details()[0]["index"]
self.updateText.emit("Model loaded: {}".format(fname))
def run(self):
self.cap = cv2.VideoCapture("udp://127.0.0.1:10000?overrun_nonfatal=1&fifo_size=300000", cv2.CAP_FFMPEG)
while self.status:
tracking_start = None
with mp.solutions.hands.Hands(model_complexity=0, min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while self.cap.isOpened():
success, image = self.cap.read()
if not success:
print("Ignoring empty camera frame.")
else:
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
h, w, c = image.shape
frame_landmarks = np.zeros(shape=(42, 3))
if results.multi_hand_landmarks:
if 0 < len(results.multi_hand_landmarks) < 3:
for j, hand in enumerate(results.multi_hand_landmarks):
for i, landmark in enumerate(hand.landmark):
frame_landmarks[21 * j + i][0] = int(landmark.x * w)
frame_landmarks[21 * j + i][1] = int(landmark.y * h)
frame_landmarks[21 * j + i][2] = landmark.z * w
_max = np.amax(frame_landmarks, axis=0)
_min = np.amin(frame_landmarks, axis=0)
frame_landmarks[:, -1] = 255 * ((frame_landmarks[:, -1] - _min[-1]
) / (_max[-1] - _min[-1]))
for lm in frame_landmarks:
cv2.circle(image, (int(lm[0]), int(lm[1])), 8, thickness=-1, color=[lm[2]] * 3)
_max1 = np.amax(frame_landmarks[:21], axis=0)
_min1 = np.amin(frame_landmarks[:21], axis=0)
cv2.rectangle(image, (int(_min1[0]), int(_min1[1])), (int(_max1[0]), int(_max1[1])),
(255, 0, 0), 3)
_max2 = np.amax(frame_landmarks[21:], axis=0)
_min2 = np.amin(frame_landmarks[21:], axis=0)
cv2.rectangle(image, (int(_min2[0]), int(_min2[1])), (int(_max2[0]), int(_max2[1])),
(255, 0, 0), 3)
frame_landmarks -= frame_landmarks[0]
to_predict = frame_landmarks[:21, :2].flatten()
self.interpreter.set_tensor(self.input_index, np.expand_dims(to_predict,
axis=0).astype(np.float32))
self.interpreter.invoke()
predict_result = self.interpreter.get_tensor(self.output_index)[0]
cv2.putText(image, self.actions[np.argmax(predict_result)], _min1[:2].astype(np.uint16),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
to_predict = frame_landmarks[21:, :2].flatten()
self.interpreter.set_tensor(self.input_index,
np.expand_dims(to_predict, axis=0).astype(np.float32))
self.interpreter.invoke()
predict_result2 = self.interpreter.get_tensor(self.output_index)[0]
cv2.putText(image, self.actions[np.argmax(predict_result2)], _min2[:2].astype(np.uint16),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
if (np.argmax(predict_result2) == 1 or np.argmax(predict_result) == 1) and not self.tracking:
playsound("resources/tts.mp3", block=False)
tracking_start = datetime.now()
self.print_message("Tracking started at {}".format(tracking_start))
self.tracking = True
elif (np.argmax(predict_result2) == 2 or np.argmax(predict_result) == 2) and self.tracking:
time_diff = datetime.now() - tracking_start
time_diff_formatted = "{} minutes {} seconds".format(time_diff.seconds // 60,
time_diff.seconds % 60)
self.print_message("Tracking lasted {}".format(time_diff_formatted))
self.updateTable.emit({'Date Start': [tracking_start],
'Date End': [datetime.now()],
'Date Delta': [time_diff_formatted]})
self.tracking = False
if self.tracking:
cv2.putText(image, "RECORDING", (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 0, 255), 3)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img = QImage(image.data, w, h, c * w, QImage.Format_RGB888)
self.updateFrame.emit(img)
sys.exit(-1)
class PandasModel(QAbstractTableModel):
def __init__(self, dataframe: pd.DataFrame, parent=None):
QAbstractTableModel.__init__(self, parent)
self._dataframe = dataframe
def rowCount(self, parent=QModelIndex()) -> int:
if parent == QModelIndex():
return len(self._dataframe)
return 0
def columnCount(self, parent=QModelIndex()) -> int:
if parent == QModelIndex():
return len(self._dataframe.columns)
return 0
def get_df(self):
return self._dataframe
def data(self, index: QModelIndex, role=Qt.ItemDataRole):
if not index.isValid():
return None
if role == Qt.DisplayRole:
return str(self._dataframe.iloc[index.row(), index.column()])
return None
def headerData(self, section: int, orientation: Qt.Orientation, role: Qt.ItemDataRole):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return str(self._dataframe.columns[section])
if orientation == Qt.Vertical:
return str(self._dataframe.index[section])
return None
def appendRow(self, row, index=QModelIndex()):
self.beginInsertRows(index, self.rowCount(), self.rowCount())
self._dataframe = pd.concat([self._dataframe, pd.DataFrame(row)], ignore_index=True)
self.endInsertRows()
return True
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Patterns detection")
self.setGeometry(0, 0, 800, 500)
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
tool_bar = QToolBar()
self.addToolBar(tool_bar)
self.menu = self.menuBar()
self.menu_file = self.menu.addMenu("File")
exit = QAction("Exit", self, triggered=qApp.quit)
self.menu_file.addAction(exit)
self.menu_about = self.menu.addMenu("&About")
about = QAction("About Qt", self, shortcut=QKeySequence(QKeySequence.HelpContents),
triggered=qApp.aboutQt)
self.menu_about.addAction(about)
self.label = QLabel(self)
self.th = InferThread(self)
self.th.finished.connect(self.close)
self.th.updateFrame.connect(self.set_image)
self.th.updateText.connect(self.set_text)
self.th.updateTable.connect(self.append_row)
self.th_alive = KeepAliveThread(self)
self.th_alive.finished.connect(self.close)
self.group_model = QGroupBox("Trained model")
self.group_model.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
model_layout = QHBoxLayout()
self.combobox = QComboBox()
for file in os.listdir("trained_models"):
if file.endswith(".tflite"):
self.combobox.addItem(os.path.join("trained_models", file))
model_layout.addWidget(QLabel("File:"), 10)
model_layout.addWidget(self.combobox, 90)
self.group_model.setLayout(model_layout)
buttons_layout = QHBoxLayout()
self.button1 = QPushButton("Start")
self.button2 = QPushButton("Stop/Close")
self.button1.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self.button2.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
buttons_layout.addWidget(self.button2)
buttons_layout.addWidget(self.button1)
right_layout = QHBoxLayout()
right_layout.addWidget(self.group_model, 1)
right_layout.addLayout(buttons_layout, 30)
table_layout = QHBoxLayout()
view = QTableView()
view.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
view.setAlternatingRowColors(True)
view.setSelectionBehavior(QTableView.SelectRows)
ds = {'Date Start': [], 'Date End': [], 'Date Delta': []}
df = pd.DataFrame(ds)
self.model = PandasModel(df)
view.setModel(self.model)
table_layout.addWidget(view, 1)
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addLayout(right_layout)
layout.addLayout(table_layout)
widget = QWidget(self)
widget.setLayout(layout)
self.setCentralWidget(widget)
self.button1.clicked.connect(self.start)
self.button2.clicked.connect(self.kill_thread)
self.button2.setEnabled(False)
self.combobox.currentTextChanged.connect(self.set_model)
@Slot()
def set_model(self, text):
self.th.set_file(text)
@Slot()
def kill_thread(self):
print("Finishing...")
self.button2.setEnabled(False)
self.button1.setEnabled(True)
self.th.cap.release()
cv2.destroyAllWindows()
self.status = False
self.th.terminate()
self.th_alive.terminate()
# Give time for the thread to finish
time.sleep(5)
@Slot()
def start(self):
print("Starting...")
self.button2.setEnabled(True)
self.button1.setEnabled(False)
self.th_alive.start()
self.th.set_file(self.combobox.currentText())
self.th.start()
self.th.print_config()
@Slot(QImage)
def set_image(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
@Slot(str)
def set_text(self, text):
self.status_bar.showMessage(text)
@Slot(dict)
def append_row(self, row):
self.model.appendRow(row)
if __name__ == "__main__":
app = QApplication()
w = Window()
w.show()
sys.exit(app.exec())