-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.py
275 lines (206 loc) · 10.7 KB
/
mainwindow.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
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QMainWindow, QMenu, QSizePolicy, QFileDialog, QProgressDialog,QSplitter, QApplication
from PyQt6.QtGui import QAction, QKeyEvent,QShortcut,QKeySequence,QWheelEvent,QUndoStack,QUndoCommand
import cv2
from ultralytics import YOLO
import pandas as pd
from imagewidget import ImageWidget
from timeline import TimelineWidget
def detectLabels(videoPath):
model = YOLO("yolov8x-pose-p6.pt")
video = cv2.VideoCapture(videoPath)
framesCount = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
fps = video.get(cv2.CAP_PROP_FPS)
print(f"Frames count: {framesCount}")
progress = QProgressDialog("Tracking", "Cancel", 0, framesCount)
progress.setWindowModality(Qt.WindowModality.WindowModal)
labelsDict = {"frame": [], "track_id": [], "x": [], "y": [], "h": [], "w": [], "label": []}
for j in range(17):
labelsDict[f"kp{j}x"] = []
labelsDict[f'kp{j}y']= []
labelsDict[f'conf{j}']=[]
for i in range(framesCount):
progress.setValue(i)
if progress.wasCanceled():
break
ret, frame = video.read()
if not ret:
progress.cancel()
break
res = model.track(frame, persist=True)
if res[0].boxes.id is not None: # Add this check
cls = res[0].boxes.cls.int().cpu().tolist()
boxes = res[0].boxes.xywh.cpu()
track_ids = res[0].boxes.id.int().cpu().tolist()
keypoints = res[0].keypoints.xy.cpu().tolist()
confs = res[0].keypoints.conf.cpu().tolist()
else:
cls = []
boxes = []
track_ids = []
keypoints = []
confs = []
for cl, box, track_id, kp, conf in zip(cls, boxes, track_ids, keypoints, confs):
if cl != 0:
continue
x, y, w, h = box
labelsDict['frame'].append(i)
labelsDict['track_id'].append(track_id)
labelsDict['x'].append(x.item())
labelsDict['y'].append(y.item())
labelsDict['w'].append(w.item())
labelsDict['h'].append(h.item())
labelsDict['label'].append(0)
for j in range(17):
labelsDict[f"kp{j}x"].append(kp[j][0])
labelsDict[f'kp{j}y'].append(kp[j][1])
labelsDict[f'conf{j}'].append(conf[j])
else:
progress.setValue(framesCount)
video.release()
return (framesCount, fps, labelsDict)
class MainWindow(QMainWindow):
def __init__(self) -> None:
super().__init__()
self.setWindowTitle("Labeler")
menuBar = self.menuBar()
fileMenu = QMenu("File", self)
menuBar.addMenu(fileMenu)
openVideo = QAction("Open video and calculate labels", self)
importLabels = QAction("Open video and import labels", self)
exportLabels = QAction("Export labels to csv", self)
openVideo.triggered.connect(self.openVideoCB)
importLabels.triggered.connect(self.importLabelsCb)
exportLabels.triggered.connect(self.exportLabelsCb)
fileMenu.addActions([openVideo, importLabels, exportLabels])
self.imageWidget = ImageWidget()
self.timelineWidget = TimelineWidget()
self.timelineWidget.frameSelected.connect(self.imageWidget.setFrame)
self.timelineWidget.keypointsDisplay.selectedBboxUpdate.connect(self.imageWidget.selectBBox)
self.imageWidget.selectedBBoxIdChanged.connect(self.timelineWidget.keypointsDisplay.selectBBox)
self.imageWidget.sequencesChanged.connect(self.timelineWidget.labelList.set_bboxes_cnt)
self.timelineWidget.keypointsDisplay.imageWidgetRepaint.connect(self.imageWidget.repaint)
self.timelineWidget.keypointsDisplay.setFrame.connect(self.setFrame)
self.timelineWidget.keypointsDisplay.tableUpdate.connect(self.make_undo_command)
self.imageWidget.tableUpdate.connect(self.make_undo_command)
self.imageWidget.timelineRepaint.connect(self.timelineWidget.keypointsDisplay.repaint)
self.mUndoStack = QUndoStack(self)
self.mUndoStack.setUndoLimit(10)
self.undoShortcut = QShortcut(QKeySequence("Ctrl+Z"), self)
self.undoShortcut.activated.connect(self.mUndoStack.undo)
self.redoShortcut = QShortcut(QKeySequence("Ctrl+Y"), self)
self.redoShortcut.activated.connect(self.mUndoStack.redo)
self.deleteShortcut = QShortcut(QKeySequence("Del"), self)
self.deleteShortcut.activated.connect(self.timelineWidget.keypointsDisplay.delete_keypoint)
self.deleteSequenceShortcut = QShortcut(QKeySequence("Backspace"), self)
self.deleteSequenceShortcut.activated.connect(self.timelineWidget.keypointsDisplay.delete_sequance)
self.newSequenceShortcut = QShortcut(QKeySequence("N"), self)
self.newSequenceShortcut.activated.connect(self.timelineWidget.keypointsDisplay.add_sequance)
self.newKeypointShortcut = QShortcut(QKeySequence('A'), self)
self.newKeypointShortcut.activated.connect(self.timelineWidget.keypointsDisplay.add_new_keypoint)
self.selectDownSequenceShortcut = QShortcut(QKeySequence("Down"), self)
self.selectDownSequenceShortcut.activated.connect(self.selectDownSequence)
self.selectUpSequenceShortcut = QShortcut(QKeySequence("Up"), self)
self.selectUpSequenceShortcut.activated.connect(self.selectUpSequence)
self.selectNextFrameShortcut = QShortcut(QKeySequence("Right"), self)
self.selectNextFrameShiftedShortcut = QShortcut(QKeySequence("Shift+Right"), self)
self.selectNextFrameShortcut.activated.connect(self.selectNextFrame)
self.selectNextFrameShiftedShortcut.activated.connect(self.selectNextFrame)
self.selectPrevFrameShortcut = QShortcut(QKeySequence("Left"), self)
self.selectPrevFrameShiftedShortcut = QShortcut(QKeySequence("Shift+Left"), self)
self.selectPrevFrameShortcut.activated.connect(self.selectPrevFrame)
self.selectPrevFrameShiftedShortcut.activated.connect(self.selectPrevFrame)
for i in range(9):
digitShortcut = QShortcut(QKeySequence(f"{i+1}"), self)
digitShortcut.activated.connect(lambda self = self,i=i: self.timelineWidget.keypointsDisplay.draw_class(i))
mainSplitter = QSplitter(Qt.Orientation.Vertical, self)
mainSplitter.setStretchFactor(0, 1)
mainSplitter.setStretchFactor(1, 0)
print(mainSplitter.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding))
mainSplitter.addWidget(self.imageWidget)
mainSplitter.addWidget(self.timelineWidget)
self.setCentralWidget(mainSplitter)
self.sequences = []
def selectDownSequence(self):
if self.timelineWidget.keypointsDisplay.selected_bbox + 1 < len(self.timelineWidget.keypointsDisplay.sequences):
self.timelineWidget.keypointsDisplay.selectBBox(self.timelineWidget.keypointsDisplay.selected_bbox+1)
def selectUpSequence(self):
if self.timelineWidget.keypointsDisplay.selected_bbox > 0:
self.timelineWidget.keypointsDisplay.selectBBox(self.timelineWidget.keypointsDisplay.selected_bbox-1)
def selectNextFrame(self):
self.setFrame(self.timelineWidget.timeline.value()+1)
def selectPrevFrame(self):
self.setFrame(self.timelineWidget.timeline.value()-1)
def openVideoCB(self):
videoPath = QFileDialog.getOpenFileName(self, "Open video")
if videoPath[0] == "":
return
framesCount, fps, labelsDict = detectLabels(videoPath[0])
labels = pd.DataFrame(labelsDict)
for i in labels["track_id"].unique():
self.sequences.append(labels[labels["track_id"]==i].copy().sort_values(by="frame",ascending=True))
self.imageWidget.setSequences(self.sequences)
self.timelineWidget.setSequences(self.sequences)
self.imageWidget.setVideo(videoPath[0])
self.timelineWidget.setFramesProperties(framesCount, fps)
print("done")
def importLabelsCb(self):
videoPath = QFileDialog.getOpenFileName(self, "Open video", filter="*.mp4")
labelsPath = QFileDialog.getOpenFileName(self, "Import labels", filter="*.csv")
if videoPath[0] == '' or labelsPath[0] == '':
return
video = cv2.VideoCapture(videoPath[0])
framesCount = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
fps = video.get(cv2.CAP_PROP_FPS)
print(f"Frames count: {framesCount}")
video.release()
df = pd.read_csv(labelsPath[0])
for i in df["track_id"].unique():
self.sequences.append(df[df["track_id"]==i].copy().sort_values(by="frame",ascending=True))
self.imageWidget.setVideo(videoPath[0])
self.imageWidget.setSequences(self.sequences)
self.timelineWidget.setSequences(self.sequences)
self.timelineWidget.setFramesProperties(framesCount, fps)
def exportLabelsCb(self):
labels_path = QFileDialog.getSaveFileName(self, "Save csv", None, "*.csv")[0]
if labels_path == '':
return
pd.concat(self.sequences, ignore_index=True).to_csv(labels_path, index=False)
def wheelEvent(self, event:QWheelEvent):
numDegrees = event.angleDelta().x() // 8
self.timelineWidget.timeline.setValue(self.timelineWidget.timeline.value()-numDegrees)
event.accept()
def setFrame(self,frame:int):
self.timelineWidget.timeline.setValue(frame)
def make_undo_command(self):
self.mUndoStack.push(UndoCommand(self))
class UndoCommand(QUndoCommand):
def __init__(self, parent:MainWindow):
super().__init__()
self.parent = parent
self.prev_seqs = []
for sq in parent.sequences:
self.prev_seqs.append(sq.copy())
self.seqs = []
for sq in parent.sequences:
self.seqs.append(sq.copy())
def undo(self):
print("hello")
self.seqs = []
for sq in self.parent.sequences:
self.seqs.append(sq.copy())
self.parent.sequences.clear()
for sq in self.prev_seqs:
self.parent.sequences.append(sq.copy())
self.parent.update()
self.parent.timelineWidget.labelList.set_bboxes_cnt(len(self.prev_seqs))
self.parent.imageWidget.repaint()
self.parent.timelineWidget.keypointsDisplay.repaint()
def redo(self):
self.parent.sequences.clear()
for sq in self.seqs:
self.parent.sequences.append(sq.copy())
self.parent.update()
self.parent.timelineWidget.labelList.set_bboxes_cnt(len(self.seqs))
self.parent.imageWidget.repaint()
self.parent.timelineWidget.keypointsDisplay.repaint()