-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseglable.py
276 lines (202 loc) · 8.89 KB
/
seglable.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
import sys
import os,shutil
from PyQt4 import QtGui, QtCore
import cv2
import numpy as np
from ImageViewerQt import ImageViewerQt
WINDOW_SIZE = 1050, 700
L_WINDOW = 150, WINDOW_SIZE[1]-20
R_WINDOW = QtCore.QRect(200, 50, WINDOW_SIZE[0]-220, WINDOW_SIZE[1]-70)
IMG_EXT = '.png'
class Seg_label_tool(QtGui.QMainWindow):
def __init__(self, args):
super(Seg_label_tool, self).__init__()
self.InitUI()
self.image_view.Moustmove_leftMouseButtonClicked.connect(self.mousemove_slot)
self.curimg = None
self.showimg = None
self.curlabel = None
def InitUI(self):
self.layout_design()
self.menu_tootbar_design()
self.sBar = self.statusBar()
def layout_design(self):
self.setGeometry(0, 0, WINDOW_SIZE[0], WINDOW_SIZE[1])
self.move_to_center()
self.setWindowTitle('seg_label_tool')
self.left_QWidget = QtGui.QWidget(self)
self.left_QWidget.setGeometry(10, 10, L_WINDOW[0], L_WINDOW[1])
vbox = QtGui.QVBoxLayout()
hbox1 = QtGui.QHBoxLayout()
labname_lab = QtGui.QLabel('label', self.left_QWidget)
self.labname_edit = QtGui.QLineEdit('defect', self.left_QWidget)
hbox1.addWidget(labname_lab)
hbox1.addWidget(self.labname_edit)
vbox.addLayout(hbox1)
hbox2 = QtGui.QHBoxLayout()
radius_lab = QtGui.QLabel('radius', self.left_QWidget)
self.radius_edit = QtGui.QLineEdit('50', self.left_QWidget)
hbox2.addWidget(radius_lab)
hbox2.addWidget(self.radius_edit)
vbox.addLayout(hbox2)
labbtn = QtGui.QPushButton('lab', self.left_QWidget)
labbtn.clicked.connect(self.labaction)
labbtn.setShortcut('Ctrl+N')
vbox.addWidget(labbtn)
# labsavebtn = QtGui.QPushButton('lab_save',self.left_QWidget)
# labsavebtn.clicked.connect(self.labSaveAction)
# labsavebtn.setShortcut('s')
# vbox.addWidget(labsavebtn)
labcancelbtn = QtGui.QPushButton('lab_erase', self.left_QWidget)
labcancelbtn.clicked.connect(self.labCancelAction)
labcancelbtn.setShortcut('c')
vbox.addWidget(labcancelbtn)
self.left_QWidget.setLayout(vbox)
self.image_view = ImageViewerQt(self)
self.image_view.setGeometry(R_WINDOW)
def menu_tootbar_design(self):
exitAction = QtGui.QAction(QtGui.QIcon('./icon/quit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.QApplication.quit)
saveAction = QtGui.QAction(QtGui.QIcon('./icon/save.png'), '&save', self)
saveAction.setShortcut('Ctrl+S')
saveAction.setStatusTip('save inf')
saveAction.triggered.connect(self.savefile)
opendirAction = QtGui.QAction(QtGui.QIcon('./icon/opendir.png'), '&opendir', self)
opendirAction.setShortcut('Ctrl+O')
opendirAction.setStatusTip('opendir inf')
opendirAction.triggered.connect(self.opendir)
nextAction = QtGui.QAction(QtGui.QIcon('./icon/next.png'), '&nextimg', self)
nextAction.setShortcut('n')
nextAction.setStatusTip('nextimg inf')
nextAction.triggered.connect(self.nextimg)
prevAction = QtGui.QAction(QtGui.QIcon('./icon/prev.png'), '&previmg', self)
prevAction.setShortcut('p')
prevAction.setStatusTip('prevtimg inf')
prevAction.triggered.connect(self.previmg)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
viewMenu = menubar.addMenu('&View')
fileMenu.addAction(exitAction)
toolbar = self.addToolBar('opendir')
toolbar.addAction(opendirAction)
toolbar = self.addToolBar('save')
toolbar.addAction(saveAction)
toolbar = self.addToolBar('Next')
toolbar.addAction(nextAction)
toolbar = self.addToolBar('prev')
toolbar.addAction(prevAction)
toolbar = self.addToolBar('Exit')
toolbar.addAction(exitAction)
def savefile(self):
if self.showimg is not None and self.curlabel is not None:
cv2.imwrite(self.filelist[self.curimg_index][0],self.showimg[:,:,(2,1,0)])
cv2.imwrite(self.filelist[self.curimg_index][1],self.curlabel[:,:,(2,1,0)])
def opendir(self):
dirname = None
dirname = QtGui.QFileDialog.getExistingDirectory(
self, 'open dir', './')
dirname = str(dirname)
if dirname:
# QtGui.QMessageBox.information(self,'inf',dirname)
self.filelist = []
if os.path.isdir(dirname):
self.result_img_dir = './result/img'
self.result_label_dir = './result/label'
if os.path.exists(self.result_img_dir):
shutil.rmtree(self.result_img_dir)
if os.path.exists(self.result_label_dir):
shutil.rmtree(self.result_label_dir)
os.makedirs(self.result_label_dir)
shutil.copytree(dirname,self.result_img_dir)
for file in os.listdir(dirname):
if os.path.splitext(file)[1] == IMG_EXT:
self.filelist.append((self.result_img_dir + '/' + file,self.result_label_dir+ '/' + file))
print len(self.filelist)
self.filelist.sort()
self.curimg_index = -1
self.nextimg()
else:
print 'please select a dir'
def show_img_fun(self,img,init=True):
frame = QtGui.QImage(img.data, img.shape[1],img.shape[0],img.shape[1]*3, QtGui.QImage.Format_RGB888)
self.image_view.setImage(frame,init)
def nextimg(self):
self.savefile()
self.curimg_index += 1
if self.curimg_index == len(self.filelist):
self.curimg_index -= 1
QtGui.QMessageBox.information(self, 'inf', 'this is no more image')
else:
self.curimg = cv2.imread(self.filelist[self.curimg_index][0])
self.curimg = cv2.cvtColor(self.curimg, cv2.COLOR_BGR2RGB)
self.curlabel = np.zeros_like(self.curimg)
self.showimg = np.copy(self.curimg)
self.show_img_fun(self.curimg,True)
self.isdrawing = True
self.islabing = True
self.sBar.showMessage(self.filelist[self.curimg_index][0].split('/')[-1])
def previmg(self):
self.savefile()
self.curimg_index -= 1
if self.curimg_index < 0:
self.curimg_index += 1
QtGui.QMessageBox.information(self, 'inf', 'this is first image')
else:
self.curimg = cv2.imread(self.filelist[self.curimg_index][0])
self.curimg = cv2.cvtColor(self.curimg, cv2.COLOR_BGR2RGB)
self.curlabel = np.zeros_like(self.curimg)
self.showimg = np.copy(self.curimg)
self.show_img_fun(self.showimg,True)
self.isdrawing = True
self.islabing = True
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Control:
self.image_view.canPan = True
self.isdrawing = False
QtGui.QMainWindow.keyPressEvent(self,e)
def keyReleaseEvent(self, QKeyEvent):
if QKeyEvent.key() == QtCore.Qt.Key_Control:
self.image_view.canPan = False
self.isdrawing = True
QtGui.QMainWindow.keyReleaseEvent(self, QKeyEvent)
def labaction(self):
self.islabing = True
# self.setCursor(QtCore.Qt.CrossCursor)
def labCancelAction(self):
self.islabing = False
# print 'helo'
# self.setCursor(QtCore.Qt.UpArrowCursor)
def move_to_center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def _cal_showimg(self,curimg,label):
tmp = np.copy(curimg)
tmp[np.where(label[:,:,0] == 255)] = (255,0,0)
return tmp
def mousemove_slot(self,x,y):
if self.isdrawing:
rad = int(self.radius_edit.text())
if rad < 0:
rad = 10
print 'radius is must positive ,now rad:%d'%rad
if self.islabing:
cv2.circle(self.curlabel,(x,y),radius=rad,color=(255,255,255),thickness=cv2.cv.CV_FILLED)
self.showimg = self._cal_showimg(self.curimg,self.curlabel)
self.show_img_fun(self.showimg,False)
self.sBar.showMessage('labing')
else:
cv2.circle(self.curlabel, (x, y), radius=rad, color=(0, 0, 0),thickness=cv2.cv.CV_FILLED)
self.showimg = self._cal_showimg(self.curimg, self.curlabel)
self.show_img_fun(self.showimg,False)
self.sBar.showMessage('lab_erase')
def main():
app = QtGui.QApplication(sys.argv)
seg = Seg_label_tool(sys.argv)
seg.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()