今天尝试了segment-geospatial的Segmenting remote sensing imagery with box prompts和Segmenting remote sensing imagery with point prompts案例。prompts我理解就是提示信息,可以是点、面和文本提示信息等等,告诉重点关注某些位置,有点像注意力集中机制。具体项目地址如下:
Sam2 box prompts - segment-geospatialhttps://samgeo.gishub.org/examples/sam2_box_prompts/
Sam2 point prompts - segment-geospatialhttps://samgeo.gishub.org/examples/sam2_point_prompts/第一个案例,用box prompts顺利跑出结果,代码如下:
import leafmap
from samgeo import SamGeo2, regularize
#
m = leafmap.Map(center=[47.653287, -117.588070], zoom=16, height="800px")
m.add_basemap("Satellite")
m
#
if m.user_roi is not None:
bbox = m.user_roi_bounds()
else:
bbox = [-117.6029, 47.65, -117.5936, 47.6563]
#
image = "satellite.tif"
leafmap.map_tiles_to_geotiff(
output=image, bbox=bbox, zoom=18, source="Satellite", overwrite=True
)
#
m.layers[-1].visible = False
m.add_raster(image, layer_name="Image")
m
#
sam = SamGeo2(
model_id="sam2-hiera-large",
automatic=False,
)
#
sam.set_image(image)
#
if m.user_rois is not None:
boxes = m.user_rois
else:
boxes = [
[-117.5995, 47.6518, -117.5988, 47.652],
[-117.5987, 47.6518, -117.5979, 47.652],
]
#
sam.predict(boxes=boxes, point_crs="EPSG:4326", output="mask.tif", dtype="uint8")
(图中红框是截图时加大,程序输出的一个Tiff图层,两个白色图块是SAM模型识别的)
第二个案例,用point prompts,没有顺利跑出结果,代码如下:
import leafmap
from samgeo import SamGeo2, regularize
#
m = leafmap.Map(center=[47.653287, -117.588070], zoom=16, height="800px")
m.add_basemap("Satellite")
m
#
if m.user_roi is not None:
bbox = m.user_roi_bounds()
else:
bbox = [-117.6029, 47.65, -117.5936, 47.6563]
#
image = "satellite.tif"
leafmap.map_tiles_to_geotiff(
output=image, bbox=bbox, zoom=18, source="Satellite", overwrite=True
)
#
m.layers[-1].visible = False
m.add_raster(image, layer_name="Image")
m
#
sam = SamGeo2(
model_id="sam2-hiera-large",
automatic=False,
)
#
sam.set_image(image)
#
if m.user_rois is not None:
point_coords_batch = m.user_rois
else:
point_coords_batch = [
[-117.599896, 47.655345],
[-117.59992, 47.655167],
[-117.599928, 47.654974],
[-117.599518, 47.655337],
]
#
sam.predict_by_points(
point_coords_batch=point_coords_batch,
point_crs="EPSG:4326",
output="mask.tif",
dtype="uint8",
)
报错
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
查了半天也没有调好,后续在查查看看能不能调好。