-
Notifications
You must be signed in to change notification settings - Fork 0
/
Labeller.py
294 lines (173 loc) · 7.96 KB
/
Labeller.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 11 14:26:21 2018
@author: MarcusF
"""
from qtpy import QtWidgets, QtGui, QtCore
from qtpy.QtCore import Qt, QPoint
from PyQt5.QtCore import pyqtSignal
import tifffile as tf
import numpy as np
import qimage2ndarray as qnd
import sys
class LabellerMW(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.content = Labeller()
self.setCentralWidget(self.content)
self.show()
class Labeller(QtWidgets.QWidget):
#GUI which allows the user to flick through randomly sampled images and assign a label of Vesicle or No vesicle
success = pyqtSignal()
def __init__(self):
super().__init__()
layout = QtWidgets.QGridLayout()
self.imageview = Label()
self.image_nav = ImageNavigation()
self.label_select = LabelSelector()
self.loadsavecontrol = LoadSave()
self.index = 0
self.ImageHandler = ImageCollection()
self.loadsavecontrol.load_btn.clicked.connect(self.load_images)
layout.addWidget(self.loadsavecontrol,0,0)
layout.addWidget(self.label_select,1,0)
layout.addWidget(self.imageview,0,1)
layout.addWidget(self.image_nav,1,1)
self.setLayout(layout)
def check_index(self):
if self.index == self.ImageHandler.imagecollection.shape[0]:
save_prompt = QtWidgets.QMessageBox()
save_prompt.setText('You have reached the end of the image collection, please save the class labels')
save_prompt.exec_()
return True
else:
return False
def assign_class_label_vesicle(self):
if self.check_index():
return
self.ImageHandler.class_labels[self.index] = 1
def assign_class_label_not_vesicle(self):
if self.check_index():
return
self.ImageHandler.class_labels[self.index] =0
def increment_image_by_one(self):
self.index +=1
self.label_select.set_activeframe(self.ImageHandler.imagecollection[self.index])
def decrement_image_by_one(self):
self.index -=1
self.label_select.set_activeframe(self.ImageHandler.imagecollection[self.index])
def load_images(self):
ret, images = self.loadsavecontrol.load()
if ret == -1:
return
else:
self.ImageHandler.set_imagecollection(images)
self.image_nav.next.clicked.connect(self.increment_image_by_one)
self.image_nav.previous.clicked.connect(self.decrement_image_by_one)
def raise_invalid_file_error(self):
warning_box = QtWidgets.QMessageBox()
warning_box.setText('Load attempt failed. File must have extension ".npy".')
warning_box.exec_()
class ImageCollection():
imagesloaded = pyqtSignal()
def __init__(self):
#need to upload an compressed numpy ndarray which should have shape of the form NumImages x width x height
self.imagecollection = None
def set_imagecollection(self,images):
self.imagecollection = images
#construct array to house labels. This is the same size as the number of images.
self.class_labels = np.zeros((self.image_collection.shape[0],))
#input into neural network will be numpy arrays Xdata (image clips) and ydata (labels)
self.count = self.image_collection.shape[0]
def unlabelled_images_left(self):
self.count -= 1
class Label(QtWidgets.QLabel):
#class which draws a numpy array into a QLabel by first converting it into the QPixmap object via an external package
def __init__(self, parent=None):
super(Label, self).__init__(parent=parent)
self.activeframe = None
self.maxintens = None
self.h = None
self.w = None
self.size = None
self.pixsize = None
def set_activeframe(self,frame):
warning_box = QtWidgets.QMessageBox()
try:
assert len(self.frame.shape) ==2
except AssertionError:
warning_box.setText('Invalid frame selected')
warning_box.exec_()
self.activeframe = frame
self.h = frame.shape[0]
self.w = frame.shape[1]
self.maxintens = np.ax(self.activeframe)
self.update()
def paintEvent(self, e):
super().paintEvent(e)
qp = QtGui.QPainter(self)
if self.activeframe is not None:
img = qnd.gray2qimage(self.activeframe,normalize = (0,self.maxintens))
self.size = img.size()
pix = QtGui.QPixmap.fromImage(img).scaled(self.h,self.w,Qt.KeepAspectRatio)
self.pixsize = pix.size()
pos = QtCore.QPoint(0,0)
qp.drawPixmap(pos,pix)
else:
pos = QtCore.QPoint(0.5,0.5)
qp.drawText(pos,"Load Video to Display")
class ImageNavigation(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QHBoxLayout()
self.previous = QtWidgets.QPushButton('Previous')
self.next = QtWidgets.QPushButton('Next')
layout.addWidget(self.previous)
layout.addWidget(self.next)
self.setLayout(layout)
class LabelSelector(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout()
self.vesicle = QtWidgets.QPushButton('Vesicle')
self.vesicle.setSizePolicy(QtWidgets.QSizePolicy.Minimum,QtWidgets.QSizePolicy.Minimum)
self.vesicle.setMinimumSize(100,100)
self.novesicle = QtWidgets.QPushButton('NOT Vesicle')
self.novesicle.setSizePolicy(QtWidgets.QSizePolicy.Minimum,QtWidgets.QSizePolicy.Minimum)
self.novesicle.setMinimumSize(100,100)
layout.addWidget(self.vesicle)
layout.addWidget(self.novesicle)
self.setLayout(layout)
class LoadSave(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QGridLayout()
self.loadinput = QtWidgets.QLineEdit('Input image collection path "XXX.npy"')
self.load_btn = QtWidgets.QPushButton('Load')
self.saveinput = QtWidgets.QLineEdit('Input saving path for labels')
self.save_btn = QtWidgets.QPushButton('Save')
layout.addWidget(self.loadinput,0,0)
layout.addWidget(self.load_btn,1,0)
layout.addWidget(self.saveinput,0,1)
layout.addWidget(self.save_btn,1,1)
self.setLayout(layout)
def load(self):
if '.npy' == self.loadinput.text()[-4:]:
return 1,np.load(self.loadinput.text())
else:
warning = QtWidgets.QMessageBox()
warning.setText('Make sure file path has file type ".npy"')
warning.exec_()
return -1,None
def save(self,class_labels):
warning = QtWidgets.QMessageBox()
try:
np.save(self.saveinput.text(),class_labels)
except:
warning.setText('Save failed')
warning.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
LabellingTool = LabellerMW()
app.exec_()