-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointCloudLabeler.pyw
829 lines (666 loc) · 31.9 KB
/
PointCloudLabeler.pyw
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
import numpy
from opals import Import, Grid, Shade, pyDM
from PyQt5 import QtWidgets,uic, QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QFileDialog, QDialog, QLineEdit, QPushButton, QFormLayout, QCheckBox, QHBoxLayout
import os
import opals
import numpy as np
import copy
from StationUtilities import StationCubicSpline2D
from AxisManagment import AxisManagement
# predefined classication dictionary, mapping class ids to class lables and colors
CLASSIFICATION_DATA = {0: ['0 unclassified', [210, 210, 210]],
1: ['1 undefined', [180, 180, 180]],
2: ['2 ground', [135, 70, 10]],
3: ['3 low vegetation', [185, 230, 120]],
4: ['4 medium vegetation', [145, 200, 0]],
5: ['5 high vegetation', [72, 128, 0]],
6: ['6 building', [180, 20, 20]],
7: ['7 noise', [255, 255, 200]],
8: ['8 model key point', [220, 105, 20]],
9: ['9 water', [0, 95, 255]],
10: ['10 rail', [100, 80, 60]],
11: ['11 road surface', [70, 70, 70]],
12: ['12 bridge deck', [35, 35, 35]],
13: ['13 wire guard', [255, 250, 90]],
14: ['14 wire conductor', [255, 220, 0]],
15: ['15 transmission tower', [235, 200, 60]],
16: ['16 wire connector', [190, 160, 50]],
40: ['40 bathymetric point (e.g. seafloor or riverbed)', [180, 180, 95]],
41: ['41 water surface', [35, 0, 250]],
42: ['42 derived water surface', [40, 220, 240]],
43: ['43 underwater object', [140, 80, 160]],
44: ['44 IHO S-57 object', [90, 75, 170]],
45: ['45 volume backscatter', [60, 130, 130]]}
PREDICTION = {0:'no prediction', 1:'predict next', 2:'predict previous', 3:'always predict'}
class CustomDialog(QDialog):
preview_clicked = QtCore.pyqtSignal()
def __init__(self, parent=None, defaultDistance='0'):
super().__init__(parent)
self.defaultDistance = defaultDistance
self.initUI()
QtCore.QTimer.singleShot(0, self.handle_preview)
self.finished.connect(self._close)
def initUI(self):
self.setWindowTitle('Axis Generation - Parameter Setting')
self.rotation = QLineEdit(self)
self.rotation.setText('0')
self.rotation.setFixedSize(50, 25)
self.rotation_plus_button = QPushButton('+', self)
self.rotation_plus_button.setFixedSize(30, 25)
self.rotation_plus_button.clicked.connect(self.increase_rotation)
self.rotation_plus_button.setFocusPolicy(QtCore.Qt.NoFocus)
self.rotation_minus_button = QPushButton('-', self)
self.rotation_minus_button.setFixedSize(30, 25)
self.rotation_minus_button.clicked.connect(self.decrease_rotation)
self.rotation_minus_button.setFocusPolicy(QtCore.Qt.NoFocus)
rotation_layout = QHBoxLayout()
rotation_layout.addWidget(self.rotation)
rotation_layout.addWidget(self.rotation_plus_button)
rotation_layout.addWidget(self.rotation_minus_button)
self.distance = QLineEdit(self)
self.distance.setText(self.defaultDistance)
self.distance.setFixedSize(50, 25)
self.shpFileExport = QCheckBox("Export to shp-File", self)
self.shpFileExport.setChecked(False)
self.rotation_plus_button.clicked.connect(self.handle_preview)
self.rotation_minus_button.clicked.connect(self.handle_preview)
self.ok_button = QPushButton('OK', self)
self.ok_button.clicked.connect(self.accept)
#self.ok_button.setDefault(True)
self.ok_button.setFocusPolicy(QtCore.Qt.NoFocus)
form_layout = QFormLayout()
form_layout.addRow('Rotation Angle [deg]:', rotation_layout)
form_layout.addRow('Distance between the Axis:', self.distance)
form_layout.addRow(self.shpFileExport)
form_layout.addRow(self.ok_button)
self.setLayout(form_layout)
def rotationButtons(self):
button_layout = QtWidgets.QHBoxLayout()
button_layout.addWidget(self.rotation_minus_button)
button_layout.addWidget(self.rotation_plus_button)
return button_layout
def handle_preview(self):
self.preview_clicked.emit()
def _close(self):
self.parent().closeDialog(self)
def increase_rotation(self):
current_value = float(self.rotation.text() or 0)
self.rotation.setText(str(current_value + 1))
def decrease_rotation(self):
current_value = float(self.rotation.text() or 0)
self.rotation.setText(str(current_value - 1))
def _close(self):
self.parent().closeDialog(self)
def keyReleaseEvent(self, event):
if event.key() == QtCore.Qt.Key_Return:
self.handle_preview()
class ClassificationTool(QtWidgets.QMainWindow):
def __init__(self):
super(ClassificationTool, self).__init__()
uic.loadUi('PointCloudLabeler.ui', self)
#"D:\users\fmeixner\PointCloudLabeler\Data\Fluss_110736_0_loos_528600_533980_Klassifiziert.odm"
self.Overview.setStyleSheet("border: 1px solid black; background-color: rgb(255, 255, 255);")
self.Section.setMouseTracking(True)
self.Overview.setMouseTracking(True)
self.station_axis = None
self.current_station = None
self.min_station = None
self.max_station = None
self.odm = None
self.result = None
self.direction = None
self.layout = None
self.layout2 = None
self.rot_camera = None
self.along = None
self.across = None
self.overlap = None
self.begin = None
self.counter = 0
self.lineend = None
self.forwards = False
self.backwards = False
self.hightcolor = False
self.Point = False
self.FalseAxis = False
self.Rectangle = False
self.firstSection = None
self.meanPtDistance = None
self.ptsLoad = 0
self.ptsClass = 0
self.ptsNoClass = 0
self.knnPts = 0
self.manuallyClassified = '_manuallyClassified'
self.classificationData = CLASSIFICATION_DATA
self.prediction = PREDICTION
self.classHisto = {} # class histogram of the current section
self.axis_manager = None
self.axis_pts = None
self.currrentaxisID = None
self.initUI()
def refeshClassComboBox(self):
#first remove all entries from combo box
currSelection = self.ClassList.currentText()
self.ClassList.clear()
#now fill combo box
for key, val in self.classificationData.items():
pixmap = QPixmap(100,100)
pixmap.fill((QColor(val[1][0],val[1][1],val[1][2])))
icon = QIcon(pixmap)
self.ClassList.addItem(icon,val[0],QColor(val[1][0],val[1][1],val[1][2]))
# restore current selection if necessary
if currSelection != "":
self.ClassList.setCurrentText(currSelection)
def PredictComboBox(self):
currentSelection = self.knnPrediction.currentText()
for key, val in self.prediction.items():
self.knnPrediction.addItem(val)
if currentSelection != "":
self.knnPrediction.setCurrentText(currentSelection)
def initUI(self):
#Build ComboBox:
self.refeshClassComboBox()
self.PredictComboBox()
self.PathToAxisShp.setEnabled(False)
self.LoadButton.pressed.connect(self.load_pointcloud)
self.LoadAxis.pressed.connect(self.clearAxisView)
self.LoadAxis.pressed.connect(self.viewFirstSection)
self.Next.pressed.connect(self.nextSection)
self.Previous.pressed.connect(self.previousSection)
self.HightColor.clicked.connect(self.HightColoring)
self.ClassColor.clicked.connect(self.ClassColoring)
self.OrthoView.clicked.connect(self.setOrthoView)
self.PointSelection.clicked.connect(self.SelectPoint)
self.RectangleSelection.clicked.connect(self.SelectRectangle)
self.ClassList.currentTextChanged.connect(self.PointsClassification)
self.Reset.clicked.connect(self.resetSection)
self.PointSize.valueChanged.connect(self.Section.setPointSize)
self.LineSize.valueChanged.connect(self.Overview.changeLineWidth)
self.StatusMessageModel = QStandardItemModel()
self.StatusMessages.setModel(self.StatusMessageModel)
self.Overview.setAxisList(self.AxisView)
self.Overview.polylinePicked.connect(self.handlePickedPolyline)
self.Save.pressed.connect(self.save_file)
self.Section.setClassifcationData(self.classificationData)
self.DrawMode.clicked.connect(self.DigitalAxis)
self.SelectionMode.clicked.connect(self.DigitalAxis)
self.EditMode.toggled.connect(self.activateEditing)
self.Insert.toggled.connect(self.EditButtonsToggled)
self.Delete.toggled.connect(self.EditButtonsToggled)
self.Move.toggled.connect(self.EditButtonsToggled)
self.Generate.clicked.connect(self.GenerateAxis)
self.ZoomIn.pressed.connect(self.Overview.zoomIn)
self.ZoomOut.pressed.connect(self.Overview.zoomOut)
self.ZoomAll.pressed.connect(self.Overview.zoomOnLayer)
def load_pointcloud(self, path=None):
if path is None:
path, _ = QFileDialog.getOpenFileName(self, "Select point cloud file", "",
"OPALS Datamanager (*.odm);;LAS Files (*.las *laz);;All Files (*.*)")
if path == "":
return
self.PathToFile.setText(os.path.abspath(path))
os.chdir(os.path.dirname(os.path.abspath(path)))
# get the filename
_, data = os.path.split(path)
name, _ = os.path.splitext(data)
try:
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
self.file_name = name
odm_name = name + '.odm'
grid_name = name + '_z.tif'
shd_name = name + '_shd.tif'
axis_odm_name = name + '_axis.odm'
#import into odm if needed
if os.path.isfile(odm_name) == False:
Import.Import(inFile=data, outFile=odm_name).run()
#Extract the header of the odm to get the point density
self.ptsDensity = pyDM.Datamanager.getHeaderODM(odm_name).estimatedPointDensity()
#create shading
if os.path.isfile(grid_name) == False:
Grid.Grid(inFile=odm_name, outFile=grid_name, filter='echo[last]',
interpolation=opals.Types.GridInterpolator.movingPlanes, gridSize=0.5).run()
if os.path.isfile(shd_name) == False:
Shade.Shade(inFile=grid_name, outFile=shd_name).run()
# load the opals datamanager in read and write
self.odm = pyDM.Datamanager.load(odm_name, readOnly=False, threadSafety=False)
except Exception as e:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "Warning",
"Wrong file type! \nPlease choose a file with the right type. ").exec_()
try:
if os.path.isfile(axis_odm_name) == False:
self.axis_manager = AxisManagement(axis_odm_name)
self.Overview.setAxisManagement(self.axis_manager)
self.Overview.AxisODMPath = os.path.abspath(axis_odm_name)
else:
self.axis_manager = AxisManagement(None)
self.Overview.setAxisManagement(self.axis_manager)
self.Overview.setShading(shd_name)
self.Overview.dataRefresh()
self.Overview.SelectAxis = True
self.PathToFile.setEnabled(False)
self.PathToAxisShp.setEnabled(True)
except Exception as e:
return
finally:
QtWidgets.QApplication.restoreOverrideCursor()
def load_axis(self,File=True):
if self.PathToAxisShp.text() == '':
self.PathToAxisShp.setText(self.Overview.AxisODMPath)
pts = None
if File:
axis = str(self.PathToAxisShp.text()).strip()
axis_file, _ = QFileDialog.getOpenFileName(self, "Select axis file", "",
"OPALS Datamanager (*.odm);;Shape File (*.shp);;All Files (*.*)")
if axis_file == "":
return
self.PathToAxisShp.setText(os.path.abspath(axis_file))
_, data = os.path.split(axis_file)
name, _ = os.path.splitext(data)
if _ != '.shp' and _ != '.odm':
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "Warning!",
"Wrong file type! \nFile has to be a shape-file.").exec_()
elif _ == '.shp':
axis_odm_name = name + ".odm"
self.axis_manager = AxisManagement(axis_odm_name, overwrite=True)
self.axis_manager.readShpFile(axis_file)
self.Overview.setAxisManagement(self.axis_manager)
if not self.axis_manager.empty():
pts = self.axis_manager.polyline2linestring(self.axis_manager.axis[0][0])
elif _ == '.odm':
self.axis_manager = AxisManagement(axis_file)
self.Overview.setAxisManagement(self.axis_manager)
if not self.axis_manager.empty():
pts = self.axis_manager.polyline2linestring(self.axis_manager.axis[0][0])
else:
pts = self.axis_pts
if not pts:
self.FalseAxis = True
return
self.FalseAxis = False
self.polygonSize()
# maximal extrapolation distance at start and end of axis
extrapolation_distance = float(self.along_section.text().strip())*5
self.station_axis = StationCubicSpline2D(pts)
self.current_station = 0
self.min_station = self.station_axis.min_station()-extrapolation_distance # min allowed station value
self.max_station = self.station_axis.max_station()+extrapolation_distance # max allowed station value
self.PathToAxisShp.setEnabled(False)
def polygon(self):
def poly_points(start, vector, length, width):
start_point = np.array([start]).reshape(1, 2)
rot_vector = np.array([-vector[1], vector[0]]).reshape(1, 2)
vector = np.array([vector[0], vector[1]]).reshape(1, 2)
p1 = start_point + (rot_vector * length / 2)
p2 = start_point + (-rot_vector * length / 2)
p3 = p2 + (width * vector)
p4 = p1 + (width * vector)
self.rot_camera = rot_vector
return p1, p2, p3, p4
p1, p2, p3, p4 = poly_points(self.begin, self.direction, self.across, self.along)
pf = pyDM.PolygonFactory()
def create_polygon(p1, p2, p3, p4):
pf.addPoint(p1[0, 0], p1[0, 1])
pf.addPoint(p2[0, 0], p2[0, 1])
pf.addPoint(p3[0, 0], p3[0, 1])
pf.addPoint(p4[0, 0], p4[0, 1])
pf.closePart()
return pf.getPolygon()
# create the polygon
polygon = create_polygon(p1, p2, p3, p4)
self.Overview.setSelectionBox(p1, p2, p3, p4)
self.Overview.section_color = 'red'
self.Overview.overlap = self.overlap
self.Overview.drawSection()
# extract the points inside of the polygon
try:
result = pyDM.NumpyConverter.searchPoint(self.odm, polygon, self.layout2, withCoordinates=True, noDataObj=0)
except Exception as e:
# this occurs if no points where found
result = {}
result['Classification'] = np.empty(shape=(0, 0))
result['x'] = np.empty(shape=(0, 0))
result['y'] = np.empty(shape=(0, 0))
result['z'] = np.empty(shape=(0, 0))
self.result = result
self.checkClassification = result['Classification'].copy()
self.ptsLoad = len(self.result['x'])
# build histogram of class ids
classes, counts = numpy.unique(self.result['Classification'], return_counts=True)
self.classHisto = {}
for cl, cn in zip(classes, counts):
self.classHisto[cl] = cn
self.ptsNoClass = 0 if 0 not in self.classHisto else self.classHisto[0]
self.ptsClass = self.ptsLoad - self.ptsNoClass
# add new classes to self.classificationData if needed
classesAdded = False
for classId in self.classHisto:
if classId in self.classificationData:
continue
c = [np.random.random() for i in range(3)]
self.classificationData[classId] = [f'{classId} undefined class_{classId}',[int(c[i]*255) for i in range(3)]]
classesAdded = True
# update class selection combo box if needed
if classesAdded:
self.refeshClassComboBox()
def createPolygon(self):
if not self.odm:
return
if not self.station_axis:
return
self.overlap = (float(self.overlap_section.text().strip()))/100
dm = self.odm
lf = pyDM.AddInfoLayoutFactory()
type, inDM = lf.addColumn(dm, 'Id', True); assert inDM == True
type, inDM = lf.addColumn(dm, 'GPSTime', True); assert inDM == True
type, inDM = lf.addColumn(dm, 'Amplitude', True); assert inDM == True
type, inDM = lf.addColumn(dm, 'Classification', True); assert inDM == True
type, inDM = lf.addColumn(dm,self.manuallyClassified,True,pyDM.ColumnType.bool_) #add atribute for knn
self.layout = lf.getLayout()
lf2 = pyDM.AddInfoLayoutFactory()
type, inDM = lf2.addColumn(dm, 'Id', True); assert inDM == True
type, inDM = lf2.addColumn(dm, 'Classification', True); assert inDM == True
type, inDM = lf2.addColumn(dm, self.manuallyClassified, True, pyDM.ColumnType.uint8)
self.layout2 = lf2.getLayout()
self.begin, self.direction = self.station_axis.get_point_and_direction(self.current_station)
self.polygon()
def polygonSize(self):
if not self.meanPtDistance:
self.meanPtDistance = 1 / np.sqrt(self.ptsDensity)
self.across = 1000 * self.meanPtDistance
if self.across > 30:
self.across = 30.0
else:
self.across = round(1000 * self.meanPtDistance,2)
self.along = round(5 * self.meanPtDistance,2)
self.along_section.setText(str(self.along))
self.across_section.setText(str(self.across))
else:
return
def ptsInSection(self):
self.Section.setData(self.result)
if len(self.result["x"]) > 2:
coords1 = [self.result["x"][0], self.result["y"][0], self.result["z"][0]]
coords2 = [coords1[0] + 10., coords1[1] + 10., coords1[2] + 10.]
self.Section.setStretchAxis(coords1, coords2)
def handlePickedPolyline(self, polyline):
if polyline != []:
self.axis_pts = polyline
self.viewFirstSection(False)
self.axis_pts = []
else:
self.Section._clear()
def clearAxisView(self):
self.AxisView.clear()
def viewFirstSection(self,File=True):
try:
if not self.odm:
return
if self.Overview.AxisView:
self.clearAxisView()
del self.Overview.selection
if not File:
self.load_axis(File=File)
else:
self.load_axis()
if self.FalseAxis:
return
self.createPolygon()
self.ptsInSection()
self.Section.setOrthoView(self.rot_camera)
self.Section.dataRefresh()
self.firstSection = True
self.showMessages()
except Exception as e:
return
def overlapPolygons(self):
# self.disableButtonFunctions()
if not self.station_axis:
return
self.overlap = float(self.overlap_section.text().strip())/100
def activateEditing(self):
self.Insert.setChecked(False)
self.Delete.setChecked(False)
self.Move.setChecked(False)
isChecked = self.EditMode.isChecked()
self.Insert.setEnabled(isChecked)
self.Delete.setEnabled(isChecked)
self.Move.setEnabled(isChecked)
if isChecked:
self.Overview.Draw = False
def EditButtonsToggled(self):
sender = self.sender()
if sender.isChecked():
if sender == self.Insert:
self.Overview.insert = True
self.Overview.delete = False
self.Overview.move = False
self.Delete.setChecked(False)
self.Move.setChecked(False)
elif sender == self.Delete:
self.Overview.delete = True
self.Overview.insert = False
self.Overview.move = False
self.Insert.setChecked(False)
self.Move.setChecked(False)
elif sender == self.Move:
self.Overview.move = True
self.Overview.insert = False
self.Overview.delete = False
self.Insert.setChecked(False)
self.Delete.setChecked(False)
def GenerateAxis(self):
try:
self.polygonSize()
defaultDistance = str(self.across)
defaultWidth = str(self.along)
dialog = CustomDialog(self,defaultDistance=defaultDistance)
dialog.preview_clicked.connect(lambda: self.handlePreview(dialog))
dialog.finished.connect(lambda: self.closeDialog(dialog))
if dialog.exec_() == QDialog.Accepted:
arial_axis_odm_name = self.file_name + '_arial_axis.odm'
rotation = dialog.rotation.text()
distance = dialog.distance.text()
self.Overview.ArialCoverage(distance=distance, width=self.along, rotation=rotation, preview=False, export=dialog.shpFileExport.isChecked(), filename=arial_axis_odm_name)
if not self.axis_manager.empty():
self.axis_pts = self.axis_manager.polyline2linestring(self.axis_manager.axis[0][0])
self.across = float(dialog.distance.text())
self.viewFirstSection(File=False)
self.across_section.setText(str(self.across))
self.PathToAxisShp.setText(os.path.abspath(arial_axis_odm_name))
self.PathToAxisShp.setEnabled(False)
except Exception as e:
return
def handlePreview(self, dialog):
rotation = dialog.rotation.text()
distance = dialog.distance.text()
self.Overview.clear()
self.Overview.ArialCoverage(distance=distance, width=self.along, rotation=rotation, preview=True)
def closeDialog(self, dialog):
self.Overview.clear()
def changePolygonSize(self):
if not self.station_axis:
return
self.along = float(self.along_section.text().strip())
self.across = float(self.across_section.text().strip())
self.polygon()
self.ptsInSection()
self.Section.dataRefresh()
self.Overview.dataRefresh()
self.showMessages()
def setOrthoView(self):
try:
self.Section.setOrthoView(self.rot_camera)
except Exception as e:
return
def DigitalAxis(self):
if self.DrawMode.isChecked():
self.Overview.insert = False
self.Overview.delete = False
self.Overview.move = False
self.Overview.Draw = True
self.Overview.SelectAxis = False
self.SelectionMode.setChecked(False)
elif self.SelectionMode.isChecked():
self.Overview.insert = False
self.Overview.delete = False
self.Overview.move = False
self.Overview.Draw = False
self.Overview.SelectAxis = True
self.DrawMode.setChecked(False)
def changeAttributes(self):
if np.array_equal(self.result['Classification'],self.checkClassification) == False:
self.setObj = {}
self.setObj['Id'] = self.result['Id']
self.setObj['Classification'] = self.result['Classification']
self.setObj[self.manuallyClassified] = self.result[self.manuallyClassified]
pyDM.NumpyConverter.setById(self.setObj, self.odm, self.layout2)
self.odm.save()
def knn(self):
# create 3d kdtree for nearest neighbour selection
kdtree = pyDM.PointIndexLeaf(pyDM.IndexType.kdtree, 3, True)
# settings for nn selection
nnCount = 1
searchMode = pyDM.SelectionMode.nearest
maxSearchDist = -1
for idx in range(len(self.knnSection['x'])):
classid = self.knnSection['Classification'][idx]
pt = pyDM.Point(self.knnSection['x'][idx],self.knnSection['y'][idx],self.knnSection['z'][idx])
pt.setAddInfoView(self.layout2, False)
pt.info().set(1,int(classid))
kdtree.addPoint(pt)
assigned_pts = 0
for idx in range(len(self.result['x'])):
if self.result['Classification'][idx] == 0:
searchPt = pyDM.Point(self.result['x'][idx],self.result['y'][idx],self.result['z'][idx])
pts = kdtree.searchPoint(nnCount,searchPt,maxSearchDist,searchMode)
if pts != []:
classid = pts[0].info().get(1)
self.result['Classification'][idx] = classid
self.result[self.manuallyClassified][idx] = 2
assigned_pts += 1
self.knnPts = assigned_pts
self.ptsNoClass = sum(self.result['Classification'] > 0)
self.ptsNoClass = sum(self.result['Classification'] == 0)
# update histogram
classes, counts = numpy.unique(self.result['Classification'], return_counts=True)
self.classHisto = {}
for cl, cn in zip(classes, counts):
self.classHisto[cl] = cn
def nextSection(self):
if not self.station_axis:
return
ds = self.along * (1 - self.overlap)
new_station = self.current_station + ds
if new_station + ds > self.max_station:
new_station = self.max_station-ds
if new_station == self.current_station:
return
self.changeAttributes()
self.knnSection = copy.deepcopy(self.result)
self.current_station = new_station
self.begin, self.direction = self.station_axis.get_point_and_direction(self.current_station)
self.polygon()
if self.knnPrediction.currentText() == 'predict next' or self.knnPrediction.currentText() == 'always predict':
self.knn()
self.ptsInSection()
self.Section.dataRefresh()
self.Overview.dataRefresh()
self.showMessages()
def previousSection(self):
if not self.station_axis:
return
ds = self.along * (1 - self.overlap)
new_station = self.current_station - ds
if new_station < self.min_station:
new_station = self.min_station
if new_station == self.current_station:
return
self.changeAttributes()
self.knnSection = copy.deepcopy(self.result)
self.current_station = new_station
self.begin, self.direction = self.station_axis.get_point_and_direction(self.current_station)
self.polygon()
if self.knnPrediction.currentText() == 'predict previous' or self.knnPrediction.currentText() == 'always predict':
self.knn()
self.ptsInSection()
self.Section.dataRefresh()
self.Overview.dataRefresh()
self.showMessages()
def PointsClassification(self):
for key, value in self.classificationData.items():
if value[0] == str(self.ClassList.currentText()):
self.Section.currentClass = key
def SelectPoint(self):
if not self.station_axis:
return
if self.RectangleSelection.isChecked():
self.RectangleSelection.setChecked(False)
self.Section.SelectRectangle = False
if self.PointSelection.isChecked():
self.PointsClassification()
self.Section.SelectPoint = True
elif self.PointSelection.isChecked() == False:
self.Section.SelectPoint = False
def SelectRectangle(self):
if not self.station_axis:
return
if self.PointSelection.isChecked():
self.PointSelection.setChecked(False)
self.Section.SelectPoint = False
if self.RectangleSelection.isChecked():
self.Section.SelectRectangle = True
elif self.RectangleSelection.isChecked() == False:
self.Section.SelectRectangle = False
def ClassColoring(self):
try:
if self.firstSection:
if self.HightColor.isChecked():
self.HightColor.setChecked(False)
if self.ClassColor.isChecked():
self.Section.currentColor = 1
self.Section.dataRefresh()
except Exception as e:
return
def HightColoring(self):
try:
if self.ClassColor.isChecked():
self.ClassColor.setChecked(False)
if self.HightColor.isChecked():
self.Section.currentColor = 2
self.Section.dataRefresh()
except Exception as e:
return
def showMessages(self):
self.StatusMessageModel.clear()
self.StatusMessageModel.appendRow(QStandardItem(r'Current station: {:.2f} - {:.2f}'.format(self.current_station,
self.current_station + self.along)))
self.StatusMessageModel.appendRow(QStandardItem(r'Loaded: {} Points'.format(self.ptsLoad)))
self.StatusMessageModel.appendRow(QStandardItem(r'Classified: {} Points'.format(self.ptsClass)))
self.StatusMessageModel.appendRow(QStandardItem(r'Unclassified: {} Points'.format(self.ptsNoClass)))
self.StatusMessageModel.appendRow(QStandardItem(r'Class histogram: {}'.format(self.classHisto)))
if self.knnPrediction.currentText() == ('predict previous' or 'always predict' or 'predict next'):
self.StatusMessageModel.appendRow(QStandardItem(r'Class predicted: {} Points'.format(self.knnPts)))
def resetSection(self):
if not self.station_axis:
return
self.Section.Reset()
def save_file(self):
if not self.station_axis:
return
self.changeAttributes()
self.Section.deleteReset()
self.PathToFile.setEnabled(True)
self.PathToAxisShp.setEnabled(True)
def keyReleaseEvent(self, event):
if event.key() == QtCore.Qt.Key_Return or event.key() == QtCore.Qt.Key_Tab:
self.changePolygonSize()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = ClassificationTool()
if len(sys.argv) > 1:
win.load_pointcloud(sys.argv[1])
win.show()
sys.exit(app.exec_())