-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
374 lines (308 loc) · 17.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
373
374
from PyQt5.Qt import *
from PyQt5.QtCore import *
import sys
import os
import matplotlib.pyplot as plt
from ui import Ui_MainWindow
from qt_material import apply_stylesheet
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
self.actionView.triggered.connect(self.call_back_action_actionView)
self.actionDraw_Fig.triggered.connect(self.call_back_action_actionDraw_Fig)
self.actionEdit.triggered.connect(self.call_back_action_actionEdit)
self.page3_open_dir.setIcon(QIcon("./icons/open_dir.png"))
self.page3_open_file.setIcon(QIcon("./icons/open-file.png"))
self.page3_build.setIcon(QIcon("./icons/build.png"))
self.page3_save.setIcon(QIcon("./icons/save.png"))
self.page3_clear.setIcon(QIcon("./icons/clear.png"))
self.pushButton_load_table.clicked.connect(self.call_back_push_button_load_table)
self.page2_load_clip_board.clicked.connect(self.call_back_push_button_load_from_clipboard)
self.pushButton_build.clicked.connect(self.call_back_push_button_build)
self.page3_open_file.clicked.connect(self.call_back_page3_button_open_file)
self.page3_open_model_path.clicked.connect(self.call_back_page3_button_open_model_path)
self.page3_img_list_widget.currentRowChanged.connect(self.call_back_page3_img_list_widget_clicked)
self.page3_img_list_widget.clicked.connect(self.call_back_page3_img_list_widget_clicked)
self.page3_build.clicked.connect(self.call_back_page3_img_list_widget_clicked)
self.page3_heatmap.toggled.connect(self.call_back_clicked_page3_heatmap)
self.page3_bbox.toggled.connect(self.call_back_clicked_page3_boudingbox)
self.H_Max, self.W_Max = 960, 960
self.page1_imglistWidget.dropEvent = self.dropEvent
self.page1_imglistWidget.dragEnterEvent = self.dragEnterEvent
self.page1_imglistWidget.setStyleSheet(
'border-width: 1px;background: white;border-style: solid;border-color: rgb(125, 125, 125);')
self.page1_result_listwidget.setStyleSheet(
'border-width: 1px;background: white;border-style: solid;border-color: rgb(125, 125, 125);')
self.page1_text_outputs.setStyleSheet('border-width: 1px;border-style: solid;border-color: rgb(125, 125, 125);')
# self.setMouseTracking(True)
self.mousePosX, self.mousePosY = 120, 38
self.mousePosX_gap, self.mousePosY_gap = 0, 0
self.mousePosX_Flag, self.mousePosY_Flag = False, False
self.page2_tableWidget.verticalHeader().setVisible(True)
self.page2_tableWidget.horizontalHeader().setVisible(True)
def eventFilter(self, objwatched, event):
eventType = event.type()
for i in self.children():
if isinstance(i, QVBoxLayout) or isinstance(i, QHBoxLayout) \
or isinstance(i, QLayout) or isinstance(i, QAction):
# if not isinstance(i, QListWidget) and not isinstance(i, QListWidgetItem):
self.is_focus_keyboard_event(objwatched, eventType, i)
return super().eventFilter(objwatched, event)
def is_focus_keyboard_event(self, watched, event, obj):
if watched == obj:
if event == QEvent.FocusIn:
obj.grabKeyboard()
elif event == QEvent.FocusIn:
obj.releaseKeyboard()
def call_back_action_actionView(self):
self.stackedWidget.setCurrentIndex(0)
def call_back_action_actionDraw_Fig(self):
self.stackedWidget.setCurrentIndex(1)
def call_back_action_actionEdit(self):
self.stackedWidget.setCurrentIndex(2)
def call_back_clicked_page3_heatmap(self):
self.page3_stackedWidget.setCurrentIndex(0)
def call_back_clicked_page3_boudingbox(self):
self.page3_stackedWidget.setCurrentIndex(1)
def call_back_push_button_load_table(self):
file, filetype = QFileDialog.getOpenFileName(self, "Select file", "",
"All Files (*);;Txt Files (*.txt)")
try:
log = open(file, 'r', encoding='utf-8').readlines()
spliter = self.comboBox_spliter.currentText()
gap_line = self.spinBox_gap_line.value()
import re
n = self.page2_tableWidget.columnCount()
self.page2_tableWidget.setRowCount(len(list(range(0, len(log), gap_line))))
for i in range(0, len(log), gap_line):
pattern = re.compile(r"\d+\.?\d*")
res = re.findall(pattern, log[i].strip())
self.page2_tableWidget.setColumnCount(n + len(res))
for j in range(len(res)):
row_item = QTableWidgetItem(res[j])
self.page2_tableWidget.setItem(i, n + j, row_item)
except Exception as e:
print(e)
def call_back_push_button_load_from_clipboard(self):
try:
log = clipboard.mimeData()
if not (log.formats() == ['text/plain']):
return
log = log.text().strip().split('\n')
spliter = self.comboBox_spliter.currentText()
gap_line = self.spinBox_gap_line.value()
import re
n = self.page2_tableWidget.columnCount()
self.page2_tableWidget.setRowCount(len(list(range(0, len(log), gap_line))))
for i in range(0, len(log), gap_line):
pattern = re.compile(r"\d+\.?\d*")
res = re.findall(pattern, log[i].strip())
self.page2_tableWidget.setColumnCount(n + len(res))
for j in range(len(res)):
row_item = QTableWidgetItem(res[j])
self.page2_tableWidget.setItem(i, n + j, row_item)
except Exception as e:
print(e)
def call_back_push_button_build(self):
try:
x = int(self.page2_x_column.toPlainText()) - 1
y = [int(i) - 1 for i in self.page2_y_column.toPlainText().split(';')]
x_s = [float(self.page2_tableWidget.item(i, x).text())
for i in range(self.page2_tableWidget.rowCount())]
y_s = []
for i in y:
y_s.append([float(self.page2_tableWidget.item(j, i).text())
for j in range(self.page2_tableWidget.rowCount())])
fig = plt.figure()
color = ['green', 'blue', 'red', 'pink', 'black', 'darkblue']
titles = [x for x in self.page2_y_title.toPlainText().split(';')]
for i in range(len(y_s)):
plt.plot(x_s, y_s[i], color=color[i], label=titles[i])
plt.xlabel(self.page2_x_title.toPlainText())
plt.legend()
plt.show()
plt.title(self.page2_fig_title.toPlainText())
except Exception as e:
print(e)
def call_back_page3_button_open_file(self):
files, filetype = QFileDialog.getOpenFileNames(self, "Select files", ".",
"All Files (*);;Img Files (*.png, *.jpg, *.jpeg)")
try:
for file in files:
if file.endswith('.jpg') or file.endswith('.png'):
self.additem(file, self.page3_img_list_widget)
except:
pass
def call_back_page3_button_open_model_path(self):
file, filetype = QFileDialog.getOpenFileName(self, "Select file", ".", "All Files (*);;Model Files (*.pth, *.pt)")
try:
self.page3_model_path.setText(file)
except:
pass
def additem(self, img_path, obj, set_text=True):
pix1 = QPixmap(img_path).scaled(self.page1_imglistWidget.iconSize())
item = QListWidgetItem()
item.setIcon(QIcon(pix1.scaled(self.H_Max, self.W_Max, Qt.KeepAspectRatio, Qt.SmoothTransformation)))
if set_text:
item.setToolTip(img_path)
# item.setText(os.path.split(img_path)[-1])
obj.addItem(item)
def call_back_page3_img_list_widget_clicked(self, event): # set background_img
try:
item = self.page3_img_list_widget.selectedItems()[0]
h, w = self.page3_target_img.height(), self.page3_target_img.width()
img = QPixmap(item.toolTip()).scaled(w, h, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.page3_target_img.setPixmap(img)
self.page3_result_img.setPixmap(QPixmap(''))
if self.page3_heatmap.isChecked():
from lib.gradCam import GradCAM
import cv2
from PIL import Image
img_path = item.toolTip()
model_path = self.page3_model_path.text().strip()
model_name = self.page3_model_name.currentText()
select_t_layer = False if self.page3_target_layer.currentText() == 'Auto' else True
if self.page3_model_path.text().strip() is not '':
class_index = int(self.page3_category.text().strip())
else:
class_index = None
grad_cam = GradCAM(img_path, model_name, model_path, select_t_layer, class_index)
print('Class_index for {} is {}.'.format(img_path, grad_cam.class_index))
heat_map = grad_cam()
# img = QPixmap(item.toolTip()).scaled(w, h, Qt.KeepAspectRatio, Qt.SmoothTransformation)
heat_map = cv2.cvtColor(heat_map, cv2.COLOR_BGR2RGB)
heat_map = Image.fromarray(heat_map)
heat_map = heat_map.toqpixmap()
heat_map = heat_map.scaled(w, h, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.page3_result_img.setPixmap(heat_map)
except Exception as e:
print(e)
def keyReleaseEvent(self, e: QKeyEvent): # sometimes keyPressEvent not work, switch to keyReleaseEvent
try:
if e.key() == Qt.Key_Delete:
items = self.page1_imglistWidget.selectedItems()
for i in items:
self.page1_imglistWidget.takeItem(self.page1_imglistWidget.indexFromItem(i).row())
selected_items = self.page2_tableWidget.selectedItems()
if len(selected_items) == self.page2_tableWidget.rowCount():
Columns = set([x.column() for x in selected_items])
for i in Columns:
self.page2_tableWidget.removeColumn(i)
elif len(selected_items) == self.page2_tableWidget.columnCount():
Rows = set([x.row() for x in selected_items])
for i in Rows:
self.page2_tableWidget.removeRow(i)
elif e.key() == Qt.Key_Minus:
self.page1_imglistWidget.setIconSize(self.page1_imglistWidget.iconSize() * 0.9)
elif e.key() == Qt.Key_Plus:
if not self.page1_imglistWidget.iconSize().height() >= self.H_Max:
self.page1_imglistWidget.setIconSize(self.page1_imglistWidget.iconSize() * 1.1)
elif e.key() == Qt.Key_Left:
items = self.page1_imglistWidget.selectedItems() # 获取所有的拖入item
for i in items:
current_row = self.page1_imglistWidget.indexFromItem(i).row()
self.page1_imglistWidget.takeItem(self.page1_imglistWidget.indexFromItem(i).row()) # 实时移除来源item
self.page1_imglistWidget.insertItem(current_row - 1, i)
self.page1_imglistWidget.item(current_row - 1).setSelected(True)
elif e.key() == Qt.Key_Right:
items = self.page1_imglistWidget.selectedItems() # 获取所有的拖入item
for i in items:
current_row = self.page1_imglistWidget.indexFromItem(i).row()
self.page1_imglistWidget.takeItem(self.page1_imglistWidget.indexFromItem(i).row()) # 实时移除来源item
self.page1_imglistWidget.insertItem(current_row + 1, i)
self.page1_imglistWidget.item(current_row + 1).setSelected(True)
except Exception as e:
print(e)
def dragEnterEvent(self, e: QDragEnterEvent):
e.accept()
def dropEvent(self, e: QDropEvent):
try:
img_paths = [x.toLocalFile() for x in e.mimeData().urls()]
import glob
for path in img_paths:
if os.path.isdir(path):
files = glob.glob(path + '/*')
for file in files:
if file.endswith('.jpg') or file.endswith('.png'):
self.additem(file, self.page1_imglistWidget)
else:
self.additem(path, self.page1_imglistWidget)
e.accept()
except:
pass
# TODO: fix the move item problem
try:
pos = e.pos() # 获取拖入事件的坐标
current_item = self.page1_imglistWidget.itemAt(pos) # 获取当前坐标下的item
current_index = self.page1_imglistWidget.indexFromItem(current_item) # 获取该item的index
current_row = current_index.row() # 获取行数
items = self.page1_imglistWidget.selectedItems() # 获取所有的拖入item
for i in items:
self.page1_imglistWidget.takeItem(self.page1_imglistWidget.indexFromItem(i).row()) # 实时移除来源item
self.page1_imglistWidget.insertItem(current_row, i)
e.accept()
except:
pass
e.ignore()
def mousePressEvent(self, event):
n = event.button()
# print(event.x(), event.y()) # (465, 298)
#
# print(self.page1_imglistWidget.geometry().x(), self.page1_imglistWidget.geometry().x() + self.page1_imglistWidget.geometry().width()) # 9 376
# print(self.page1_imglistWidget.geometry().y(), self.page1_imglistWidget.geometry().y() + self.page1_imglistWidget.geometry().height()) # 9 267
# TODO: fix different style
relative_pos_x, relative_pos_y = event.x() - self.mousePosX_gap - self.page1_imglistWidget.geometry().x(), \
event.y() - self.mousePosY_gap - self.page1_imglistWidget.geometry().y()
if relative_pos_x < self.page1_imglistWidget.geometry().width() + 20 and relative_pos_x > self.page1_imglistWidget.geometry().width() - 20:
self.mousePosX = event.x()
self.mousePosX_Flag = True
if relative_pos_y < self.page1_imglistWidget.geometry().height() + 20 and relative_pos_y > self.page1_imglistWidget.geometry().height() - 20:
self.mousePosY = event.y()
self.mousePosY_Flag = True
def mouseDoubleClickEvent(self, event):
self.mousePosX_gap = event.x() - self.page1_imglistWidget.geometry().x() - self.page1_imglistWidget.geometry().width()
self.mousePosY_gap = event.y() - self.page1_imglistWidget.geometry().y() - self.page1_imglistWidget.geometry().height()
# print(self.mousePosX_gap, self.mousePosY_gap)
def mouseReleaseEvent(self, event):
self.mousePosX_Flag = False
self.mousePosY_Flag = False
def mouseMoveEvent(self, event):
if self.mousePosX_Flag:
xChanged = event.x() - self.mousePosX
self.page1_imglistWidget.resize(self.page1_imglistWidget.geometry().width() + xChanged,
self.page1_imglistWidget.geometry().height())
self.page1_result_listwidget.setGeometry(self.page1_result_listwidget.geometry().x() + xChanged,
self.page1_result_listwidget.geometry().y(),
self.page1_result_listwidget.geometry().width() - xChanged,
self.page1_result_listwidget.geometry().height())
self.mousePosX = event.x()
if self.mousePosY_Flag:
yChanged = event.y() - self.mousePosY
self.page1_imglistWidget.resize(self.page1_imglistWidget.geometry().width(),
self.page1_imglistWidget.geometry().height() + yChanged)
self.page1_result_listwidget.setGeometry(self.page1_result_listwidget.geometry().x(),
self.page1_result_listwidget.geometry().y(),
self.page1_result_listwidget.geometry().width(),
self.page1_result_listwidget.geometry().height() + yChanged)
self.page1_text_outputs.setGeometry(self.page1_text_outputs.geometry().x(),
self.page1_text_outputs.geometry().y() + yChanged,
self.page1_text_outputs.geometry().width(),
self.page1_text_outputs.geometry().height() - yChanged)
self.mousePosY = event.y()
class message(QThread):
signal = pyqtSignal()
def __init__(self, Window):
super(message, self).__init__()
self.window = Window
def run(self):
self.signal.emit()
if __name__ == "__main__":
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
clipboard = app.clipboard()
myWin = MyMainWindow()
apply_stylesheet(app, theme='default_light.xml')
myWin.show()
app.installEventFilter(myWin)
sys.exit(app.exec_())