-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathapp.py
455 lines (368 loc) · 15.6 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
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
import os
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from functools import partial
from toolBar import ToolBar
from canvas import Canvas
from lib import newIcon
from zoomWidget import ZoomWidget
from grab_cut import Grab_cut
import cv2
__appname__ = 'ImageMatting'
defaultFilename = '.'
class WindowMixin(object):
def menu(self, title, actions=None):
menu = self.menuBar().addMenu(title)
if actions:
addActions(menu, actions)
return menu
def toolbar(self, title, actions=None):
toolbar = ToolBar(title)
toolbar.setObjectName('{}ToolBar'.format(title))
toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
if actions:
addActions(toolbar, actions)
self.addToolBar(Qt.LeftToolBarArea, toolbar)
return toolbar
class ResizedQWidget(QWidget):
def sizeHint(self):
return QSize(100, 150)
def newAction(parent, text, slot=None, shortcut=None,
tip=None, icon=None, checkable=False,
enable=True):
a = QAction(text, parent)
if icon is not None:
a.setIcon(QIcon(icon))
if shortcut is not None:
a.setShortcut(shortcut)
if tip is not None:
a.setToolTip(tip)
a.setStatusTip(tip)
if slot is not None:
a.triggered.connect(slot)
if checkable:
a.setCheckable(True)
a.setEnabled(enable)
return a
def addActions(widget, actions):
for action in actions:
if action is None:
widget.addSeparator()
elif isinstance(action, QMenu):
widget.addMenu(action)
else:
widget.addAction(action)
class struct(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
class MainWindow(QMainWindow, WindowMixin):
FIT_WINDOW, FIT_WIDTH, MANUAL_ZOOM = list(range(3))
def __init__(self, defaultFilename=None):
super().__init__()
self.dirty = True
self.mImgList = []
self.dirname = None
self._beginner = True
self.image_out_np = None
self.default_save_dir = None
# Application state
self.filePath = None
self.mattingFile = None
listLayout = QVBoxLayout()
listLayout.setContentsMargins(0, 0, 0, 0)
matResultShow = ResizedQWidget()
matResultShow.resize(150, 150)
self.pic = QLabel(matResultShow)
self.pic.resize(150, 150)
self.pic.setGeometry(50, 20, 150, 150)
# self.pic.resize(matResultShow.width(), matResultShow.height())
# self.pic.setScaledContents(True)
matResultShow.setLayout(listLayout)
self.resultdock = QDockWidget('Result Image', self)
# self.resultdock.adjustSize()
self.resultdock.setObjectName('result')
self.resultdock.setWidget(matResultShow)
self.resultdock.resize(150, 150)
self.fileListWidget = QListWidget()
self.fileListWidget.itemDoubleClicked.connect(
self.fileitemDoubleClicked)
fileListLayout = QVBoxLayout()
fileListLayout.setContentsMargins(0, 0, 0, 0)
fileListLayout.addWidget(self.fileListWidget)
fileListContainer = QWidget()
fileListContainer.setLayout(fileListLayout)
self.filedock = QDockWidget('File List', self)
self.filedock.setObjectName('Files')
self.filedock.setWidget(fileListContainer)
self.zoomWidget = ZoomWidget()
self.canvas = Canvas(parent=self)
scroll = QScrollArea()
scroll.setWidget(self.canvas)
scroll.setWidgetResizable(True)
self.scrollBars = {
Qt.Vertical: scroll.verticalScrollBar(),
Qt.Horizontal: scroll.horizontalScrollBar()
}
self.scrollArea = scroll
self.canvas.scrollRequest.connect(self.scrollRequest)
self.setCentralWidget(scroll)
self.addDockWidget(Qt.RightDockWidgetArea, self.resultdock)
self.addDockWidget(Qt.RightDockWidgetArea, self.filedock)
self.filedock.setFeatures(QDockWidget.DockWidgetFloatable)
self.dockFeatures = QDockWidget.DockWidgetClosable | QDockWidget.DockWidgetFloatable
self.resultdock.setFeatures(
self.resultdock.features() ^ self.dockFeatures)
# Actions
action = partial(newAction, self)
open_file = action('&Open', self.openFile, 'Ctrl+O', 'Open image')
open_dir = action('&Open Dir', self.openDir,
'Ctrl+D', 'Open image dir')
change_save_dir = action('&Change Save Dir', self.changeSavedirDialog)
# open_next_img = action('&Next Image', self.openNextImg,
# 'Ctrl+N', 'Open next image')
# open_pre_img = action('&Previous Image', self.openPreImg,
# 'Ctrl+M', 'Open previous image')
save = action('&Save', self.saveFile, 'Crl+S', 'Save output image')
create = action('Create\nRectBox', self.createShape,
'w', 'Draw a new Box')
matting = action('&Create\nMatting', self.grabcutMatting,
'e', 'GrabcutMatting')
self.scalers = {
self.FIT_WINDOW: self.scaleFitWindow,
self.FIT_WIDTH: self.scaleFitWidth,
# Set to one to scale to 100% when loading files.
self.MANUAL_ZOOM: lambda: 1,
}
# store actions for further handling
self.actions = struct(save=save, open_file=open_file,
open_dir=open_dir, change_save_dir=change_save_dir,
# open_next_img=open_next_img, open_pre_img=open_pre_img,
create=create, matting=matting)
# Auto saving: enable auto saving if pressing next
# self.autoSaving = QAction('Auto Saving', self)
# self.autoSaving.setCheckable(True)
# self.autoSaving.setChecked()
# set toolbar
self.tools = self.toolbar('Tools')
self.actions.all = (save, open_file, open_dir,
change_save_dir, create,
# open_pre_img, open_next_img,
matting)
addActions(self.tools, self.actions.all)
# set status
self.statusBar().showMessage('{} started.'.format(__appname__))
def okToContinue(self):
if self.dirty:
reply = QMessageBox.question(self, "Attention",
"you have unsaved changes, proceed anyway?",
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
if reply == QMessageBox.Cancel:
return False
elif reply == QMessageBox.Yes:
return self.fileSave
return True
def resetState(self):
self.canvas.resetState()
def errorMessage(self, title, message):
return QMessageBox.critical(self, title,
'<p><b>%s</b></p>%s' % (title, message))
def beginner(self):
return self._beginner
def advanced(self):
return not self.beginner()
def openFile(self, _value=False):
path = os.path.dirname(self.filePath) if self.filePath else '.'
formats = ['*.%s' % fmt.data().decode("ascii").lower()
for fmt in QImageReader.supportedImageFormats()]
filters = "Image (%s)" % ' '.join(formats)
filename = QFileDialog.getOpenFileName(
self, '%s - Choose Image or Label file' % __appname__, path, filters)
if filename:
if isinstance(filename, (tuple, list)):
filename = filename[0]
self.loadFile(filename)
def openDir(self, dirpath=None):
defaultOpenDirPath = dirpth if dirpath else '.'
targetDirPath = QFileDialog.getExistingDirectory(self,
'%s - Open Directory' % __appname__, defaultOpenDirPath,
QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
self.importDirImages(targetDirPath)
def importDirImages(self, dirpath):
self.fileListWidget.clear()
self.mImgList = self.scanAllImages(dirpath)
# self.openNextImg()
for imgPath in self.mImgList:
item = QListWidgetItem(imgPath)
self.fileListWidget.addItem(item)
def scanAllImages(self, folderPath):
extensions = ['.%s' % fmt.data().decode("ascii").lower()
for fmt in QImageReader.supportedImageFormats()]
imageList = []
for root, dirs, files in os.walk(folderPath):
for file in files:
if file.lower().endswith(tuple(extensions)):
relativePath = os.path.join(root, file)
path = os.path.abspath(relativePath)
imageList.append(path)
imageList.sort(key=lambda x: x.lower())
return imageList
def fileitemDoubleClicked(self, item=None):
currIndex = self.mImgList.index(item.text())
if currIndex < len(self.mImgList):
filename = self.mImgList[currIndex]
if filename:
self.loadFile(filename)
def loadFile(self, filePath=None):
self.resetState()
self.canvas.setEnabled(False)
# highlight the file item
if filePath and self.fileListWidget.count() > 0:
index = self.mImgList.index(filePath)
fileWidgetItem = self.fileListWidget.item(index)
fileWidgetItem.setSelected(True)
if filePath and os.path.exists(filePath):
# load image
self.imageData = read(filePath, None)
image = QImage.fromData(self.imageData)
if image.isNull():
self.errorMessage(u'Error opening file',
u'<p>Make sure <i>%s</i> is a valid image file.' % filePath)
self.status('Error reading %s' % filePath)
return False
self.status('Loaded %s' % os.path.basename(filePath))
self.image = image
self.filePath = filePath
self.canvas.loadPixmap(QPixmap.fromImage(image))
self.canvas.setEnabled(True)
self.adjustScale(initial=True)
self.paintCanvas()
# self.toggleActions(True)
def status(self, message, delay=5000):
self.statusBar().showMessage(message, delay)
def adjustScale(self, initial=False):
value = self.scalers[self.FIT_WINDOW if initial else self.zoomMode]()
self.zoomWidget.setValue(int(100 * value))
def scaleFitWindow(self):
"""Figure out the size of the pixmap in order to fit the main widget."""
e = 2.0 # So that no scrollbars are generated.
w1 = self.centralWidget().width() - e
h1 = self.centralWidget().height() - e
a1 = w1 / h1
# Calculate a new scale value based on the pixmap's aspect ratio.
w2 = self.canvas.pixmap.width() - 0.0
h2 = self.canvas.pixmap.height() - 0.0
a2 = w2 / h2
return w1 / w2 if a2 >= a1 else h1 / h2
def scaleFitWidth(self):
# The epsilon does not seem to work too well here.
w = self.centralWidget().width() - 2.0
return w / self.canvas.pixmap.width()
def paintCanvas(self):
assert not self.image.isNull(), "cannot paint null image"
self.canvas.scale = 0.01 * self.zoomWidget.value()
self.canvas.adjustSize()
self.canvas.update()
def createShape(self):
assert self.beginner()
self.canvas.setEditing(False)
self.actions.create.setEnabled(False)
def toggleDrawMode(self, edit=True):
self.canvas.setEditing(edit)
self.actions.createMode.setEnabled(edit)
self.actions.editMode.setEnabled(not edit)
def grabcutMatting(self):
if self.mattingFile is None:
self.mattingFile = Grab_cut()
def format_shape(s):
return dict(line_color=s.line_color.getRgb(),
fill_color=s.fill_color.getRgb(),
points=[(p.x(), p.y()) for p in s.points])
shape = format_shape(self.canvas.shapes[-1])
self.image_out_np = self.mattingFile.image_matting(self.filePath,
shape, iteration=10)
self.showResultImg(self.image_out_np)
self.actions.save.setEnabled(True)
def showResultImg(self, image_np):
# resize to pic
factor = min(self.pic.width() /
image_np.shape[1], self.pic.height() / image_np.shape[0])
image_np = cv2.resize(image_np, None, fx=factor,
fy=factor, interpolation=cv2.INTER_AREA)
# image_np = cv2.resize((self.pic.height(), self.pic.width()))
image = QImage(image_np, image_np.shape[1],
image_np.shape[0], QImage.Format_ARGB32)
matImg = QPixmap(image)
self.pic.setPixmap(matImg)
def saveFile(self):
self._saveFile(self.saveFileDialog())
def _saveFile(self, saved_path):
print(saved_path)
if saved_path:
Grab_cut.resultSave(saved_path, self.image_out_np)
self.setClean()
self.statusBar().showMessage('Saved to %s' % saved_path)
self.statusBar().show()
def saveFileDialog(self):
caption = '%s - Choose File' % __appname__
filters = 'File (*%s)' % 'png'
if self.default_save_dir is not None and len(self.default_save_dir):
openDialogPath = self.default_save_dir
else:
openDialogPath = self.currentPath()
print(openDialogPath)
dlg = QFileDialog(self, caption, openDialogPath, filters)
dlg.setDefaultSuffix('png')
dlg.setAcceptMode(QFileDialog.AcceptSave)
filenameWithoutExtension = os.path.splitext(self.filePath)[0]
dlg.selectFile(filenameWithoutExtension)
dlg.setOption(QFileDialog.DontUseNativeDialog, False)
if dlg.exec_():
return dlg.selectedFiles()[0]
return ''
def currentPath(self):
return os.path.dirname(self.filePath) if self.filePath else '.'
def changeSavedirDialog(self, _value=False):
if self.default_save_dir is not None:
path = self.default_save_dir
else:
path = '.'
dirpath = QFileDialog.getExistingDirectory(self,
'%s - Save annotations to the directory' % __appname__, path, QFileDialog.ShowDirsOnly
| QFileDialog.DontResolveSymlinks)
if dirpath is not None and len(dirpath) > 1:
self.default_save_dir = dirpath
self.statusBar().showMessage('%s . Annotation will be saved to %s' %
('Change saved folder', self.default_save_dir))
self.statusBar().show()
def setClean(self):
self.dirty = False
self.actions.save.setEnabled(False)
self.actions.create.setEnabled(True)
def openNextImg():
pass
def openPreImg():
pass
def scrollRequest(self, delta, orientation):
units = - delta / (8 * 15)
bar = self.scrollBars[orientation]
bar.setValue(bar.value() + bar.singleStep() * units)
def read(filename, default=None):
try:
with open(filename, 'rb') as f:
return f.read()
except Exception:
return default
def get_main_app(argv=[]):
app = QApplication(argv)
app.setApplicationName(__appname__)
app.setWindowIcon(newIcon("app"))
ex = MainWindow()
ex.show()
return app, ex
def main(argv=[]):
app, ex = get_main_app(argv)
return app.exec_()
if __name__ == '__main__':
sys.exit(main(sys.argv))