-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
372 lines (315 loc) · 13.4 KB
/
main.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
#ui widget window from ui file
from PyQt5 import QtWidgets
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPixmap
from PyQt5 import QtCore
import sys
import os
import sys
sys.path.append("..")
from segment_anything import sam_model_registry, SamPredictor
import cv2
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
import numpy as np
#keyboard input library
sam_checkpoint = "models/sam_vit_b_01ec64.pth"
model_type = "vit_b"
device = "cpu"
def show_mask(mask, ax, random_color=False):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = np.array([30/255, 144/255, 255/255, 0.6])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_points(coords, labels, ax, marker_size=375):
pos_points = coords[labels==1]
neg_points = coords[labels==0]
ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)
ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)
def show_box(box, ax):
x0, y0 = box[0], box[1]
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
sam.to(device=device)
predictor = SamPredictor(sam)
#load ui file
qtCreatorFile = "window.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.show()
self.openFile.clicked.connect(self.open_file)
self.addClass.clicked.connect(self.add_class)
self.removeClass.clicked.connect(self.delete_class)
self.startButton.clicked.connect(self.start)
self.classWidget.itemClicked.connect(self.class_selected)
self.startBoxesButton.clicked.connect(self.start_boxes)
self.saveMaskButton.clicked.connect(self.save_mask)
self.boxes = np.array([])
self.boxes_masks = np.array([])
self.input_point = np.array([])
self.input_label = np.array([])
self.this_mask = {}
self.image = None
self.box_start_point = None
self.box_end_point = None
def delete_class(self):
#delete class from classWidget
class_name = self.classWidget.currentItem().text()
self.classWidget.takeItem(self.classWidget.currentRow())
#delete mask file if it exists
if os.path.exists(f"masks/{class_name}.png"):
os.remove(f"masks/{class_name}.png")
#delete mask from this_mask if it exists
if class_name in self.this_mask.keys():
del self.this_mask[class_name]
#clear mask label
self.maskLabel.clear()
def class_selected(self):
#get selected class name if there is one
if self.classWidget.currentItem() == None:
return
class_name = self.classWidget.currentItem().text()
if class_name == "":
return
#load mask from file
#check if class name in this mask keys
if self.this_mask is not None:
if class_name in self.this_mask.keys():
mask = self.this_mask[class_name]
#save mask to file
cv2.imwrite(f"masks/{class_name}.png", mask*255)
#show mask in maskLabel
pixmap = QPixmap(f"masks/{class_name}.png")
#resize image to fit in label
pixmap = pixmap.scaled(self.maskLabel.width(), self.maskLabel.height(), QtCore.Qt.KeepAspectRatio)
self.maskLabel.setPixmap(pixmap)
def save_mask(self):
#get selected class name if there is one
if self.classWidget.currentItem() == None:
#no class selected message box
QtWidgets.QMessageBox.about(self, "Error", "No class selected")
return
class_name = self.classWidget.currentItem().text()
if class_name == "":
#no class selected message box
QtWidgets.QMessageBox.about(self, "Error", "No class selected")
return
#select file to save mask to
file_name = QtWidgets.QFileDialog.getSaveFileName(self, "Save Mask", f"masks/{class_name}.png", "PNG (*.png)")
#save mask to file
cv2.imwrite(file_name[0], self.this_mask[class_name]*255)
def start_boxes(self):
#get selected class name if there is one
if self.classWidget.count() == 0:
#no classes added message box
QtWidgets.QMessageBox.about(self, "Error", "No classes added")
return
if self.classWidget.currentItem() == None:
#no class selected message box
QtWidgets.QMessageBox.about(self, "Error", "No class selected")
return
class_name = self.classWidget.currentItem().text()
if class_name == "":
#no class selected message box
QtWidgets.QMessageBox.about(self, "Error", "No class selected")
return
#clear points
self.boxes = np.array([])
self.boxes_masks = np.array([])
plt.figure(figsize=(10,10))
plt.imshow(self.image)
#show_points(self.input_point, self.input_label, plt)
plt.axis('off')
#on key or mouse click
cid = plt.gcf().canvas.mpl_connect('button_press_event', self.box_start)
#on mouse release
cid = plt.gcf().canvas.mpl_connect('button_release_event', self.box_end)
#rs = RectangleSelector(plt.gca(), self.box_selected)
plt.show()
def box_start(self, eclick):
self.box_start_point = eclick
def box_end(self, erelease):
self.box_end_point = erelease
self.box_selected(self.box_start_point, self.box_end_point)
def box_selected(self, eclick, erelease):
#print(eclick.xdata, eclick.ydata, erelease.xdata, erelease.ydata)
box = np.array([[eclick.xdata, eclick.ydata, erelease.xdata, erelease.ydata]])
#print(box)
#if right click
if eclick.button == 3:
#remove nearest box and its mask
if self.boxes.shape[0] > 0:
#get nearest box
nearest_box = np.argmin(np.linalg.norm(self.boxes - np.array([eclick.xdata, eclick.ydata, eclick.xdata, eclick.ydata]), axis=1))
#remove nearest box
self.boxes = np.delete(self.boxes, nearest_box, axis=0)
#remove nearest mask
self.boxes_masks = np.delete(self.boxes_masks, nearest_box, axis=0)
#if left click
elif eclick.button == 1:
if self.boxes.shape[0] == 0:
self.boxes = box
else:
self.boxes = np.vstack((self.boxes, box))
# predict boxes
masks,scores,logits = predictor.predict(
point_coords=None,
point_labels=None,
box=box,
multimask_output=False,
)
print(masks.shape)
# get mask with highest score
mask = masks[np.argmax(scores)]
#print(mask.shape)
# add mask to boxes_masks
if self.boxes_masks.shape[0] == 0:
self.boxes_masks = np.array([mask])
#add dimensions to mask
mask = np.expand_dims(mask, axis=0)
else:
self.boxes_masks = np.concatenate((self.boxes_masks, np.array([mask])), axis=0)
image =self.image.copy()
plt.cla()
plt.imshow(image)
current_class = self.classWidget.currentItem().text()
#clear this_mask for current class
if current_class in self.this_mask.keys():
self.this_mask[current_class] = np.zeros(self.this_mask[current_class].shape)
# show all masks,and combine them to this_mask
for mask in self.boxes_masks:
#mask plot
show_mask(mask, plt.gca())
#print(mask.shape)
#combine masks
if current_class in self.this_mask.keys():
self.this_mask[current_class] = np.logical_or(self.this_mask[current_class], mask)
else:
self.this_mask[current_class] = mask
for box_ in self.boxes :
#print(box_)
#box plot
show_box(box_, plt.gca())
#print(self.boxes_masks.shape)
plt.axis('off')
plt.draw()
def start(self):
#get selected class name if there is one
if self.classWidget.count() == 0:
#no classes added message box
QtWidgets.QMessageBox.about(self, "Error", "No classes added")
return
if self.classWidget.currentItem() == None:
#no class selected message box
QtWidgets.QMessageBox.about(self, "Error", "No class selected")
return
class_name = self.classWidget.currentItem().text()
if class_name == "":
#no class selected message box
QtWidgets.QMessageBox.about(self, "Error", "No class selected")
return
#clear points
self.input_point = np.array([])
self.input_label = np.array([])
plt.figure(figsize=(10,10))
plt.imshow(self.image)
#show_points(self.input_point, self.input_label, plt)
plt.axis('on')
#on key or mouse click
cid = plt.gcf().canvas.mpl_connect('button_press_event', self.onclick)
plt.show()
#save the mask to file with class name
#cv2.imwrite(f"{class_name}.png", self.this_mask*255)
def add_class(self):
#add class to classWidget
class_name = self.classEdit.text()
self.classWidget.addItem(class_name)
self.classEdit.setText("")
def open_file(self):
print("open file")
#get file path only image files. gif, jpg, png, bmp,tiff, tif, jpeg
file_path = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '.', "Image files (*.jpg *.gif *.png *.bmp *.tiff *.tif *.jpeg)")[0]
print(file_path)
self.fileEdit.setText(file_path)
#load image and show in imageLabel
pixmap = QPixmap(file_path)
#resize image to fit in label
pixmap = pixmap.scaled(self.imageLabel.width(), self.imageLabel.height(), QtCore.Qt.KeepAspectRatio)
self.imageLabel.setPixmap(pixmap)
image = cv2.imread(file_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.image = image
#show wait message with progress bar
predictor.set_image(image)
#show final message
#get mouse click points
def onclick(self,event):
x, y = event.xdata, event.ydata
print(f"Clicked at {x:.0f}, {y:.0f}")
#check which mouse button was pressed
if event.button == 2:
#left click
#check if ctrl key is pressed
print("Removing point")
#remove the point
i = np.argmin(np.sum(np.square(self.input_point - np.array([[x, y]])), axis=1))
#check if array is empty
if self.input_point.shape[0] == 1:
self.input_point = np.array([])
self.input_label = np.array([])
else:
self.input_point = np.delete(self.input_point, i, axis=0)
self.input_label = np.delete(self.input_label, i, axis=0)
elif event.button == 1:
print("Adding positive point")
if self.input_point.shape[0] == 0:
self.input_point = np.array([[x, y]])
self.input_label = np.array([1])
else:
self.input_label = np.concatenate([self.input_label, [1]], axis=0)
self.input_point = np.concatenate([self.input_point, [[x, y]]], axis=0)
elif event.button == 3:
#right click
print("Adding negative point")
if self.input_point.shape[0] == 0:
self.input_point = np.array([[x, y]])
self.input_label = np.array([0])
else:
self.input_label = np.concatenate([self.input_label, [0]], axis=0)
self.input_point = np.concatenate([self.input_point, [[x, y]]], axis=0)
#predict
masks, scores, logits = predictor.predict(
point_coords=self.input_point,
point_labels=self.input_label,
multimask_output=True,)
print(masks.shape, scores.shape, logits.shape)
#get the best mask
i = np.argmax(scores)
mask = masks[i]
score = scores[i]
print(f"Best mask score: {score:.3f}")
#clear the plot
plt.cla()
image =self.image.copy()
plt.imshow(image)
show_mask(mask, plt.gca())
#get class name from classWidget
class_name = self.classWidget.currentItem().text()
self.this_mask[class_name] = mask
show_points(self.input_point, self.input_label, plt.gca())
plt.title(f"Mask {i+1}, Score: {score:.3f}", fontsize=18)
plt.axis('off')
plt.draw()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyApp()
sys.exit(app.exec_())