forked from AISP-PL/yaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewerEditorImage.py
468 lines (401 loc) · 17.2 KB
/
ViewerEditorImage.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
'''
Created on 30 gru 2020
@author: spasz
'''
import os
import subprocess
import cv2
import numpy as np
from datetime import datetime
from helpers.QtDrawing import QDrawPolygon, QDrawPolyline, QDrawJoints,\
QDrawCrosshair, QDrawText, QDrawArrow, CvBGRColorToQColor, QDrawRectangle,\
CvImage2QtImage, CvRGBColorToQColor, QDrawElipse
from helpers.boxes import PointToRelative, PointToAbsolute, PointsToRect,\
IsInside
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPainter, QBrush, QFont, QPixmap
from PyQt5.Qt import QPoint, QTimer
from PyQt5.QtCore import Qt, pyqtSignal, QRect, QPointF
import helpers.boxes as boxes
import logging
from helpers.images import GetFixedFitToBox
class ViewerEditorImage(QWidget):
''' States of editor.'''
# Editor mode
ModeNone = 0
ModeAddAnnotation = 1
ModeRenameAnnotation = 2
ModeRemoveAnnotation = 3
ModePaintCircle = 4
# Miniature position
MiniatureLeft = 0
MiniatureRight = 1
# Image scaling mode
ImageScalingResize = 0
ImageScalingResizeAspectRatio = 1
ImageScalingOriginalSize = 2
# Image scaling algorithm
ImageScalingLinear = 0
ImageScalingNearest = 1
# Define signals
signalEditorFinished = pyqtSignal(int, name='EditorFinished')
def __init__(self, parent):
# QWidget constructor call
super().__init__(parent=parent)
# ----- Viewed data ------
# Configuration
self.config = {
'isConfidence': True,
'isAnnotationsHidden': False,
'isLabelsHidden': False,
}
# Background image loaded
self.imageBg = None
# Mode of scaling image
self.imageScaling = self.ImageScalingResize
# Annoter for image
self.annoter = None
# Current class number
self.classNumber = 0
# ----- Editor data ------
# Editor mode
self.editorMode = self.ModeNone
# Editor extra argument for mode
self.editorModeArgument = None
# Current mouse coords
self.mousePosition = None
# Mouse trajectory
self.mouseTrajectory = None
# List of all clicks
self.mouseClicks = []
# Is dragging
self.mouseDragging = False
# Miniature current position
self.miniaturePosition = ViewerEditorImage.MiniatureLeft
# UI init and show
self.setMouseTracking(True)
self.show()
def TrajectoryToAbsoluteQTrajectory(self, trajectory, width, height):
'''Recalculate trajectory to Q absolute trajectory.'''
qtrajectory = []
for element in trajectory:
x, y = PointToAbsolute((element[0], element[1]),
width, height)
qtrajectory.append(QPointF(x, y))
return qtrajectory
def AbsoluteQTrajectoryToTrajectory(self, qtrajectory, width, height):
'''Recalculate trajectory to Q absolute trajectory.'''
trajectory = []
for qpoint in qtrajectory:
x, y = qpoint.x(), qpoint.y()
x, y = PointToRelative((x, y),
width, height)
trajectory.append((x, y))
return trajectory
def ItemQTrajectoryUpdate(self, item, width, height):
''' Update absolute Q trajectory in item.'''
item['QViewTrajectory'] = self.TrajectoryToAbsoluteQTrajectory(
item['trajectory'], width, height)
item['QViewWidth'] = width
item['QViewHeight'] = height
return item
def GetOption(self, name):
''' Set configuration option.'''
if (name in self.config):
return self.config[name]
return None
def SetOption(self, name, value):
''' Set configuration option.'''
if (name in self.config):
self.config[name] = value
self.update()
else:
logging.error('(ViewerEditorImage) Unknown configuration option!')
def SetClassNumber(self, index):
''' Set class number. '''
self.classNumber = index
def SetEditorMode(self, mode, argument=None):
''' Sets editor mode.'''
self.__resetEditorMode()
self.editorMode = mode
self.editorModeArgument = argument
return True
def SetEditorModeArgument(self, argument):
''' Set/update editor mode argument.'''
self.editorModeArgument = argument
def __resetEditorMode(self, defaultMode=ModeNone):
''' Resets editor mode.'''
self.mousePosition = None
self.mouseTrajectory = None
self.mouseClicks = []
self.mouseDragging = False
self.editorMode = defaultMode
def SetAnnoter(self, annoter):
''' Set annoter handle.'''
if (annoter is not None):
self.annoter = annoter
self.update()
def SetImage(self, image):
''' Set imagePath to show.'''
if (image is not None):
self.imageBg = image
self.update()
def SetImageScaling(self, scalingMode):
''' Sets image scaling mode.'''
self.imageScaling = scalingMode
def GetImageSize(self):
''' Returns .'''
if (self.imageBg is not None):
height, width = self.imageBg.shape[0:2]
return width, height
return 0, 0
def GetViewSize(self):
''' Returns .'''
width, height = self.rect().getRect()[2:]
return width, height
def GetHoveredAnnotation(self, point):
''' Finds currently hovered annotation.'''
founded = None
for element in self.annoter.GetAnnotations():
if (element.IsInside(point) == True):
if (founded is not None):
area1 = boxes.GetArea(element.GetBox())
area2 = boxes.GetArea(founded.GetBox())
if (area1 < area2):
founded = element
else:
founded = element
return founded
def SaveScreenshot(self, filepath):
''' Returns screenshot.'''
pixmap = QPixmap((self.size()))
self.render(pixmap)
pixmap.save(filepath)
def ScreenshotToFile(self, path) -> bool:
'''Public method which renders widget to file.'''
# Render to pixmap of same width and height
pixmap = self.grab(self.rect())
# Save to filepath (return bool)
return pixmap.save(path)
def mouseMoveEvent(self, event):
''' Handle mouse move event.'''
self.mousePosition = event.pos()
if (self.mouseTrajectory is not None):
self.mouseTrajectory.append(event.pos())
self.update()
def mousePressEvent(self, event):
''' Handle mouse event.'''
# Mode Add Annotation
if (self.editorMode == self.ModeAddAnnotation):
# LPM adds point
if (event.buttons() == Qt.LeftButton):
self.mouseClicks.append(event.pos())
# If stored 2 points the call finish.
if (len(self.mouseClicks) >= 2):
self.CallbackEditorFinished()
# Mode Rename Annotation
elif (self.editorMode == self.ModeRenameAnnotation):
# LPM finds annotation
if (event.buttons() == Qt.LeftButton):
x, y = event.pos().x(), event.pos().y()
viewWidth, viewHeight = self.GetViewSize()
toDelete = self.GetHoveredAnnotation(boxes.PointToRelative((x, y),
viewWidth, viewHeight))
# If clicked on annotation the return
if (toDelete is not None):
self.editorModeArgument = toDelete
self.CallbackEditorFinished()
# Mode Remove Annotation
elif (self.editorMode == self.ModeRemoveAnnotation):
# LPM finds annotation
if (event.buttons() == Qt.LeftButton):
x, y = event.pos().x(), event.pos().y()
viewWidth, viewHeight = self.GetViewSize()
toDelete = self.GetHoveredAnnotation(boxes.PointToRelative((x, y),
viewWidth, viewHeight))
# If clicked on annotation the return
if (toDelete is not None):
self.editorModeArgument = toDelete
self.CallbackEditorFinished()
# Mode Paint circle
elif (self.editorMode == self.ModePaintCircle):
# LPM finds annotation
if (event.buttons() == Qt.LeftButton):
# Enabled mouse trajectory storing
self.mouseTrajectory = [event.pos()]
def mouseReleaseEvent(self, event):
''' Handle mouse event.'''
# Mode Paint circle
if (self.editorMode == self.ModePaintCircle):
self.CallbackEditorFinished()
def CallbackEditorFinished(self):
''' Finished editor mode.'''
# Get Preview info width & height
widgetWidth, widgetHeight = self.GetViewSize()
# Get image width & height
imWidth, imHeight = self.GetImageSize()
# ---------- Select scaling mode -----------
# Resize
if (self.imageScaling == self.ImageScalingResize):
viewportWidth, viewportHeight = widgetWidth, widgetHeight
# Resize with aspect ratio
elif (self.imageScaling == self.ImageScalingResizeAspectRatio):
viewportWidth, viewportHeight = GetFixedFitToBox(
imWidth, imHeight, widgetWidth, widgetHeight)
# Original size
elif (self.imageScaling == self.ImageScalingOriginalSize):
viewportWidth, viewportHeight = imWidth, imHeight
# Mouse clicks to relative
clicksRel = self.AbsoluteQTrajectoryToTrajectory(
self.mouseClicks, viewportWidth, viewportHeight)
# Mouse trajectory relative
if (self.mouseTrajectory is not None):
trajectoryRel = self.AbsoluteQTrajectoryToTrajectory(
self.mouseTrajectory, viewportWidth, viewportHeight)
# Mode Add Annotation
if (self.editorMode == self.ModeAddAnnotation):
box = PointsToRect(clicksRel[0], clicksRel[1])
self.annoter.AddAnnotation(box,
self.classNumber)
# Mode Rename Annotation
elif (self.editorMode == self.ModeRenameAnnotation):
annote = self.editorModeArgument
annote.SetClassNumber(self.classNumber)
annote.SetAuthorType(AnnoteAuthorType.byHand)
# Mode Remove Annotation
elif (self.editorMode == self.ModeRemoveAnnotation):
self.annoter.RemoveAnnotation(self.editorModeArgument)
# Mode Paint circle
elif (self.editorMode == self.ModePaintCircle):
imWidth, imHeight = self.GetImageSize()
for relPoint in trajectoryRel:
imPoint = PointToAbsolute(relPoint, imWidth, imHeight)
# Add drawing to original image
self.annoter.PaintCircles([imPoint],
self.editorModeArgument,
(0, 0, 0))
previousMode = self.editorMode
self.__resetEditorMode(previousMode)
self.update()
# Emit signal
self.signalEditorFinished.emit(previousMode)
def paintMiniature(self,
painter: QPainter,
image: np.array,
miniWidth: int = 128,
miniHeight: int = 128) -> None:
''' Painting miniature of image in painter.'''
# Get Preview info width & height
widgetWidth, widgetHeight = self.GetViewSize()
# Get image dimensions
imHeight, imWidth = image.shape[0:2]
# Get fitted image dimensions
miniWidth, miniHeight = GetFixedFitToBox(
imWidth, imHeight, miniWidth, miniHeight)
# Change cv2 image to pixmap
pixmap = CvImage2QtImage(cv2.resize(image, (miniWidth, miniHeight)))
# Create position Qrect
if (self.miniaturePosition == ViewerEditorImage.MiniatureLeft):
miniaturePosition = QPointF(0, 0)
else:
miniaturePosition = QPointF(widgetWidth-miniWidth, 0)
# Check if mouse over miniature
if (self.mousePosition is not None):
point = (self.mousePosition.x(), self.mousePosition.y())
mx, my = miniaturePosition.x(), miniaturePosition.y()
if (IsInside(point, (mx, my, mx+miniWidth, my+miniHeight))):
# Miniature position : Switch
if (self.miniaturePosition == ViewerEditorImage.MiniatureLeft):
self.miniaturePosition = ViewerEditorImage.MiniatureRight
else:
self.miniaturePosition = ViewerEditorImage.MiniatureLeft
# Draw on painter in QRect corner
painter.drawPixmap(miniaturePosition, pixmap)
def paintEvent(self, event):
''' Draw on every paint event.'''
# Get Preview info width & height
widgetWidth, widgetHeight = self.GetViewSize()
# Create painter
widgetPainter = QPainter()
widgetPainter.begin(self)
# If image is loaded?
if (self.imageBg is not None):
# Setup image width & height
imHeight, imWidth = self.imageBg.shape[0:2]
image = self.imageBg
# If there is no image then fill Black and draw text
else:
# Setup image width & height
imWidth, imHeight = self.GetViewSize()
image = np.zeros([imWidth, imHeight, 3], dtype=np.uint8)
# ---------- Select scaling mode -----------
# Resize
if (self.imageScaling == self.ImageScalingResize):
viewportWidth, viewportHeight = widgetWidth, widgetHeight
# Resize with aspect ratio
elif (self.imageScaling == self.ImageScalingResizeAspectRatio):
viewportWidth, viewportHeight = GetFixedFitToBox(
imWidth, imHeight, widgetWidth, widgetHeight)
widgetPainter.setViewport(0, 0, viewportWidth, viewportHeight)
# Original size
elif (self.imageScaling == self.ImageScalingOriginalSize):
widgetPainter.setViewport(0, 0, imWidth, imHeight)
viewportWidth, viewportHeight = imWidth, imHeight
# Recalculate mouse position to viewport
mousePosition = None
if (self.mousePosition is not None):
mx, my = boxes.PointToRelative((self.mousePosition.x(), self.mousePosition.y()),
viewportWidth, viewportHeight)
mousePosition = QPointF(mx*widgetWidth, my*widgetHeight)
# Recalculate mouse clicks to viewport
mouseClicks = []
if (len(self.mouseClicks)):
for mouseClick in self.mouseClicks:
mx, my = boxes.PointToRelative((mouseClick.x(), mouseClick.y()),
viewportWidth, viewportHeight)
mouseClicks.append(QPointF(mx*widgetWidth, my*widgetHeight))
# Draw current OpenCV image as pixmap
pixmap = CvImage2QtImage(image)
widgetPainter.drawPixmap(self.rect(), pixmap)
# Draw miniature
self.paintMiniature(widgetPainter, image)
# Draw all annotations
if (not self.config['isAnnotationsHidden']):
for annotate in self.annoter.GetAnnotations():
annotate.QtDraw(widgetPainter,
isConfidence=True,
isLabel=not self.config['isLabelsHidden'])
# Draw hovered annotation
if (mousePosition is not None):
# Get hovered annotation
annote = self.GetHoveredAnnotation(
boxes.PointToRelative((self.mousePosition.x(), self.mousePosition.y()),
viewportWidth, viewportHeight))
if (annote is not None):
annote.QtDraw(widgetPainter,
highlight=True,
isConfidence=True,
isLabel=True)
# -------- Mode Annotation Draw -------
if (self.editorMode == self.ModeAddAnnotation):
if (len(mouseClicks) != 0) and (mousePosition is not None):
# Draw rectangle box
QDrawRectangle(widgetPainter,
[mouseClicks[0], mousePosition],
pen=Qt.green,
brushColor=Qt.green,
brushOpacity=0.1
)
# -------- Mode Paint Draw -------
if (self.editorMode == self.ModePaintCircle):
if (mousePosition is not None):
imWidth, imHeight = self.GetImageSize()
radius = round((self.editorModeArgument/imWidth)*viewportWidth)
QDrawElipse(widgetPainter,
mousePosition,
radius)
# Draw crosshair
if (mousePosition is not None):
QDrawCrosshair(widgetPainter, mousePosition.x(), mousePosition.y(),
widgetWidth, widgetHeight, Qt.red)
widgetPainter.end()