MCPcopy
hub / github.com/opengeos/segment-geospatial / segment_button_click

Function segment_button_click

samgeo/common.py:2339–2432  ·  view source on GitHub ↗
(change)

Source from the content-addressed store, hash-verified

2337 m.on_interaction(handle_map_interaction)
2338
2339 def segment_button_click(change):
2340 if change["new"]:
2341 segment_button.value = False
2342 with output:
2343 output.clear_output()
2344 if len(m.fg_markers) == 0:
2345 print("Please add some foreground markers.")
2346 segment_button.value = False
2347 return
2348
2349 else:
2350 try:
2351 fg_points = [
2352 [marker.location[1], marker.location[0]]
2353 for marker in m.fg_markers
2354 ]
2355 bg_points = [
2356 [marker.location[1], marker.location[0]]
2357 for marker in m.bg_markers
2358 ]
2359 point_coords = fg_points + bg_points
2360 point_labels = [1] * len(fg_points) + [0] * len(bg_points)
2361
2362 filename = f"masks_{random_string()}.tif"
2363 filename = os.path.join(out_dir, filename)
2364 if sam.model_version == "sam":
2365 sam.predict(
2366 point_coords=point_coords,
2367 point_labels=point_labels,
2368 point_crs="EPSG:4326",
2369 output=filename,
2370 )
2371 elif sam.model_version == "sam2":
2372 sam.predict_by_points(
2373 point_coords_batch=point_coords,
2374 point_labels_batch=point_labels,
2375 point_crs="EPSG:4326",
2376 output=filename,
2377 )
2378 elif sam.model_version == "sam3":
2379 sam.generate_masks_by_points_patch(
2380 point_coords_batch=point_coords,
2381 point_labels_batch=point_labels,
2382 point_crs="EPSG:4326",
2383 min_size=min_size,
2384 max_size=max_size,
2385 )
2386 sam.save_masks(
2387 output=filename, min_size=min_size, max_size=max_size
2388 )
2389 if m.find_layer("Masks") is not None:
2390 m.remove_layer(m.find_layer("Masks"))
2391 if m.find_layer("Regularized") is not None:
2392 m.remove_layer(m.find_layer("Regularized"))
2393
2394 if hasattr(sam, "prediction_fp") and os.path.exists(
2395 sam.prediction_fp
2396 ):

Callers

nothing calls this directly

Calls 9

random_stringFunction · 0.85
predict_by_pointsMethod · 0.80
raster_to_vectorFunction · 0.70
regularizeFunction · 0.70
predictMethod · 0.45
save_masksMethod · 0.45
generate_masksMethod · 0.45

Tested by

no test coverage detected