Repository navigation
Expand file tree
/
Copy pathDrawBox.py
More file actions
259 lines (211 loc) · 8.38 KB
/
Copy pathDrawBox.py
File metadata and controls
259 lines (211 loc) · 8.38 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
import math
import os
from PyQt5 import QtCore, QtGui, QtWidgets
def toDarknetRect(r, iw, ih):
clip = lambda x: max(min(x, 1.0), 0.0)
return (
clip((r.left() + (r.width() / 2)) / iw),
clip((r.top() + (r.height() / 2)) / ih),
clip(r.width() / iw),
clip(r.height() / ih),
)
def fromDarknetRect(r, iw, ih):
cx, cy, w, h = r
return QtCore.QRect(
iw * (cx - w / 2),
ih * (cy - h / 2),
iw * w,
ih * h,
)
class Window(QtWidgets.QMainWindow):
def __init__(self, args):
super().__init__()
self.args = args
self.files = args.files
self.filei = 0
self.rects = [[] for _ in range(len(args.files))]
self.recti = -1
for i, file in enumerate(self.files):
prefix, _ = os.path.splitext(file)
try:
with open(prefix + '.txt') as f:
for line in f:
clss, x, y, width, height = line.rstrip().split(' ')
assert clss == '0'
x, y = float(x), float(y)
width, height = float(width), float(height)
self.rects[i].append((x, y, width, height))
except:
continue
self.canvas = Canvas()
self.canvas.rectDrawn.connect(self.handle_rect_drawn)
self.canvas.rectResized.connect(self.handle_rect_resized)
self.canvas.rectSelected.connect(self.handle_rect_selected)
self.setCentralWidget(self.canvas)
self.load_image()
def save_output(self):
if self.args.read_only:
return
prefix, _ = os.path.splitext(self.files[self.filei])
with open(prefix + '.txt', 'w') as f:
for r in self.rects[self.filei]:
f.write('%i %.5f %.5f %.5f %.5f\n' % (0, *r))
#with open(args.output, 'w') as f:
# json.dump(rectmap, f)
def handle_rect_drawn(self, rect):
self.rects[self.filei].append(toDarknetRect(rect, *self.imagesize))
self.recti = len(self.rects[self.filei]) - 1
self.update_rects()
def handle_rect_resized(self, recti, rect):
self.rects[self.filei][self.recti] = \
toDarknetRect(rect, *self.imagesize)
self.update_rects()
def handle_rect_selected(self, recti):
self.recti = recti
self.update_rects()
def load_image(self):
image = QtGui.QImage(self.files[self.filei])
self.imagesize = image.width(), image.height()
self.canvas.image = image
self.recti = -1
self.update_rects()
self.resize(image.width(), image.height())
self.canvas.update()
def update_rects(self):
self.canvas.rects = [
fromDarknetRect(r, *self.imagesize)
for r in self.rects[self.filei]
]
self.canvas.recti = self.recti
self.canvas.update()
self.save_output()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Q:
QtCore.QCoreApplication.quit()
elif event.key() == QtCore.Qt.Key_Left:
if self.filei > 0:
self.filei -= 1
self.load_image()
elif event.key() == QtCore.Qt.Key_Right:
if self.filei < len(self.files) - 1:
self.filei += 1
self.load_image()
elif event.key() == QtCore.Qt.Key_Tab:
if len(self.rects[self.filei]) > 0:
self.recti = (self.recti + 1) % len(self.rects[self.filei])
self.update_rects()
elif event.key() == QtCore.Qt.Key_Backtab:
if len(self.rects[self.filei]) > 0:
self.recti = (self.recti - 1) % len(self.rects[self.filei])
self.update_rects()
elif event.key() == QtCore.Qt.Key_Backspace:
if self.recti >= 0:
del self.rects[self.filei][self.recti]
self.recti -= 1
self.update_rects()
class Canvas(QtWidgets.QWidget):
rectDrawn = QtCore.pyqtSignal(QtCore.QRect)
rectResized = QtCore.pyqtSignal(int, QtCore.QRect)
rectSelected = QtCore.pyqtSignal(int)
handleSize = 4
def __init__(self):
super().__init__()
self.image = None
self.rects = []
self.recti = -1
self.dragBegin = None
self.dragEnd = None
self.isDrawing = False
self.isResizing = False
self.show()
def paintEvent(self, event):
qp = QtGui.QPainter(self)
if self.image:
qp.drawImage(0, 0, self.image)
# Draw any existing rectangles
for i, rect in enumerate(self.rects):
if i == self.recti:
qp.setBrush(QtGui.QBrush(QtGui.QColor(100, 255, 255, 50)))
else:
qp.setBrush(QtGui.QBrush(QtGui.QColor(255, 255, 100, 50)))
qp.drawRect(rect)
# Draw grabbers for the current rectangle
if self.recti >= 0:
qp.setBrush(QtGui.QBrush(QtGui.QColor(255, 255, 255, 255)))
qp.drawEllipse(self.rects[self.recti].topLeft(),
self.handleSize, self.handleSize)
qp.drawEllipse(self.rects[self.recti].topRight(),
self.handleSize, self.handleSize)
qp.drawEllipse(self.rects[self.recti].bottomLeft(),
self.handleSize, self.handleSize)
qp.drawEllipse(self.rects[self.recti].bottomRight(),
self.handleSize, self.handleSize)
# Draw the currently drawn rectangle
if self.isDrawing:
qp.setBrush(QtGui.QBrush(QtGui.QColor(100, 255, 255, 50)))
qp.drawRect(QtCore.QRect(self.dragBegin, self.dragEnd))
def mousePressEvent(self, event):
self.dragBegin = event.pos()
self.dragEnd = event.pos()
if self.recti >= 0:
dist = \
lambda a, b: math.sqrt((a.x() - b.x())**2 + (a.y() - b.y())**2)
corners = {
'tl': self.rects[self.recti].topLeft(),
'tr': self.rects[self.recti].topRight(),
'bl': self.rects[self.recti].bottomLeft(),
'br': self.rects[self.recti].bottomRight(),
}
opposites = {
'tl': self.rects[self.recti].bottomRight(),
'tr': self.rects[self.recti].bottomLeft(),
'bl': self.rects[self.recti].topRight(),
'br': self.rects[self.recti].topLeft(),
}
for abbr, corner in corners.items():
if dist(corner, self.dragBegin) < self.handleSize:
self.isResizing = True
self.dragEnd = corner
self.dragBegin = opposites[abbr]
break
self.isDrawing = not self.isResizing
self.update()
def mouseMoveEvent(self, event):
self.dragEnd = event.pos()
if self.isResizing:
# Emit that we resized the rect
rect = QtCore.QRect(self.dragBegin, self.dragEnd).normalized()
self.rectResized.emit(self.recti, rect)
self.update()
def mouseReleaseEvent(self, event):
self.dragEnd = event.pos()
# We did not move very, interpret it as a click
distance = (self.dragEnd - self.dragBegin).manhattanLength()
if not self.isResizing and distance < 4:
for i, rect in reversed(list(enumerate(self.rects))):
if rect.contains(self.dragEnd):
self.rectSelected.emit(i)
break
elif self.isDrawing:
# Emit that something was drawn
rect = QtCore.QRect(self.dragBegin, self.dragEnd).normalized()
self.rectDrawn.emit(rect)
self.isDrawing = self.isResizing = False
self.update()
if __name__ == '__main__':
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--read-only', action='store_true')
parser.add_argument('files', nargs='+')
args = parser.parse_args()
basenames = set()
for f in args.files:
basename = os.path.basename(f)
if basename in basenames:
raise argparse.ArgumentError('All files must have unique names')
basenames.add(basename)
app = QtWidgets.QApplication(sys.argv)
window = Window(args)
window.show()
sys.exit(app.exec_())