Skip to content

Commit

Permalink
fix multi-connect signal issues,
Browse files Browse the repository at this point in the history
fix ui alignment issues
fix load empty raise error
  • Loading branch information
coolzhao committed Jun 14, 2023
1 parent c1b4ea2 commit 5a842aa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
50 changes: 36 additions & 14 deletions geo_sam_tool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from qgis.core import QgsProject, QgsVectorLayer, QgsFeature, QgsGeometry
from qgis.gui import QgsMapToolEmitPoint, QgsRubberBand, QgsMapTool, QgsMapToolPan
from qgis.core import (
QgsPointXY, QgsWkbTypes,QgsMarkerSymbol, QgsRectangle, QgsField, QgsFillSymbol,
QgsPointXY, QgsWkbTypes,QgsMarkerSymbol, QgsRectangle, QgsField, QgsFillSymbol, QgsMessageLog, Qgis,
QgsGeometry, QgsFeature, QgsVectorLayer, QgsRasterLayer, QgsSimpleMarkerSymbolLayer, QgsSingleSymbolRenderer)
from qgis.PyQt.QtCore import QCoreApplication,QVariant
from PyQt5.QtCore import Qt, pyqtSignal, QEvent, QObject, QTimer
Expand Down Expand Up @@ -542,7 +542,7 @@ def sam_predict(self, canvas_points, canvas_rect, sam_polygon):

input_point, input_label = canvas_points.get_points_and_labels(img_clip_transform)
box = canvas_rect.get_img_box(img_clip_transform)
print("box", box)
# print("box", box)

img_features = batch['image']
self.predictor.set_image_feature(img_features, img_shape=(1024, 1024))
Expand All @@ -553,7 +553,8 @@ def sam_predict(self, canvas_points, canvas_rect, sam_polygon):
box=box,
multimask_output=False,
)
print(masks.shape) # (number_of_masks) x H x W
QgsMessageLog.logMessage("SAM predict executed", 'Geo SAM', level=Qgis.Info)
# print(masks.shape) # (number_of_masks) x H x W

mask = masks[0, ...]
# mask = mask_morph
Expand Down Expand Up @@ -639,8 +640,6 @@ def _init_feature_related(self):
# self.shortcut_undo.setContext(Qt.ApplicationShortcut)
# self.shortcut_undo.activated.connect(self.execute_segmentation)

self.execute_SAM.connect(self.execute_segmentation)
self.activate_fg.connect(self.draw_foreground_point)

# init tools
self.tool_click_fg = ClickTool(self.canvas, self.canvas_points.feature_fg, self.canvas_points.layer_fg, self.execute_SAM)
Expand All @@ -650,6 +649,11 @@ def _init_feature_related(self):
def create_widget_selector(self):
self._init_feature_related()
self.load_demo_img()

receiversCount = self.receivers(self.execute_SAM)
if receiversCount == 0:
self.execute_SAM.connect(self.execute_segmentation)
self.activate_fg.connect(self.draw_foreground_point)

UI_Selector = uic.loadUi(os.path.join(self.cwd, "ui/Selector.ui"))
# connect signals for buttons
Expand All @@ -669,6 +673,9 @@ def create_widget_selector(self):
self.wdg_sel.pushButton_fg.setCheckable(True)
self.wdg_sel.pushButton_bg.setCheckable(True)
self.wdg_sel.pushButton_rect.setCheckable(True)
# self.wdg_sel.closed.connect(self.destruct()) # qgis.gui.QgsDockWidget, actually QDockWidget no closed signal
# self.wdg_sel.closeEvent = self.destruct
# self.wdg_sel.visibilityChanged.connect(self.destruct)

# add widget to QGIS
self.wdg_sel.setFloating(True)
Expand All @@ -677,6 +684,15 @@ def create_widget_selector(self):
# start with fg
self.draw_foreground_point()

def destruct(self):
self.save_shp_file()
receiversCount = self.receivers(self.execute_SAM)
if receiversCount > 0:
self.execute_SAM.disconnect()
self.activate_fg.disconnect()
self.canvas.setMapTool(self.toolPan)
# self.wdg_sel.destroy()

def enable_disable(self):
radioButton = self.sender()
if not radioButton.isChecked():
Expand Down Expand Up @@ -780,9 +796,13 @@ def find_feature(self):

def load_feature(self):
self.feature_dir = self.wdg_sel.path_feature.text()
self._init_feature_related()
self.load_shp_file()
self.draw_foreground_point()
if self.feature_dir is not None and os.path.exists(self.feature_dir):
self.clear_layers()
self._init_feature_related()
self.load_shp_file()
self.draw_foreground_point()
else:
self.iface.messageBar().pushMessage("Feature folder not exist", "choose a another folder", level=Qgis.Info)

def clear_layers(self):
self.canvas_points._reset_points_layer()
Expand All @@ -793,16 +813,18 @@ def clear_layers(self):
self.reset_label_tool()

def save_shp_file(self):
self.polygon.commit_changes()
if hasattr(self, "polygon"):
self.polygon.commit_changes()
self.clear_layers()
# self.activate_fg.emit()

def reset_label_tool(self):
if self.prompt_type == 'bbox':
self.draw_rect()
else:
self.draw_foreground_point()
self.prompt_type = 'fgpt'
if hasattr(self, "prompt_type"):
if self.prompt_type == 'bbox':
self.draw_rect()
else:
self.draw_foreground_point()
self.prompt_type = 'fgpt'


# gs = Geo_SAM(iface, cwd)
Expand Down
30 changes: 15 additions & 15 deletions ui/Selector.ui
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<rect>
<x>10</x>
<y>0</y>
<width>231</width>
<width>241</width>
<height>90</height>
</rect>
</property>
Expand All @@ -44,9 +44,9 @@
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>70</x>
<x>80</x>
<y>20</y>
<width>90</width>
<width>85</width>
<height>16</height>
</rect>
</property>
Expand Down Expand Up @@ -79,7 +79,7 @@
<widget class="QPushButton" name="pushButton_bg">
<property name="geometry">
<rect>
<x>120</x>
<x>130</x>
<y>50</y>
<width>100</width>
<height>25</height>
Expand All @@ -99,7 +99,7 @@
<widget class="QFrame" name="frame_2">
<property name="geometry">
<rect>
<x>240</x>
<x>250</x>
<y>0</y>
<width>121</width>
<height>90</height>
Expand Down Expand Up @@ -150,7 +150,7 @@
<widget class="QFrame" name="frame_4">
<property name="geometry">
<rect>
<x>480</x>
<x>490</x>
<y>0</y>
<width>120</width>
<height>90</height>
Expand Down Expand Up @@ -195,7 +195,7 @@
<widget class="QFrame" name="frame_5">
<property name="geometry">
<rect>
<x>360</x>
<x>370</x>
<y>0</y>
<width>121</width>
<height>90</height>
Expand Down Expand Up @@ -242,7 +242,7 @@
<rect>
<x>10</x>
<y>89</y>
<width>231</width>
<width>241</width>
<height>80</height>
</rect>
</property>
Expand Down Expand Up @@ -308,7 +308,7 @@
<widget class="QFrame" name="frame_6">
<property name="geometry">
<rect>
<x>240</x>
<x>250</x>
<y>89</y>
<width>241</width>
<height>80</height>
Expand Down Expand Up @@ -376,10 +376,10 @@
<widget class="QFrame" name="frame_7">
<property name="geometry">
<rect>
<x>480</x>
<x>490</x>
<y>89</y>
<width>121</width>
<height>81</height>
<width>120</width>
<height>80</height>
</rect>
</property>
<property name="frameShape">
Expand All @@ -405,7 +405,7 @@
</property>
</widget>
</widget>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>0</x>
Expand All @@ -416,7 +416,7 @@
</property>
<layout class="QHBoxLayout" name="horizontalLayout"/>
</widget>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>0</x>
Expand All @@ -427,7 +427,7 @@
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2"/>
</widget>
<widget class="QWidget" name="">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>0</x>
Expand Down

0 comments on commit 5a842aa

Please sign in to comment.