{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\"Open\n", "\n", "**Earth Engine Image Segmentation with the Segment Anything Model**\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "# !pip install -U geemap" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# %pip install segment-geospatial pycrs" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "from samgeo import SamGeo" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "point = ee.Geometry.Point(-122.259679, 37.871838)\n", "collection = (\n", " ee.ImageCollection(\"USDA/NAIP/DOQQ\")\n", " .filterBounds(point)\n", " .filterDate(\"2008-01-01\", \"2018-01-01\")\n", " .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n", ")\n", "image = collection.first()\n", "Map.addLayer(image, {}, \"NAIP\")\n", "Map.centerObject(point, 16)\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "bbox = Map.user_roi_coords()\n", "if bbox is None:\n", " bbox = [-122.2666, 37.8682, -122.252, 37.8752]" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "geemap.ee_to_geotiff(\n", " image, \"naip.tif\", bbox, zoom=17, vis_params={\"bands\": [\"R\", \"G\", \"B\"]}\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "sam = SamGeo(\n", " model_type=\"vit_h\",\n", " checkpoint=\"sam_vit_h_4b8939.pth\",\n", " device=None,\n", " sam_kwargs=None,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "sam.generate(\"naip.tif\", output=\"masks.tif\", foreground=True, unique=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "sam.show_masks(cmap=\"binary_r\")" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "sam.show_anns(axis=\"off\", alpha=1, output=\"annotations.tif\")" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "Map.add_raster(\"annotations.tif\", opacity=0.5, layer_name=\"Masks\")\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "sam.tiff_to_vector(\"masks.tif\", \"masks.shp\")" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "style = {\n", " \"color\": \"#3388ff\",\n", " \"weight\": 2,\n", " \"fillColor\": \"#7c4185\",\n", " \"fillOpacity\": 0.5,\n", "}\n", "Map.add_vector(\"masks.shp\", layer_name=\"Vector\", style=style)\n", "Map" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }