Skip to content

Commit

Permalink
zoom to image, add extent attributes of sam_model
Browse files Browse the repository at this point in the history
  • Loading branch information
coolzhao committed Jun 14, 2023
1 parent a6cd459 commit 58c55eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Geo-SAM
# Geo SAM

By Joey and [Fancy](https://github.com/Fanchengyan) from [CryoLab](https://cryocuhk.github.io/), ESSC, CUHK.

Expand Down Expand Up @@ -152,4 +152,4 @@ After selecting the feature folder, you may press the `Load` button to load the

## Acknowledgement

This repo benefits from Segment Anything and TorchGeo. Thanks for their wonderful works.
This repo benefits from [Segment Anything](https://github.com/facebookresearch/segment-anything) and [TorchGeo](https://github.com/microsoft/torchgeo). Thanks for their wonderful works.
17 changes: 14 additions & 3 deletions geo_sam_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ def transform_extent_to_feature_crs(self, extent: QgsRectangle, point_crs: QgsCo
extent_transformed = transform.transformBoundingBox(extent)
return extent_transformed

def transform_extent_from_feature_crs(self, extent: QgsRectangle, point_crs: QgsCoordinateReferenceSystem):
'''transform extent from feature crs to point crs'''
# point_crs = QgsCoordinateReferenceSystem(point_crs)
transform = QgsCoordinateTransform(self.feature_crs, point_crs, QgsProject.instance())
extent_transformed = transform.transformBoundingBox(extent)
return extent_transformed


class LayerExtent:
def __init__(self):
Expand Down Expand Up @@ -497,6 +504,8 @@ def _prepare_data_and_layer(self):
# Load sam decoder
sam = sam_model_registry_no_encoder[self.model_type](checkpoint=self.sam_checkpoint)
self.predictor = SamPredictorNoImgEncoder(sam)
feature_bounds = self.test_features.index.bounds # list [minx, maxx, miny, maxy, mint, maxt]
self.extent = QgsRectangle(feature_bounds[0], feature_bounds[2], feature_bounds[1], feature_bounds[3])

def sam_predict(self, canvas_points, canvas_rect, sam_polygon):
min_x, max_x, min_y, max_y = LayerExtent.union_extent(canvas_points.extent, canvas_rect.extent)
Expand Down Expand Up @@ -601,9 +610,6 @@ def load_demo_img(self):
rlayer = QgsRasterLayer(img_path, self.demo_img_name)
if rlayer.isValid():
QgsProject.instance().addMapLayer(rlayer)
canvas = self.iface.mapCanvas()
canvas.setExtent(rlayer.extent())
canvas.refresh()
else:
print("Demo image layer failed to load!")
# self.iface.addRasterLayer(img_path, self.demo_img_name)
Expand All @@ -620,6 +626,11 @@ def _init_feature_related(self):
self.canvas_points = Canvas_Points(self.canvas, self.transform_crs)
self.canvas_rect = Canvas_Rectangle(self.canvas, self.transform_crs)

canvas = self.iface.mapCanvas()
extent_canvas = self.transform_crs.transform_extent_from_feature_crs(self.sam_model.extent, QgsProject.instance().crs())
canvas.setExtent(extent_canvas)
canvas.refresh()

self.canvas_points.init_points_layer()
self.canvas_rect._init_rect_layer()

Expand Down

0 comments on commit 58c55eb

Please sign in to comment.