forked from dwaithe/quantifly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2_release.py
1228 lines (1021 loc) · 48.5 KB
/
v2_release.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Main script for running QuantiFly training.
import time
import numpy as np
from PyQt4 import QtGui, QtCore,QtWebKit
import errno
import os
import os.path
import re
import cPickle as pickle
import sys
from scipy.special import _ufuncs_cxx
import sklearn.utils.lgamma
from gnu import return_license
import matplotlib.lines as lines
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import v2_functions as v2
"""QuantiFly Software v2.0
Copyright (C) 2015 Dominic Waithe
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
class fileDialog(QtGui.QMainWindow):
"""The dialog for loading images"""
def __init__(self,parent):
super(fileDialog, self).__init__()
self.parent = parent
self.initUI()
self.parent.config ={}
try:
self.parent.config = pickle.load(open(os.path.expanduser('~')+'/.densitycount/config.p', "rb" ));
self.parent.filepath = self.parent.config['filepath']
except:
self.parent.filepath = os.path.expanduser('~')+'/'
try:
os.makedirs(os.path.expanduser('~')+'/.densitycount/')
except:
'unable to make directory: ',os.path.expanduser('~')+'/.densitycount/'
def initUI(self):
self.textEdit = QtGui.QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open new File')
openFile.triggered.connect(self.showDialog)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
self.setGeometry(300, 300, 350, 500)
self.setWindowTitle('File dialog')
#self.show()
def showDialog(self):
par_obj.file_array =[]
par_obj.feat_arr ={}
par_obj.RF ={}
self.parent.selIntButton.setEnabled(False)
#filepath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
for path in QtGui.QFileDialog.getOpenFileNames(self, 'Open file',self.parent.filepath,'Images(*.tif *.tiff *.png);;'):
par_obj.file_array.append(path)
self.parent.config['filepath'] = str(QtCore.QFileInfo(path).absolutePath())+'/'
pickle.dump(self.parent.config, open(str(os.path.expanduser('~')+'/.densitycount/config.p'), "w" ))
self.parent.image_status_text.showMessage('Status: Loading Images. ')
success, updateText = v2.import_data_fn(par_obj, par_obj.file_array)
#loadWin.CH_cbx1.stateChange()
self.parent.image_status_text.showMessage(updateText)
if success == True:
self.parent.updateAfterImport()
class Load_win_fn(QtGui.QWidget):
"""The class for loading and processing images"""
def __init__(self,par_obj,win):
super(Load_win_fn, self).__init__()
#Vertical layout
vbox = QtGui.QVBoxLayout()
self.setLayout(vbox)
hbox0 = QtGui.QHBoxLayout()
vbox.addLayout(hbox0)
#Load images button
self.loadImages_button = QtGui.QPushButton("Load Images", self)
hbox0.addWidget(self.loadImages_button)
hbox0.addStretch()
about_btn = QtGui.QPushButton('About')
about_btn.clicked.connect(self.on_about)
hbox0.addWidget(about_btn)
#Load button.
self.Text_CHopt = QtGui.QLabel()
vbox.addWidget(self.Text_CHopt)
self.ex = fileDialog(self)
self.loadImages_button.clicked.connect(self.ex.showDialog)
#SigmaData input field.
self.feature_scale_input = QtGui.QLineEdit(str(par_obj.feature_scale))
#SigmaData input Text.
self.feature_scaleText = QtGui.QLabel()
hbox1 = QtGui.QHBoxLayout()
vbox.addWidget(self.feature_scaleText)
vbox.addLayout(hbox1)
self.feature_scaleText.resize(40,20)
self.feature_scaleText.setText('Input sigma for feature calculation default (0.8):')
self.feature_scaleText.hide()
hbox1.addWidget(self.feature_scale_input)
hbox1.addStretch()
self.feature_scale_input.resize(10,10)
self.feature_scale_input.textChanged[str].connect(self.feature_scale_change)
self.feature_scale_input.hide()
CH_pX = 0
CH_pY = 50
#Channel dialog generation.
vbox.addWidget(self.Text_CHopt)
self.Text_CHopt.setText('Please select which channels you want to include in the training:')
self.Text_CHopt.resize(500,40)
self.Text_CHopt.hide()
hbox2 = QtGui.QHBoxLayout()
#Object factory for channel selection.
for i in range(0,10):
name = 'self.CH_cbx'+str(i+1)+'_txt = QtGui.QLabel()'
exec(name)
name = 'self.CH_cbx'+str(i+1)+'_txt.setText(\'CH '+str(i+1)+':\')'
exec(name)
name = 'hbox2.addWidget(self.CH_cbx'+str(i+1)+'_txt)'
exec(name)
name = 'self.CH_cbx'+str(i+1)+'_txt.hide()'
exec(name)
name = 'self.CH_cbx'+str(i+1)+'= checkBoxCH()'
exec(name)
name = 'hbox2.addWidget(self.CH_cbx'+str(i+1)+')'
exec(name)
name = 'self.CH_cbx'+str(i+1)+'.hide()'
exec(name)
name = 'self.CH_cbx'+str(i+1)+'.setChecked(True)'
exec(name)
name = 'self.CH_cbx'+str(i+1)+'.type =\'feature_ch\''
exec(name)
hbox2.addStretch()
vbox.addLayout(hbox2)
self.figure1 = Figure(figsize=(2,2))
self.canvas1 = FigureCanvas(self.figure1)
#self.figure1.patch.set_facecolor('white')
self.plt1 = self.figure1.add_subplot(1,1,1)
self.resize(100,100)
self.canvas1.hide()
self.figure1.subplots_adjust(left=0.001, right=0.999, top=0.999, bottom=0.001)
self.plt1.set_xticklabels([])
self.plt1.set_yticklabels([])
hbox3 = QtGui.QHBoxLayout()
vbox.addLayout(hbox3)
hbox3.addWidget(self.canvas1)
hbox3.addStretch()
#Channel dialog generation.
self.Text_FrmOpt2 = QtGui.QLabel()
vbox.addWidget(self.Text_FrmOpt2)
self.Text_FrmOpt2.hide()
#Image frames dialog.
Text_FrmOpt1_panel = QtGui.QHBoxLayout()
self.Text_FrmOpt1 = QtGui.QLabel()
self.Text_FrmOpt1.setText('Please choose the frames you wish to use for training. Use either \',\' to separate individual frames or a \'-\' to indicate a range:')
self.Text_FrmOpt1.hide()
Text_FrmOpt1_panel.addWidget(self.Text_FrmOpt1)
vbox.addLayout(Text_FrmOpt1_panel)
#Image frames input.
linEdit_Frm_panel = QtGui.QHBoxLayout()
self.linEdit_Frm = QtGui.QLineEdit()
self.linEdit_Frm.hide()
linEdit_Frm_panel.addWidget(self.linEdit_Frm)
linEdit_Frm_panel.addStretch()
vbox.addLayout(linEdit_Frm_panel)
#Feature calculation to perform:
self.Text_Radio = QtGui.QLabel()
vbox.addWidget(self.Text_Radio)
self.Text_Radio.setText('Feature select which kind of feature detection you would like to use:')
self.Text_Radio.resize(500,40)
self.Text_Radio.hide()
self.radio_group=QtGui.QButtonGroup(self) # Number
self.r0 = QtGui.QRadioButton("Basic",self)
self.r1 = QtGui.QRadioButton("Fine",self)
self.r1.setChecked(True)
self.radio_group.addButton(self.r0)
self.radio_group.addButton(self.r1)
vbox.addWidget(self.r0)
vbox.addWidget(self.r1)
self.r0.hide()
self.r1.hide()
vbox.addStretch()
#Green status text.
#Load images button
hbox1 = QtGui.QHBoxLayout()
vbox.addLayout(hbox1)
self.confirmImages_button = QtGui.QPushButton("Confirm Images")
hbox1.addWidget(self.confirmImages_button)
self.confirmImages_button.clicked.connect(self.processImgs)
self.confirmImages_button.setEnabled(False)
#Move to training button.
self.selIntButton = QtGui.QPushButton("Goto Training")
hbox1.addWidget(self.selIntButton)
self.selIntButton.clicked.connect(win.loadTrainFn)
self.selIntButton.setEnabled(False)
self.image_status_text = QtGui.QStatusBar()
self.image_status_text.setStyleSheet("QLabel { color : green }")
self.image_status_text.showMessage('Status: Highlight training images in folder. ')
vbox.addWidget(self.image_status_text)
hbox1.addStretch()
#File browsing functions
layout = QtGui.QVBoxLayout()
def on_about(self):
self.about_win = QtGui.QWidget()
self.about_win.setWindowTitle('About QuantiFly Software v2.0')
license = return_license()
#with open (sys.path[0]+'/GNU GENERAL PUBLIC LICENSE.txt', "r") as myfile:
# data=myfile.read().replace('\n', ' ')
# license.append(data)
# And give it a layout
layout = QtGui.QVBoxLayout()
self.view = QtWebKit.QWebView()
self.view.setHtml('''
<html>
<body>
<form>
<h1 >About</h1>
<p>Software written by Dominic Waithe (c) 2015</p>
'''+str(license)+'''
</form>
</body>
</html>
''')
layout.addWidget(self.view)
self.about_win.setLayout(layout)
self.about_win.show()
self.about_win.raise_()
def feature_scale_change(self,text):
"""Updates on change of feature scale"""
par_obj.feature_scale = float(text)
def updateAfterImport(self):
"""Specific to ui updates"""
if par_obj.file_ext == 'tif' or par_obj.file_ext == 'tiff':
if par_obj.tiff_file.maxFrames >1:
self.linEdit_Frm.setText('1-'+str(par_obj.uploadLimit))
self.Text_FrmOpt2.setText('There are '+str(par_obj.tiff_file.maxFrames+1)+' frames in total.')
self.Text_FrmOpt1.show()
self.Text_FrmOpt2.show()
self.linEdit_Frm.show()
self.confirmImages_button.setEnabled(True)
self.plt1.cla()
self.plt1.imshow(255-par_obj.ex_img)
self.plt1.set_xticklabels([])
self.plt1.set_yticklabels([])
self.canvas1.show()
self.canvas1.draw()
par_obj.ch_active =[];
if par_obj.numCH> 2:
self.Text_CHopt.show()
for i in range(0,par_obj.numCH):
name = 'self.CH_cbx'+str(i+1)+'.show()'
exec(name)
name = 'self.CH_cbx'+str(i+1)+'_txt.show()'
exec(name)
par_obj.ch_active.append(i)
else:
par_obj.ch_active.append(0)
self.feature_scale_input.show()
self.feature_scaleText.show()
self.r0.show()
self.r1.show()
self.Text_Radio.show()
def hyphen_range(self,s):
""" yield each integer from a complex range string like "1-9,12, 15-20,23"
>>> list(hyphen_range('1-9,12, 15-20,23'))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 16, 17, 18, 19, 20, 23]
>>> list(hyphen_range('1-9,12, 15-20,2-3-4'))
Traceback (most recent call last):
...
ValueError: format error in 2-3-4
"""
for x in s.split(','):
elem = x.split('-')
if len(elem) == 1: # a number
yield int(elem[0])
elif len(elem) == 2: # a range inclusive
start, end = map(int, elem)
for i in xrange(start, end+1):
yield i
else: # more than one hyphen
raise ValueError('format error in %s' % x)
def processImgs(self):
"""Loads images and calculates the features."""
#Resets everything should this be another patch of images loaded.
imgs =[]
gt_im_sing_chgs =[]
fmStr = self.linEdit_Frm.text()
par_obj.feat_arr ={}
par_obj.pred_arr ={}
par_obj.sum_pred ={}
par_obj.frames_2_load ={}
par_obj.left_2_calc =[]
par_obj.saved_ROI =[]
par_obj.saved_dots=[]
par_obj.curr_img = 0
par_obj.eval_load_im_win_eval = False
if self.r0.isChecked():
par_obj.feature_type = 'basic'
if self.r1.isChecked():
par_obj.feature_type = 'fine'
#Now we commit our options to our imported files.
if par_obj.file_ext == 'png':
for i in range(0,par_obj.file_array.__len__()):
par_obj.left_2_calc.append(i)
par_obj.frames_2_load[i] = [0]
self.image_status_text.showMessage('Status: Loading Images. Loading Image Num: '+str(par_obj.file_array.__len__()))
v2.im_pred_inline_fn(par_obj, self)
elif par_obj.file_ext =='tiff' or par_obj.file_ext =='tif':
if par_obj.tiff_file.maxFrames>1:
for i in range(0,par_obj.file_array.__len__()):
par_obj.left_2_calc.append(i)
try:
np.array(list(self.hyphen_range(fmStr)))-1
par_obj.frames_2_load[i] = np.array(list(self.hyphen_range(fmStr)))-1
except:
self.image_status_text.showMessage('Status: The supplied range of image frames is in the wrong format. Please correct and click confirm images.')
return
self.image_status_text.showMessage('Status: Loading Images.')
v2.im_pred_inline_fn(par_obj, self)
else:
for i in range(0,par_obj.file_array.__len__()):
par_obj.left_2_calc.append(i)
par_obj.frames_2_load[i] = [0]
v2.im_pred_inline_fn(par_obj, self)
count = 0
for b in par_obj.left_2_calc:
frames =par_obj.frames_2_load[b]
for i in frames:
count = count+1
par_obj.test_im_start= 0
par_obj.test_im_end= count
for i in par_obj.left_2_calc:
im_array = np.zeros((par_obj.height,par_obj.width))
par_obj.dense_array[i]=im_array
self.image_status_text.showMessage('Status: Images loaded. Click \'Goto Training\'')
self.selIntButton.setEnabled(True)
par_obj.imgs = imgs
def report_progress(self,message):
self.image_status_text.showMessage('Status: '+message)
app.processEvents()
class Win_fn(QtGui.QWidget):
"""Class which houses main training functionality"""
def __init__(self,par_obj):
super(Win_fn, self).__init__()
#Sets up the figures for displaying images.
self.figure1 = Figure(figsize=(8, 8), dpi=100)
self.canvas1 = FigureCanvas(self.figure1)
self.figure1.patch.set_facecolor('grey')
self.plt1 = self.figure1.add_subplot(1, 1, 1)
im_RGB = np.zeros((512, 512))
#Makes sure it spans the whole figure.
self.figure1.subplots_adjust(left=0.001, right=0.999, top=0.999, bottom=0.001)
self.plt1.imshow(255-im_RGB)
#Removes the tick labels
self.plt1.set_xticklabels([])
self.plt1.set_yticklabels([])
#Initialises the second figure.
self.figure2 = Figure(figsize=(8, 8), dpi=100)
self.canvas2 = FigureCanvas(self.figure2)
self.figure2.patch.set_facecolor('grey')
self.plt2 = self.figure2.add_subplot(1, 1, 1)
#Makes sure it spans the whole figure.
self.figure2.subplots_adjust(left=0.001, right=0.999, top=0.999, bottom=0.001)
self.plt2.imshow(255-im_RGB)
self.plt2.set_xticklabels([])
self.plt2.set_yticklabels([])
#The ui for training
self.count_txt = QtGui.QLabel()
self.image_num_txt = QtGui.QLabel()
box = QtGui.QVBoxLayout()
self.setLayout(box)
#Widget containing the top panel.
top_panel = QtGui.QHBoxLayout()
#Top left and right widget panels
top_left_panel = QtGui.QGroupBox('Basic Controls')
top_right_panel = QtGui.QGroupBox('Advanced Controls')
#Grid layouts for the top and left panels.
self.top_left_grid = QtGui.QGridLayout()
self.top_right_grid = QtGui.QGridLayout()
self.top_left_grid.setSpacing(2)
self.top_right_grid.setSpacing(2)
#Widgets for the top panel.
top_panel.addWidget(top_left_panel)
top_panel.addWidget(top_right_panel)
top_panel.addStretch()
#Set the layout of the panels to be the grids.
top_left_panel.setLayout(self.top_left_grid)
top_right_panel.setLayout(self.top_right_grid)
#Sets up the button which changes to the prev image
self.prev_im_btn = QtGui.QPushButton('Prev Image')
self.prev_im_btn.setEnabled(True)
#Sets up the button which changes to the next Image.
self.next_im_btn = QtGui.QPushButton('Next Image')
self.next_im_btn.setEnabled(True)
#Sets the current text.
self.image_num_txt.setText('The Current Image is: ' + str(par_obj.curr_img +1))
self.count_txt = QtGui.QLabel()
#Sets up the button which saves the ROI.
self.save_ROI_btn = QtGui.QPushButton('Save ROI')
self.save_ROI_btn.setEnabled(True)
#Sets up the button which saves the ROI.
self.save_dots_btn = QtGui.QPushButton('Save Dots')
self.save_dots_btn.setEnabled(False)
#Button for training model
self.train_model_btn = QtGui.QPushButton('Train Model')
self.train_model_btn.setEnabled(False)
#Selects and reactivates an existing ROI.
self.sel_ROI_btn = QtGui.QPushButton('Select ROI')
self.sel_ROI_btn.setEnabled(True)
self.select_ROI= False
#Allows deletion of dots.
self.remove_dots_btn = QtGui.QPushButton('Remove Dots')
self.remove_dots_btn.setEnabled(False)
self.remove_dots = False
#Populates the grid with the different widgets.
self.top_left_grid.addWidget(self.prev_im_btn, 0, 0)
self.top_left_grid.addWidget(self.next_im_btn, 0, 1)
self.top_left_grid.addWidget(self.image_num_txt, 2, 0, 2, 3)
self.top_left_grid.addWidget(self.save_ROI_btn, 4, 0)
self.top_left_grid.addWidget(self.save_dots_btn, 4, 1)
self.top_left_grid.addWidget(self.train_model_btn, 4, 2)
self.top_left_grid.addWidget(self.sel_ROI_btn, 5, 0)
self.top_left_grid.addWidget(self.remove_dots_btn, 5, 1)
#SigmaData input Label.
self.sigma_data_text = QtGui.QLabel(self)
self.sigma_data_text.setText('Input Sigma for Kernel Size:')
self.top_right_grid.addWidget(self.sigma_data_text, 0, 0)
#SigmaData input field.
self.sigma_data_input = QtGui.QLineEdit(str(par_obj.sigma_data))
self.sigma_data_input.onChanged = self.sigmaOnChange
self.sigma_data_input.setFixedWidth(40)
self.sigma_data_input.textChanged[str].connect(self.sigmaOnChange)
self.top_right_grid.addWidget(self.sigma_data_input, 0, 1)
#Feature scale input Label.
#self.sigma_data_text = QtGui.QLabel()
#self.sigma_data_text.setText('Scale of Feature Descriptor:')
#self.top_right_grid.addWidget(self.sigma_data_text, 1, 0)
#Feature scale input field
#self.feature_scale_input = QtGui.QLineEdit(str(par_obj.feature_scale))
#self.feature_scale_input.onChanged = self.feature_scale_change
#self.feature_scale_input.resize(40, 20)
#self.feature_scale_input.textChanged[str].connect(self.feature_scale_change)
#self.feature_scale_input.setFixedWidth(40)
#self.top_right_grid.addWidget(self.feature_scale_input, 1, 1)
#Feature scale input btn.
#self.feat_scale_change_btn = QtGui.QPushButton('Recalculate Features')
#self.feat_scale_change_btn.setEnabled(True)
#self.top_right_grid.addWidget(self.feat_scale_change_btn, 1, 2)
#Saves the model
self.save_model_btn = QtGui.QPushButton('Save Training Model')
self.save_model_btn.setEnabled(False)
self.top_right_grid.addWidget(self.save_model_btn, 1, 0)
#Saves the extremel random decision tree model
self.save_model_name_txt = QtGui.QLineEdit('Insert Model Name')
self.top_right_grid.addWidget(self.save_model_name_txt, 1, 1)
self.output_count_txt = QtGui.QLabel()
self.top_right_grid.addWidget(self.output_count_txt, 3,0,1,4)
#Saves the extremely random decision tree model.
self.save_model_desc_txt = QtGui.QLineEdit('Insert Model Description')
self.save_model_desc_txt.setFixedWidth(200)
self.top_right_grid.addWidget(self.save_model_desc_txt, 2, 0, 1, 4)
self.clear_dots_btn = QtGui.QPushButton('Clear All ROI')
self.top_right_grid.addWidget(self.clear_dots_btn, 1, 2)
#Shows the kernel label distributions
self.kernel_show_btn = QtGui.QPushButton('Show Kernel')
self.clear_dots_btn.setEnabled(False)
self.top_right_grid.addWidget(self.kernel_show_btn, 2, 2)
self.top_right_grid.setRowStretch(4,2)
#Sets up the image panel splitter.
image_panel = QtGui.QSplitter(QtCore.Qt.Horizontal)
image_panel.addWidget(self.canvas1)
image_panel.addWidget(self.canvas2)
#Sets up mouse settings on the image
self.canvas1.mpl_connect('axes_enter_event', self.on_enter)
self.canvas1.mpl_connect('axes_leave_event', self.on_leave)
self.bpe =self.canvas1.mpl_connect('button_press_event', self.on_click)
self.bre =self.canvas1.mpl_connect('button_release_event', self.on_unclick)
self.ome =self.canvas1.mpl_connect('motion_notify_event', self.on_motion)
#Splitter which separates the controls at the top and the images below.
splitter = QtGui.QSplitter(QtCore.Qt.Vertical)
hbox1 = QtGui.QWidget()
hbox1.setLayout(top_panel)
splitter.addWidget(hbox1)
splitter.addWidget(image_panel)
box.addWidget(splitter)
#Status bar which is located beneath images.
self.image_status_text = QtGui.QStatusBar()
box.addWidget(self.image_status_text)
self.image_status_text.showMessage('Status: Please Select a Region and Click \'Save ROI\'. ')
#Connects the buttons.
self.save_ROI_btn.clicked.connect(self.save_roi_fn)
self.save_dots_btn.clicked.connect(self.save_dots_fn)
self.prev_im_btn.clicked.connect(self.prev_im_btn_fn)
self.next_im_btn.clicked.connect(self.next_im_btn_fn)
self.sel_ROI_btn.clicked.connect(self.sel_ROI_btn_fn)
self.remove_dots_btn.clicked.connect(self.remove_dots_btn_fn)
self.train_model_btn.clicked.connect(self.train_model_btn_fn)
#self.feat_scale_change_btn.clicked.connect(self.feat_scale_change_btn_fn)
self.kernel_show_btn.clicked.connect(self.kernel_btn_fn)
self.clear_dots_btn.clicked.connect(self.clear_dots_fn)
self.save_model_btn.clicked.connect(self.saveForestFn)
#Initialises the variables for the beginning of the counting.
par_obj.first_time = True
par_obj.dots = []
par_obj.rects = np.zeros((1,4))
par_obj.var =[]
par_obj.saved_dots =[]
par_obj.saved_ROI =[]
par_obj.subdivide_ROI=[]
self.m_Cursor = self.makeCursor()
def report_progress(self,message):
self.image_status_text.showMessage('Status: '+message)
app.processEvents()
def keyPressEvent(self, ev):
"""When the . and , keys are pressed"""
if ev.key() == QtCore.Qt.Key_Period:
self.next_im_btn_fn()
if ev.key() == QtCore.Qt.Key_Comma:
self.prev_im_btn_fn()
def wheelEvent(self, event):
"""When the mousewheel is rotated"""
if event.delta() > 0:
self.next_im_btn_fn()
if event.delta() < 0:
self.prev_im_btn_fn()
def loadTrainFn(self):
#Win_fn()
channel_wid = QtGui.QWidget()
channel_lay = QtGui.QHBoxLayout()
for b in range(0,par_obj.numCH):
name = 'self.CH_cbx'+str(b)+'= checkBoxCH()'
exec(name)
name = 'self.CH_cbx'+str(b)+'.setText(\'CH '+str(b+1)+'\')'
exec(name)
name = 'self.CH_cbx'+str(b)+'.setChecked(True)'
exec(name)
name = 'self.CH_cbx'+str(b)+'.type=\'visual_ch\''
exec(name)
name = 'self.CH_cbx'+str(b)+'.id='+str(b)
exec(name)
name = 'channel_lay.addWidget(self.CH_cbx'+str(b)+')';
exec(name)
channel_lay.addStretch()
channel_wid.setLayout(channel_lay)
win.top_left_grid.addWidget(channel_wid,1,0,1,3)
win_tab.setCurrentWidget(win)
app.processEvents()
self.checkChange()
def on_click(self,event):
"""When the image is clicked"""
par_obj.mouse_down = True
#When the draw ROI functionality is enabled:
if(par_obj.draw_ROI == True):
self.x1 = event.xdata
self.y1 = event.ydata
par_obj.ori_x = event.xdata
par_obj.ori_y = event.ydata
def on_motion(self,event):
"""When the mouse is being dragged"""
#When the draw ROI functionality is enabled:
if(par_obj.draw_ROI == True and par_obj.mouse_down == True):
#Finds current cursor position
par_obj.ori_x_2 = event.xdata
par_obj.ori_y_2 = event.ydata
try:
self.plt1.lines.remove(self.l1[0])
self.plt1.lines.remove(self.l2[0])
self.plt1.lines.remove(self.l3[0])
self.plt1.lines.remove(self.l4[0])
except:
pass
self.plt1.autoscale(False)
self.l1 = self.plt1.plot([self.x1, event.xdata], [self.y1, self.y1], '-' ,color='r')
self.l2 = self.plt1.plot([event.xdata, event.xdata], [self.y1, event.ydata], '-' ,color='r')
self.l3 = self.plt1.plot([event.xdata, self.x1], [ event.ydata, event.ydata], '-' ,color='r')
self.l4 = self.plt1.plot([self.x1, self.x1], [ event.ydata, self.y1], '-' ,color='r')
#self.plt1.Line2D([event.xdata, event.xdata], [self.y1, event.ydata], transform=self.plt1.transData, figure=self.plt1,color='r')
#self.plt1.Line2D([event.xdata, self.x1], [ event.ydata, event.ydata], transform=self.plt1.transData, figure=self.plt1,color='r')
#self.plt1.Line2D([self.x1, self.x1], [ event.ydata, self.y1], transform=self.plt1.transData, figure=self.plt1,color='r')
self.canvas1.draw()
def on_unclick(self, event):
"""When the mouse is released"""
par_obj.mouse_down = False
#If we are in the roi drawing phase
if(par_obj.draw_ROI == True):
t2 = time.time()
x = event.xdata
y = event.ydata
par_obj.rect_w = x - par_obj.ori_x
par_obj.rect_h = y - par_obj.ori_y
#Corrects the corrdinates if out of rectangle.
if(x < 0): x=0
if(y < 0): y=0
if(x > par_obj.width): x=par_obj.width-1
if(y > par_obj.height): y=par_obj.height-1
t1 = time.time()
print t1-t2
#If we are in the dot drawing phase
if(par_obj.draw_dots == True):
x = int(np.round(event.xdata,0))
y = int(np.round(event.ydata,0))
#Are we with an existing box.
if(x > par_obj.rects[1]-par_obj.roi_tolerance and x < (par_obj.rects[1]+ par_obj.rects[3])+par_obj.roi_tolerance and y > par_obj.rects[2]-par_obj.roi_tolerance and y < (par_obj.rects[2]+ par_obj.rects[4])+par_obj.roi_tolerance):
appendDot = True
#Appends dots to array if in an empty pixel.
if(par_obj.dots.__len__()>0):
for i in range(0,par_obj.dots.__len__()):
if (x == par_obj.dots[i][1] and y == par_obj.dots[i][2]):
appendDot = False
if(appendDot == True):
par_obj.dots.append((par_obj.curr_img,x,y))
i = par_obj.dots[-1]
self.plt1.autoscale(False)
self.plt1.plot([i[1]-5,i[1]+5],[i[2],i[2]],'-',color='r')
self.plt1.plot([i[1],i[1]],[i[2]-5,i[2]+5],'-',color='r')
self.canvas1.draw()
if(par_obj.remove_dots == True):
#par_obj.pixMap = QtGui.QPixmap(q2r.rgb2qimage(par_obj.imgs[par_obj.curr_img]))
x = event.xdata
y = event.ydata
self.draw_saved_dots_and_roi()
#Are we with an existing box.
if(x > par_obj.rects[1]-par_obj.roi_tolerance and x < (par_obj.rects[1]+ par_obj.rects[3])+par_obj.roi_tolerance and y > par_obj.rects[2]-par_obj.roi_tolerance and y < (par_obj.rects[2]+ par_obj.rects[4])+par_obj.roi_tolerance):
#Appends dots to array if in an empty pixel.
if(par_obj.dots.__len__()>0):
for i in range(0,par_obj.dots.__len__()):
if ((abs(x -par_obj.dots[i][1])<3 and abs(y - par_obj.dots[i][2])<3)):
par_obj.dots.pop(i)
par_obj.saved_dots.append(par_obj.dots)
par_obj.saved_ROI.append(par_obj.rects)
self.update_density_fn()
par_obj.dots = par_obj.saved_dots[par_obj.ROI_index[par_obj.roi_select]]
par_obj.rects = par_obj.saved_ROI[par_obj.ROI_index[par_obj.roi_select]]
par_obj.saved_dots.pop(par_obj.ROI_index[par_obj.roi_select])
par_obj.saved_ROI.pop(par_obj.ROI_index[par_obj.roi_select])
break
for i in range(0,self.plt1.lines.__len__()):
self.plt1.lines.pop(0)
self.dots_and_square(par_obj.dots,par_obj.rects,'y')
print 'here1'
self.canvas1.draw()
print 'here2'
if(par_obj.select_ROI== True):
x = event.xdata
y = event.ydata
for b in range(0,par_obj.ROI_index.__len__()):
dots = par_obj.saved_dots[par_obj.ROI_index[b]]
rects = par_obj.saved_ROI[par_obj.ROI_index[b]]
if(x > rects[1] and x < (rects[1]+ rects[3]) and y > rects[2] and y < (rects[2]+ rects[4])):
par_obj.roi_select = b
par_obj.dots = par_obj.saved_dots[par_obj.ROI_index[par_obj.roi_select]]
par_obj.rects = par_obj.saved_ROI[par_obj.ROI_index[par_obj.roi_select]]
par_obj.saved_dots.pop(par_obj.ROI_index[par_obj.roi_select])
par_obj.saved_ROI.pop(par_obj.ROI_index[par_obj.roi_select])
for i in range(0,self.plt1.lines.__len__()):
self.plt1.lines.pop(0)
self.draw_saved_dots_and_roi()
self.dots_and_square(dots,rects,'y')
self.canvas1.draw()
self.sel_ROI_btn.setEnabled(False)
self.save_dots_btn.setEnabled(True)
self.remove_dots_btn.setEnabled(True)
par_obj.select_ROI= False
par_obj.draw_ROI = False
par_obj.draw_dots = True
def dots_and_square(self, dots,rects,colour):
#self.l5 = lines.Line2D([rects[1], rects[1]+rects[3]], [rects[2],rects[2]], transform=self.plt1.transData, figure=self.plt1,color=colour)
#self.l6 = lines.Line2D([rects[1]+rects[3], rects[1]+rects[3]], [rects[2],rects[2]+rects[4]], transform=self.plt1.transData, figure=self.plt1,color=colour)
#self.l7 = lines.Line2D([rects[1]+rects[3], rects[1]], [rects[2]+rects[4],rects[2]+rects[4]], transform=self.plt1.transData, figure=self.plt1,color=colour)
#self.l8 = lines.Line2D([rects[1], rects[1]], [rects[2]+rects[4],rects[2]], transform=self.plt1.transData, figure=self.plt1,color=colour)
#self.plt1.lines.extend([self.l5,self.l6,self.l7,self.l8])
self.plt1.autoscale(False)
self.plt1.plot([rects[1], rects[1]+rects[3]], [rects[2],rects[2]], '-',color=colour)
self.plt1.plot([rects[1]+rects[3], rects[1]+rects[3]], [rects[2],rects[2]+rects[4]], '-',color=colour)
self.plt1.plot([rects[1]+rects[3], rects[1]], [rects[2]+rects[4],rects[2]+rects[4]], '-',color=colour)
self.plt1.plot([rects[1], rects[1]], [rects[2]+rects[4],rects[2]], '-',color=colour)
#Draws dots in list
for i in iter(dots):
self.plt1.plot([i[1]-5,i[1]+5],[i[2],i[2]],'-',color=colour)
self.plt1.plot([i[1],i[1]],[i[2]-5,i[2]+5],'-',color=colour)
return
def makeCursor(self):
m_LPixmap = QtGui.QPixmap(28, 28)
bck = QtGui.QColor(168, 34, 3)
bck.setAlpha(0)
m_LPixmap.fill(bck)
qp = QtGui.QPainter(m_LPixmap)
qp.setPen(QtGui.QColor(0, 255, 0,200))
qp.drawLine(14,0,14,28)
qp.drawLine(0,14,28,14)
qp.setOpacity(1.0)
m_Cursor = QtGui.QCursor(m_LPixmap)
qp.setOpacity(0.0)
qp.end()
return m_Cursor
def on_enter(self,ev):
#Changes cursor to the special crosshair on entering image pane.
QtGui.QApplication.setOverrideCursor(self.m_Cursor)
def on_leave(self,ev):
QtGui.QApplication.restoreOverrideCursor()
def save_roi_fn(self):
#If there is no width or height either no roi is selected or it is too thin.
success = v2.save_roi_fn(par_obj)
if success == True:
print ('Saved ROI')
win.image_status_text.showMessage('Status: Select instances in region then click \'save Dots\' ')
par_obj.draw_ROI = False
par_obj.draw_dots = True
win.save_ROI_btn.setEnabled(False)
win.save_dots_btn.setEnabled(True)
win.remove_dots_btn.setEnabled(True)
win.sel_ROI_btn.setEnabled(False)
par_obj.remove_dots = False
def deleteDotsFn(self,sel_ROI_btn_fn):
print('Dot deleted')
par_obj.saved_dots.append(par_obj.dots)
par_obj.saved_ROI.append(par_obj.rects)
par_obj.dots = par_obj.saved_dots[par_obj.ROI_index[par_obj.roi_select]]
par_obj.rects = par_obj.saved_ROI[par_obj.ROI_index[par_obj.roi_select]]
par_obj.saved_dots.pop(par_obj.ROI_index[par_obj.roi_select])
par_obj.saved_ROI.pop(par_obj.ROI_index[par_obj.roi_select])
#Creates the qpainter object
#Now we update a density image of the current Image.
self.update_density_fn()
def save_dots_fn(self):
print('Saved Dots')
win.image_status_text.showMessage('Status: Highlight new ROI or train. ')
win.train_model_btn.setEnabled(True)
par_obj.saved_dots.append(par_obj.dots)
par_obj.saved_ROI.append(par_obj.rects)
self.draw_saved_dots_and_roi()
self.save_ROI_btn.setEnabled(True)
self.save_dots_btn.setEnabled(False)
self.remove_dots_btn.setEnabled(False)
self.sel_ROI_btn.setEnabled(True)
self.clear_dots_btn.setEnabled(True)
par_obj.draw_ROI = True
par_obj.draw_dots = False
par_obj.remove_dots = False
par_obj.dots_past = par_obj.dots
par_obj.dots = []
par_obj.rects = np.zeros((1,4))
par_obj.ori_x=0
par_obj.ori_y=0
par_obj.rect_w=0
par_obj.rect_h =0
self.goto_img_fn(par_obj.curr_img)
#Now we update a density image of the current Image.
self.update_density_fn()
def update_density_fn(self):
#Construct empty array for current image.
par_obj.im_for_train = [par_obj.curr_img]
v2.update_density_fn(par_obj)
self.plt2.cla()
self.plt2.imshow(par_obj.dense_array[par_obj.curr_img])
self.plt2.set_xticklabels([])
self.plt2.set_yticklabels([])
self.canvas2.draw()
def draw_saved_dots_and_roi(self):
for i in range(0,par_obj.subdivide_ROI.__len__()):
if(par_obj.subdivide_ROI[i][0] ==par_obj.curr_img):
rects =par_obj.subdivide_ROI[i]
dots = []
self.dots_and_square(dots,rects,'w')
for i in range(0,par_obj.saved_dots.__len__()):
if(par_obj.saved_ROI[i][0] == par_obj.curr_img):
dots = par_obj.saved_dots[i]
rects = par_obj.saved_ROI[i]
self.dots_and_square(dots,rects,'w')
def prev_im_btn_fn(self):
im_num = par_obj.curr_img - 1
if im_num >-1:
par_obj.curr_img = im_num
self.goto_img_fn(im_num)
def next_im_btn_fn(self):
im_num = par_obj.curr_img + 1
if im_num <par_obj.test_im_end:
par_obj.curr_img = im_num
self.goto_img_fn(im_num)
def goto_img_fn(self,im_num):
#Goto and evaluate image function.
v2.eval_goto_img_fn(im_num,par_obj,self)
self.draw_saved_dots_and_roi()
par_obj.dots = []
par_obj.rects = np.zeros((1,4))
par_obj.select_ROI= False
par_obj.draw_ROI = True
par_obj.draw_dots = False
par_obj.remove_dots = False
self.save_ROI_btn.setEnabled(True)
self.save_dots_btn.setEnabled(False)
self.remove_dots_btn.setEnabled(False)
self.sel_ROI_btn.setEnabled(True)
par_obj.ROI_index=[]
def sel_ROI_btn_fn(self):
par_obj.ROI_index =[]
if(par_obj.select_ROI== False):
self.save_ROI_btn.setEnabled(False)
par_obj.select_ROI= True
par_obj.draw_ROI = False
par_obj.draw_dots = False
par_obj.remove_dots = False
for i in range(0,par_obj.saved_ROI.__len__()):
if(par_obj.saved_ROI[i][0] == par_obj.curr_img):
par_obj.ROI_index.append(i)
for b in range(0,par_obj.ROI_index.__len__()):
dots = par_obj.saved_dots[par_obj.ROI_index[b]]
rects = par_obj.saved_ROI[par_obj.ROI_index[b]]
self.dots_and_square(dots,rects,'y')