From 5a842aa02b290e37dbb4377071c555347c2fbec9 Mon Sep 17 00:00:00 2001 From: Joey Zhao Date: Thu, 15 Jun 2023 05:13:38 +0800 Subject: [PATCH] fix multi-connect signal issues, fix ui alignment issues fix load empty raise error --- geo_sam_tool.py | 50 +++++++++++++++++++++++++++++++++++-------------- ui/Selector.ui | 30 ++++++++++++++--------------- 2 files changed, 51 insertions(+), 29 deletions(-) diff --git a/geo_sam_tool.py b/geo_sam_tool.py index 4ed9fb6..83cd48f 100644 --- a/geo_sam_tool.py +++ b/geo_sam_tool.py @@ -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 @@ -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)) @@ -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 @@ -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) @@ -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 @@ -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) @@ -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(): @@ -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() @@ -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) diff --git a/ui/Selector.ui b/ui/Selector.ui index b394970..753ceb1 100644 --- a/ui/Selector.ui +++ b/ui/Selector.ui @@ -31,7 +31,7 @@ 10 0 - 231 + 241 90 @@ -44,9 +44,9 @@ - 70 + 80 20 - 90 + 85 16 @@ -79,7 +79,7 @@ - 120 + 130 50 100 25 @@ -99,7 +99,7 @@ - 240 + 250 0 121 90 @@ -150,7 +150,7 @@ - 480 + 490 0 120 90 @@ -195,7 +195,7 @@ - 360 + 370 0 121 90 @@ -242,7 +242,7 @@ 10 89 - 231 + 241 80 @@ -308,7 +308,7 @@ - 240 + 250 89 241 80 @@ -376,10 +376,10 @@ - 480 + 490 89 - 121 - 81 + 120 + 80 @@ -405,7 +405,7 @@ - + 0 @@ -416,7 +416,7 @@ - + 0 @@ -427,7 +427,7 @@ - + 0