-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgui_image_selector.py
192 lines (141 loc) · 6.13 KB
/
gui_image_selector.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
from PyQt5 import QtCore
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget, QGridLayout, QLabel, QScrollArea, QAction, \
QMainWindow, QStyle, qApp, QFileDialog, QSlider, QComboBox, QPushButton
from file_processing import *
from predicting import *
class MainWindow(QMainWindow):
def __init__(self, image_dir, image_set, paths, clustering_method, architecture, pollution_dir, pollution_percent, parent=None):
super(MainWindow, self).__init__(parent)
self.all_paths = get_all_images_path(image_dir)
self.image_set = image_set
self.architecture = architecture
self.pollution_dir = pollution_dir
self.paths_processed = []
deleteAct = QAction(qApp.style().standardIcon(QStyle.SP_TrashIcon), 'Delete Selection', self)
deleteAct.setStatusTip('Will delete the selected images')
deleteAct.triggered.connect(self.delete_images)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(deleteAct)
moveAct = QAction(qApp.style().standardIcon(QStyle.SP_FileDialogStart), 'Move Selection', self)
moveAct.triggered.connect(self.move_images)
self.toolbar.addAction(moveAct)
self.classifier_combo = QComboBox(self)
for method in CLUSTERING_METHODS:
self.classifier_combo.addItem(method)
self.classifier_combo.setCurrentIndex(CLUSTERING_METHODS.index(clustering_method))
self.classifier_combo.currentIndexChanged.connect(self.restore_button)
self.toolbar.addWidget(self.classifier_combo)
self.pollution_slider = QSlider(QtCore.Qt.Horizontal, self)
self.pollution_slider.setRange(0, 40)
self.pollution_slider.setStyleSheet(self.stylesheet())
self.pollution_slider.setValue(pollution_percent)
self.pollution_slider.valueChanged.connect(self.restore_button)
self.toolbar.addWidget(self.pollution_slider)
self.predictionButton = QPushButton('New Predictions', self)
self.predictionButton.clicked.connect(self.get_new_predictions)
self.predictionButton.setDisabled(True)
self.somethingChanged = False
self.toolbar.addWidget(self.predictionButton)
self.window = Window(paths)
self.setCentralWidget(self.window)
self.show()
def delete_images(self):
current_selection = self.window.get_selection()
delete_images(current_selection)
self.paths_processed += current_selection
remaining_paths = self.window.paths
for path in self.paths_processed:
try:
remaining_paths.remove(path)
except ValueError:
pass
self.window = Window(remaining_paths)
self.setCentralWidget(self.window)
def move_images(self):
current_selection = self.window.get_selection()
dir_relocation = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
if dir_relocation:
move_images(dir_relocation, current_selection)
self.paths_processed += current_selection
remaining_paths = self.window.paths
for path in self.paths_processed:
try:
remaining_paths.remove(path)
except ValueError:
pass
self.window = Window(remaining_paths)
self.setCentralWidget(self.window)
def restore_button(self):
if not self.somethingChanged:
self.somethingChanged = True
self.predictionButton.setDisabled(False)
def stylesheet(self):
return """
QSlider::groove:horizontal {
border: 1px solid #999999;
height: 8px;
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);
margin: 2px 0;
}
QSlider::handle:horizontal {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
border: 1px solid #5c5c5c;
width: 18px;
margin: -2px 0;
border-radius: 3px;
}
"""
def get_new_predictions(self):
predictions = semi_supervised_detection(self.image_set, self.classifier_combo.currentText(), self.architecture,
self.pollution_dir, float(self.pollution_slider.value()) / 100)
image_paths = get_relevant_image_paths(self.all_paths, self.paths_processed, predictions)
self.window = Window(image_paths)
self.setCentralWidget(self.window)
self.predictionButton.setDisabled(True)
self.somethingChanged = False
class ClikableLabel(QLabel):
def __init__(self, path):
super(ClikableLabel, self).__init__()
self.width = 180
self.height = 200
self.isChecked = False
pixmap = QPixmap(path)
pixmap = pixmap.scaled(self.width, self.height, QtCore.Qt.KeepAspectRatio)
self.setPixmap(pixmap)
def mousePressEvent(self, event):
self.isChecked = not self.isChecked
if self.isChecked:
self.setStyleSheet("border: 5px inset red;")
else:
self.setStyleSheet("")
class Window(QScrollArea):
def __init__(self, paths):
QScrollArea.__init__(self)
widget = QWidget()
self.layout = QGridLayout(widget)
self.paths = paths
self.all_labels = []
self.nb_columns = 5
self.populate_grid(self.paths)
self.setWidget(widget)
self.setWidgetResizable(True)
self.setMinimumWidth(1000)
self.setMinimumHeight(600)
def populate_grid(self, paths):
row = 0
column = 0
for idx, path in enumerate(paths):
label = ClikableLabel(path)
self.all_labels.append(label)
self.layout.addWidget(self.all_labels[idx], row, column)
column += 1
if column % self.nb_columns == 0:
row += 1
column = 0
def get_selection(self):
selection = []
for idx, path in enumerate(self.paths):
if self.all_labels[idx].isChecked:
selection.append(path)
return selection