-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagewidget.py
392 lines (315 loc) · 16.1 KB
/
imagewidget.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from PyQt6.QtCore import Qt, QRect, QPoint, QSize, pyqtSignal
from PyQt6.QtGui import QMouseEvent, QPaintEvent, QImage, QPainter, QResizeEvent
from PyQt6.QtWidgets import QWidget, QSizePolicy
from PyQt6 import QtGui
import cv2
import numpy as np
import pandas as pd
from pandas import Series
import psutil
from threading import Thread
class BBox:
def __init__(self, row: Series) -> None:
self.r = row
def lt_corner(self):
x = int((self.r['x'] - self.r['w']/2))
y = int((self.r['y'] - self.r['h']/2))
return (x, y)
def lb_corner(self):
x = int((self.r['x'] - self.r['w']/2))
y = int((self.r['y'] + self.r['h']/2))
return (x, y)
def rt_corner(self):
x = int((self.r['x'] + self.r['w']/2))
y = int((self.r['y'] - self.r['h']/2))
return (x, y)
def rb_corner(self):
x = int((self.r['x'] + self.r['w']/2))
y = int((self.r['y'] + self.r['h']/2))
return (x, y)
def r_middle(self):
return (int(self.r['x']+self.r['w']/2), int(self.r['y']))
def l_middle(self):
return (int(self.r['x']-self.r['w']/2), int(self.r['y']))
def t_middle(self):
return (int(self.r['x']), int(self.r['y'] - self.r['h']/2))
def b_middle(self):
return (int(self.r['x']), int(self.r['y'] + self.r['h']/2))
def containsCoords(self, coords):
lt = self.lt_corner()
rb = self.rb_corner()
return lt[0] <= coords[0] <= rb[0] and lt[1] <= coords[1] <= rb[1]
class ImageWidget(QWidget):
selectedFrameChanged = pyqtSignal(int)
selectedBBoxIdChanged = pyqtSignal(int)
sequencesChanged = pyqtSignal(int)
timelineRepaint = pyqtSignal()
tableUpdate = pyqtSignal()
def __init__(self) -> None:
super().__init__()
self.selectedBBoxId = 0
self.selectedCorner = None
self.sequences = []
self.shape = None
self.resizedShape = None
self.frame = None
self.cap = None
self.lastMousePos = None
self.startBBoxPos = None
self.widthOffset = 0
self.heightOffset = 0
self.aspectRatio = 360/640
freeRam = psutil.virtual_memory()[0]/8 #FREE RAM IN BYTES
self.maxDataSize = freeRam//4
self.ramBuffer = [None,None,None]
self.maxImgCnt = 0
self.bufferStartFrame = 0
self.p = None
self.framesCnt = 0
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
def setSequences(self, sequences):
self.sequences = sequences
def setVideo(self, filepath):
self.cap = cv2.VideoCapture(filepath)
width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float `width`
height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float `height`
self.framesCnt = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.shape = (height, width)
self.aspectRatio = height/width
self.frame = 0
self.maxImgCnt = int(self.maxDataSize/(3*3*width*height))
print(self.maxImgCnt)
self.ramBuffer = [np.empty((self.maxImgCnt,int(height),int(width),3),dtype=np.uint8) for i in range(3)]
for j in range(2):
for i in range(self.maxImgCnt):
if not self.cap.isOpened():
return
_,self.ramBuffer[j+1][i] = self.cap.read()
@staticmethod
def loadBuffer(data,frameId,framesCnt):
ramBuffer = data["buffer"]
if frameId < 0:
data["video"].set(cv2.CAP_PROP_POS_FRAMES,0)
for i in range(ramBuffer.shape[0] + frameId):
_,ramBuffer[i] = data["video"].read()
elif frameId + ramBuffer.shape[0] >= framesCnt:
if frameId >= framesCnt:
return
data["video"].set(cv2.CAP_PROP_POS_FRAMES,frameId)
for i in range(frameId + ramBuffer.shape[0] - framesCnt):
if not data["video"].isOpened():
break
_,ramBuffer[i] = data["video"].read()
else:
data["video"].set(cv2.CAP_PROP_POS_FRAMES,frameId)
for i in range(ramBuffer.shape[0]):
_,ramBuffer[i] = data["video"].read()
def updateBuffer(self):
if self.frame >= self.bufferStartFrame and self.frame < self.maxImgCnt + self.bufferStartFrame:
return
if self.p:
self.p.join()
data = {"video":self.cap}
if self.frame >= self.maxImgCnt*2 + self.bufferStartFrame or self.frame < self.bufferStartFrame-self.maxImgCnt:
frame = self.frame-self.maxImgCnt*3//2
real_frame = max(frame+self.maxImgCnt,0)
self.bufferStartFrame = real_frame
for i in range(3):
data["buffer"] = self.ramBuffer[i]
self.loadBuffer(data,frame+self.maxImgCnt*i,self.framesCnt)
return
if self.frame < self.bufferStartFrame:
if self.bufferStartFrame - self.maxImgCnt < 0:
self.bufferStartFrame = 0
for i in range(1,3):
data["buffer"] = self.ramBuffer[i]
self.loadBuffer(data,self.maxImgCnt*(i-1),self.framesCnt)
return
self.ramBuffer[1],self.ramBuffer[2],self.ramBuffer[0] = self.ramBuffer[0],self.ramBuffer[1],self.ramBuffer[2]
self.bufferStartFrame -= self.maxImgCnt
if self.bufferStartFrame == 0:
return
data["video"] = self.cap
data["buffer"] = self.ramBuffer[0]
self.p = Thread(target=self.loadBuffer,args = (data,self.bufferStartFrame-self.maxImgCnt,self.framesCnt))
else:
self.ramBuffer[0],self.ramBuffer[1],self.ramBuffer[2] = self.ramBuffer[1],self.ramBuffer[2],self.ramBuffer[0]
self.bufferStartFrame += self.maxImgCnt
if self.bufferStartFrame + self.maxImgCnt >= self.framesCnt:
return
data["video"] = self.cap
data["buffer"] = self.ramBuffer[2]
self.p = Thread(target=self.loadBuffer,args = (data,self.bufferStartFrame+self.maxImgCnt,self.framesCnt))
self.p.start()
def setFrame(self, frame):
self.frame = frame
self.repaint()
def getCoordsFromMouseEvent(self, e: QMouseEvent):
height, width = self.shape
x = int((e.pos().x() - self.widthOffset) / self.resizedShape[0] * width)
y = int((e.pos().y() - self.heightOffset) / self.resizedShape[1] * height)
return (x, y)
def mousePressEvent(self, e: QMouseEvent) -> None:
if self.shape is None:
return
coords = self.getCoordsFromMouseEvent(e)
if e.button() == Qt.MouseButton.LeftButton:
self.lastMousePos = coords
if self.selectedBBoxId is not None:
self.setCursor(Qt.CursorShape.SizeAllCursor)
else:
self.startBBoxPos = coords
if e.button() == Qt.MouseButton.RightButton:
for track_id, seq in enumerate(self.sequences):
df = seq[seq['frame'] == self.frame]
if len(df) == 0:
if seq["frame"].iloc[0]>self.frame or self.frame>seq["frame"].iloc[seq.shape[0]-1]:
continue
i = np.argwhere(seq["frame"]<self.frame)[-1,0]
frame_before = seq.iloc[i]
frame_after = seq.iloc[i+1]
div = (self.frame-int(frame_before["frame"]))/(int(frame_after["frame"]-frame_before["frame"]))
r = (frame_after-frame_before)*div + frame_before
else:
r = df.iloc[0]
bbox = BBox(r)
if bbox.containsCoords(coords):
self.selectedBBoxId = track_id
self.selectedBBoxIdChanged.emit(self.selectedBBoxId)
self.repaint()
return
self.selectedBBoxId = None
self.repaint()
def mouseReleaseEvent(self, e) -> None:
self.setCursor(Qt.CursorShape.ArrowCursor)
self.selectedCorner = None
if e.button() == Qt.MouseButton.LeftButton:
self.lastMousePos = None
if self.startBBoxPos:
self.tableUpdate.emit()
coords = self.getCoordsFromMouseEvent(e)
x = (self.startBBoxPos[0] + coords[0])//2
y = (self.startBBoxPos[1] + coords[1])//2
w = abs(self.startBBoxPos[0] - coords[0])
h = abs(self.startBBoxPos[1] - coords[1])
datas = pd.DataFrame({"frame":[self.frame],"track_id":[0], "x":[x], "y":[y],
"h":[h], "w":[w],"label":[0]})
self.sequences.append(datas)
self.selectedBBoxIdChanged.emit(len(self.sequences)-1)
self.sequencesChanged.emit(len(self.sequences))
self.timelineRepaint.emit()
self.startBBoxPos = None
def mouseMoveEvent(self, e) -> None:
if self.selectedBBoxId is not None and self.lastMousePos is not None:
lastX, lastY = self.lastMousePos
x, y = self.getCoordsFromMouseEvent(e)
selectedBBoxIdData = self.sequences[self.selectedBBoxId]
datas = selectedBBoxIdData[selectedBBoxIdData['frame'] == self.frame]
if len(datas) == 0:
if selectedBBoxIdData["frame"].iloc[0]>self.frame or self.frame>selectedBBoxIdData["frame"].iloc[selectedBBoxIdData.shape[0]-1]:
return
self.tableUpdate.emit()
i = np.argwhere(selectedBBoxIdData["frame"]<self.frame)[-1,0]
frame_before = selectedBBoxIdData.iloc[i]
frame_after = selectedBBoxIdData.iloc[i+1]
min_x1,min_x2,max_x1,max_x2 = selectedBBoxIdData['x'].iloc[i],selectedBBoxIdData['x'].iloc[i+1],selectedBBoxIdData['h'].iloc[i],selectedBBoxIdData['h'].iloc[i+1]
min_y1,min_y2,max_y1,max_y2 = selectedBBoxIdData['y'].iloc[i],selectedBBoxIdData['y'].iloc[i+1],selectedBBoxIdData['w'].iloc[i],selectedBBoxIdData['w'].iloc[i+1]
div = (self.frame-int(frame_before["frame"]))/(int(frame_after["frame"]-frame_before["frame"]))
datas = pd.DataFrame({"frame":[self.frame],"track_id":[0], "x":[(min_x2-min_x1)*div + min_x1], "y":[(min_y2-min_y1)*div + min_y1],
"h":[(max_x2-max_x1)*div + max_x1], "w":[(max_y2-max_y1)*div + max_y1],"label":[selectedBBoxIdData['label'].iloc[i]]})
self.sequences[self.selectedBBoxId] = pd.concat([selectedBBoxIdData,datas],ignore_index=True).sort_values("frame",ascending=True)
self.timelineRepaint.emit()
data = datas.iloc[0]
bbox = BBox(data)
last = np.array(self.lastMousePos)
if np.linalg.norm(np.array(bbox.rt_corner()) - last) <= 20 or self.selectedCorner == 0:
self.selectedCorner = 0
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'w'] += x - lastX
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'h'] -= y - lastY
self.repaint()
elif np.linalg.norm(np.array(bbox.lt_corner()) - last) <= 20 or self.selectedCorner == 1:
self.selectedCorner = 1
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'w'] -= x - lastX
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'h'] -= y - lastY
self.repaint()
elif np.linalg.norm(np.array(bbox.rb_corner()) - last) <= 20 or self.selectedCorner == 2:
self.selectedCorner = 2
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'w'] += x - lastX
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'h'] += y - lastY
self.repaint()
elif np.linalg.norm(np.array(bbox.lb_corner()) - last) <= 20 or self.selectedCorner == 3:
self.selectedCorner = 3
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'w'] -= x - lastX
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'h'] += y - lastY
self.repaint()
elif(bbox.containsCoords(self.lastMousePos)):
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'x'] += x - lastX
selectedBBoxIdData.loc[selectedBBoxIdData['frame'] == self.frame,'y'] += y - lastY
self.repaint()
self.lastMousePos = (x, y)
if self.startBBoxPos:
self.lastMousePos = self.getCoordsFromMouseEvent(e)
self.repaint()
def paintEvent(self, a0) -> None:
painter = QPainter(self)
if not self.cap:
brush = QtGui.QBrush()
brush.setColor(QtGui.QColor('black'))
brush.setStyle(Qt.BrushStyle.SolidPattern)
rect = QRect(0, 0, painter.device().width(), painter.device().height())
painter.fillRect(rect, brush)
return
self.updateBuffer()
image = self.ramBuffer[1][self.frame-self.bufferStartFrame]
for track_id, seq in enumerate(self.sequences):
df = seq[seq['frame'] == self.frame]
if len(df) == 0:
if seq.shape[0]==0 or seq["frame"].iloc[0]>self.frame or self.frame>seq["frame"].iloc[seq.shape[0]-1]:
continue
i = np.argwhere((seq["frame"]<self.frame).to_numpy())[-1,0]
frame_before = seq.iloc[i]
frame_after = seq.iloc[i+1]
div = (self.frame-int(frame_before["frame"]))/(int(frame_after["frame"]-frame_before["frame"]))
r = (frame_after-frame_before)*div + frame_before
else:
r = df.iloc[0]
color = (0, 255, 0)
if r['label'] == 1:
color = (255, 0, 0)
if track_id == self.selectedBBoxId:
color = (255, 255, 0)
bbox = BBox(r)
cv2.circle(image, bbox.lt_corner(), 10, color, -1)
cv2.circle(image, bbox.t_middle(), 10, color, -1)
cv2.circle(image, bbox.rt_corner(), 10, color, -1)
cv2.circle(image, bbox.l_middle(), 10, color, -1)
cv2.circle(image, bbox.r_middle(), 10, color, -1)
cv2.circle(image, bbox.lb_corner(), 10, color, -1)
cv2.circle(image, bbox.b_middle(), 10, color, -1)
cv2.circle(image, bbox.rb_corner(), 10, color, -1)
bbox = BBox(r)
image = cv2.rectangle(image, bbox.lt_corner(), bbox.rb_corner(), color, 2)
if self.startBBoxPos:
# print(self.startBBoxPos, self.lastMousePos)
image = cv2.rectangle(image, self.startBBoxPos, self.lastMousePos, (255, 255, 255), 2)
if self.aspectRatio > self.height()/self.width():
newWidth = int(self.height()/self.aspectRatio)
self.resizedShape = (newWidth, self.height())
self.heightOffset = 0
self.widthOffset = (self.width() - newWidth)//2
else:
newHeight = int(self.aspectRatio*self.width())
self.resizedShape = (self.width(), newHeight)
self.heightOffset = (self.height() - newHeight)//2
self.widthOffset = 0
image = cv2.resize(image, self.resizedShape)
height, width, _ = image.shape
bytesPerLine = width*3
size = QSize(width, height)
qImg = QImage(image.data, width, height, bytesPerLine, QImage.Format.Format_BGR888)
painter.drawImage(QRect(QPoint(self.widthOffset, self.heightOffset), size), qImg)
def sizeHint(self) -> QSize:
return QSize(640, 360)
def selectBBox(self, bbox_id):
self.selectedBBoxId = bbox_id
self.repaint()