forked from MjdMahasneh/MSLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_1.5.py
374 lines (262 loc) · 12.8 KB
/
version_1.5.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
import sys
import os
from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox
from PyQt5.QtCore import *
from PyQt5.uic import loadUi
from PyQt5.QtGui import QPixmap
import cv2
import numpy as np
import getImageList as glist
import convert_string_to_list as stl
import Write_ARs_Xml_All as wxml
import Write_ARs_Xml_Separatly as wsxml
from helpers import check_dir_setup
check_dir_setup()
class LabelMe(QDialog):
def __init__(self):
super(LabelMe,self).__init__()
loadUi('LabelMe_layout_3.ui', self)
self.setWindowTitle('LabelMe')
self.NextButton.clicked.connect(self.NextButton_clicked)
self.BackButton.clicked.connect(self.BackButton_clicked)
self.SaveButton.clicked.connect(self.SaveButton_clicked)
self.x, self.y, self.width, self.height = 100, 100, 950, 650
self.initUI()
self.image_number = -1
self.flag = 0
@pyqtSlot()
def empty_cache(self):
file_path = 'cache/temp.txt'
file = open(file_path,'w')
entry = ('')
file.write(entry)
file.close()
return()
def initUI(self):
self.setGeometry(self.x, self.y, self.width, self.height)
self.show()
self.empty_cache()
def showMessageBox(self, title, message):
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Warning)
msgBox.setText(message)
msgBox.setStandardButtons(QMessageBox.Ok)
msgBox.exec_()
def SaveButton_clicked(self):
try:
base_path_1 = os.path.dirname(os.path.realpath(__file__))
xml_file_1 = os.path.join(base_path_1, "annotation_files/annotation_for_All_files.xml")
base_path_2 = os.path.dirname(os.path.realpath(__file__))
output_file_path = 'annotation_files\\'+current_image_name[:-4]+'_annotation.xml'
xml_file_2 = os.path.join(base_path_2, output_file_path)
file_path = 'cache/temp.txt'
file = open(file_path,'r')
ARs_BB = file.read()
if len(ARs_BB) == 0:
print('no data available to be saved')
self.showMessageBox('Attention','no data available to be saved')
else:
rects = stl.string_to_list(ARs_BB)
wxml.write_ARs(xml_file_1, current_image_name, rects)
wsxml.write_ARs(xml_file_2, current_image_name, rects, w, h)
file_name = 'annotation_files/'+current_image_name
cv2.imwrite(file_name, saved_img)
print('list of rects',rects)
print(current_image_name, ' rects are saved')
print('SAVING DONE!')
if self.saving_status_label.text() == '':
self.saving_status_label.setText('Changes were saved successfully!')
else:
pass
except Exception as e:
print('ops!! something went wrong : ',e)
self.showMessageBox('ops!! something went wrong : ',e)
def BackButton_clicked(self):
cv2.destroyAllWindows()
cv2.destroyWindow('Select RoI')
self.empty_cache()
imlist = glist.get_imlist('images/band_1')
if self.saving_status_label.text() == 'Changes were saved successfully!':
self.saving_status_label.setText('')
if not (self.image_number<=0):
self.image_number-=1
img_name = imlist[self.image_number]
print('image name :',img_name)
if self.img_name_label.text() != img_name:
self.img_name_label.setText('Image being processed : ' + img_name.split('\\')[-1])
else:
pass
global current_image_name
current_image_name = img_name.split('\\')[-1]
if (self.image_number+1) <= (len(imlist)-1):
img_path = imlist[self.image_number+1]
print('img_path >>>> : ', img_path)
self.NextTimeSeries_1.setPixmap(QPixmap(img_path))
self.LabelNextTimeSeries_1.setText(img_path.split('\\')[-1])
else:
self.NextTimeSeries_1.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelNextTimeSeries_1.setText('')
if (self.image_number+2) <= (len(imlist)-1):
img_path = imlist[self.image_number+2]
self.NextTimeSeries_2.setPixmap(QPixmap(img_path))
self.LabelNextTimeSeries_2.setText(img_path.split('\\')[-1])
else:
self.NextTimeSeries_2.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelNextTimeSeries_2.setText('')
if (self.image_number+3) <= (len(imlist)-1):
img_path = imlist[self.image_number+3]
self.NextTimeSeries_3.setPixmap(QPixmap(img_path))
self.LabelNextTimeSeries_3.setText(img_path.split('\\')[-1])
else:
self.NextTimeSeries_3.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelNextTimeSeries_3.setText('')
if (self.image_number - 3) >= 0:
img_path = imlist[self.image_number-3]
self.PrevTimeSeries_1.setPixmap(QPixmap(img_path))
self.LabelPrevTimeSeries_1.setText(img_path.split('\\')[-1])
else:
self.PrevTimeSeries_1.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelPrevTimeSeries_1.setText('')
if (self.image_number - 2) >= 0:
img_path = imlist[self.image_number-2]
self.PrevTimeSeries_2.setPixmap(QPixmap(img_path))
self.LabelPrevTimeSeries_2.setText(img_path.split('\\')[-1])
else:
self.PrevTimeSeries_2.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelPrevTimeSeries_2.setText('')
if (self.image_number - 1) >= 0:
img_path = imlist[self.image_number-1]
self.PrevTimeSeries_3.setPixmap(QPixmap(img_path))
self.LabelPrevTimeSeries_3.setText(img_path.split('\\')[-1])
else:
self.PrevTimeSeries_3.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelPrevTimeSeries_3.setText('')
self.bound_img(img_name)
else:
self.showMessageBox('Attention','No more images to Annotate, you can only go to next image')
def NextButton_clicked(self):
cv2.destroyAllWindows()
self.empty_cache()
imlist = glist.get_imlist('images/band_1')
if self.saving_status_label.text() == 'Changes were saved successfully!':
self.saving_status_label.setText('')
if not (self.image_number>=int(len(imlist)-1)):
self.image_number+=1
img_name = imlist[self.image_number]
print('image name :',img_name)
if self.img_name_label.text() != img_name:
self.img_name_label.setText('Image being processed : ' + img_name.split('\\')[-1])
global current_image_name
current_image_name = img_name.split('\\')[-1]
if (self.image_number+1) <= (len(imlist)-1):
img_path = imlist[self.image_number+1]
print('img_path >>>> : ', img_path)
self.NextTimeSeries_1.setPixmap(QPixmap(img_path))
self.LabelNextTimeSeries_1.setText(img_path.split('\\')[-1])
else:
self.NextTimeSeries_1.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelNextTimeSeries_1.setText('')
if (self.image_number+2) <= (len(imlist)-1):
img_path = imlist[self.image_number+2]
self.NextTimeSeries_2.setPixmap(QPixmap(img_path))
self.LabelNextTimeSeries_2.setText(img_path.split('\\')[-1])
else:
self.NextTimeSeries_2.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelNextTimeSeries_2.setText('')
if (self.image_number+3) <= (len(imlist)-1):
img_path = imlist[self.image_number+3]
self.NextTimeSeries_3.setPixmap(QPixmap(img_path))
self.LabelNextTimeSeries_3.setText(img_path.split('\\')[-1])
else:
self.NextTimeSeries_3.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelNextTimeSeries_3.setText('')
if (self.image_number - 3) >= 0:
img_path = imlist[self.image_number-3]
self.PrevTimeSeries_1.setPixmap(QPixmap(img_path))
self.LabelPrevTimeSeries_1.setText(img_path.split('\\')[-1])
else:
self.PrevTimeSeries_1.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelPrevTimeSeries_1.setText('')
if (self.image_number - 2) >= 0:
img_path = imlist[self.image_number-2]
self.PrevTimeSeries_2.setPixmap(QPixmap(img_path))
self.LabelPrevTimeSeries_2.setText(img_path.split('\\')[-1])
else:
self.PrevTimeSeries_2.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelPrevTimeSeries_2.setText('')
if (self.image_number - 1) >= 0:
img_path = imlist[self.image_number-1]
self.PrevTimeSeries_3.setPixmap(QPixmap(img_path))
self.LabelPrevTimeSeries_3.setText(img_path.split('\\')[-1])
else:
self.PrevTimeSeries_3.setPixmap(QPixmap('cache/no_image_available.jpg'))
self.LabelPrevTimeSeries_3.setText('')
self.bound_img(img_name)
else:
self.showMessageBox('Attention','No more images to Annotate, you can only go to previous image')
def bound_img(self,img_name):
img = cv2.imread(img_name)
global saved_img
saved_img = img
global h, w, channels
h, w, channels = img.shape
try:
print('img_name is here aha!! >>>>> : ', img_name)
aux_img = get_auxiliary_band(img_name)
concatenatinated = np.concatenate((img, aux_img), axis=1)
except Exception:
print('Caution : this image does not have a corrosponding auxiliary image.')
self.showMessageBox('Caution','this image does not have a corrosponding auxiliary image.')
concatenatinated = np.concatenate((img, img), axis=1)
pass
count = 0
showCrosshair = False
fromCenter = False
rects = []
while True :
cv2.namedWindow('Select RoI',cv2.WINDOW_NORMAL)
dispHeight, dispWidth = int(h/2), int(w)
cv2.resizeWindow("Select RoI", dispWidth, dispHeight)
rect = cv2.selectROI("Select RoI", concatenatinated, fromCenter, showCrosshair)
print('this is a rect ' ,rect)
if (rect[0] > w) or ((rect[0]+rect[2]) > w):
print('The coordinates of the bounding box can not be outside image')
self.showMessageBox('Warning','The coordinates of the bounding box can not be outside image.')
else:
x2 = rect[0] + rect[2]
y2 = rect[1] + rect[3]
cv2.rectangle(concatenatinated,(rect[0],rect[1]), (x2, y2),(0,255,0),3)
print('here >>')
if rect != (0,0,0,0):
rects.append(rect)
file_path = 'cache/temp.txt'
file = open(file_path,'w')
entry = (str(rects))
file.write(entry)
file.close()
count+=1
print('list of rects ',rects)
print('count',count)
print('rects', len(rects))
print ('rect',rect)
cv2.destroyWindow('Select RoI')
def get_list_of_files_starting_with(path, start):
return [os.path.join(path, f) for f in os.listdir(path) if f.startswith(start) and f.endswith('.png')]
def get_auxiliary_band(img_name):
img_name = img_name.split('\\')[-1]
aux_image_ID = img_name.split('_')[0]
print('aux_image_ID -> :', aux_image_ID)
aux_image_path = get_list_of_files_starting_with('images/band_2', aux_image_ID)[0]
aux_img = cv2.imread(aux_image_path)
return(aux_img)
def create_output_dirs():
if not os.path.exists('cache'):
os.makedirs('cache')
if not os.path.exists('annotation_files'):
os.makedirs('annotation_files')
return
create_output_dirs()
app = QApplication(sys.argv)
widget=LabelMe()
widget.show()
sys.exit(app.exec_())