Building segmentation with DINO + SAM
INSTALL THE PACKAGE IF NEEDED
In [1]:
Copied!
# %pip install git+https://github.com/VictorPachecoAznar/TFG
# %pip install git+https://github.com/VictorPachecoAznar/TFG
SET UP SOME ENVIRONMENT VARIABLES WITHIN THE ELEMENTS
In [2]:
Copied!
from apb_spatial_computer_vision import BASE_DIR,DATA_DIR,OUT_DIR
from apb_spatial_computer_vision import BASE_DIR,DATA_DIR,OUT_DIR
In [3]:
Copied!
import os
os.environ['BASE_DIR'] = BASE_DIR
os.environ['DATA_DIR'] = DATA_DIR
os.environ['TEXT_PROMPT']='building'
os.environ['VECTOR_FILE']= os.path.join(OUT_DIR,'building_jupyter.geojson')
os.environ['NAME_ORTOFOTO']='ORTO_ZAL_BCN.tif'
import os
os.environ['BASE_DIR'] = BASE_DIR
os.environ['DATA_DIR'] = DATA_DIR
os.environ['TEXT_PROMPT']='building'
os.environ['VECTOR_FILE']= os.path.join(OUT_DIR,'building_jupyter.geojson')
os.environ['NAME_ORTOFOTO']='ORTO_ZAL_BCN.tif'
SET UP THE STUDY AREA
In [4]:
Copied!
import leafmap
map=leafmap.Map(center=[41.316,2.14],zoom=15,basemap='Esri.WorldImagery')
map
import leafmap
map=leafmap.Map(center=[41.316,2.14],zoom=15,basemap='Esri.WorldImagery')
map
Out[4]:
Map(center=[41.316, 2.14], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_o…
LOAD THE LIBRARIES
In [5]:
Copied!
from apb_spatial_computer_vision.main import pyramid_sam_apply,text_to_bbox_lowres_complete
from apb_spatial_computer_vision.raster_utilities import Ortophoto
from apb_spatial_computer_vision.sam_utilities import SamGeo_apb
from apb_spatial_computer_vision.lang_sam_utilities import LangSAM_apb
from apb_spatial_computer_vision.main import pyramid_sam_apply,text_to_bbox_lowres_complete
from apb_spatial_computer_vision.raster_utilities import Ortophoto
from apb_spatial_computer_vision.sam_utilities import SamGeo_apb
from apb_spatial_computer_vision.lang_sam_utilities import LangSAM_apb
In [6]:
Copied!
sam = SamGeo_apb(
model_type="vit_h",
automatic=False,
sam_kwargs=None,
)
lang_sam=LangSAM_apb()
sam = SamGeo_apb(
model_type="vit_h",
automatic=False,
sam_kwargs=None,
)
lang_sam=LangSAM_apb()
final text_encoder_type: bert-base-uncased
GET THE NEEDED ENVIRONMENT VARIABLES TO RUN THE CODE
In [7]:
Copied!
test_folder = os.getenv('DATA_DIR')
path_orto = os.path.join(test_folder, os.getenv('NAME_ORTOFOTO','default_orto.tif'))
input_image = Ortophoto(path_orto)
vector_file=os.getenv('VECTOR_FILE',None)
if vector_file is not None:
if os.path.exists(vector_file):
vector_file=vector_file
else:
vector_file=os.path.join(input_image.folder, vector_file)
else:
vector_file=vector_file
segmentation_name= os.getenv('TEXT_PROMPT','')
test_folder = os.getenv('DATA_DIR')
path_orto = os.path.join(test_folder, os.getenv('NAME_ORTOFOTO','default_orto.tif'))
input_image = Ortophoto(path_orto)
vector_file=os.getenv('VECTOR_FILE',None)
if vector_file is not None:
if os.path.exists(vector_file):
vector_file=vector_file
else:
vector_file=os.path.join(input_image.folder, vector_file)
else:
vector_file=vector_file
segmentation_name= os.getenv('TEXT_PROMPT','')
UNCOMMENT IF PYRAMID IS AVAILABLE
In [8]:
Copied!
input_image.pyramid=(os.path.join(input_image.folder,os.path.basename(input_image.raster_path).split('.')[0])+'_pyramid')
input_image.resolutions=os.path.join(input_image.folder,os.path.splitext(input_image.basename)[0]+'_resolutions')
input_image.pyramid=(os.path.join(input_image.folder,os.path.basename(input_image.raster_path).split('.')[0])+'_pyramid')
input_image.resolutions=os.path.join(input_image.folder,os.path.splitext(input_image.basename)[0]+'_resolutions')
RUN GROUNDING DINO TO GET THE BOUNDING BOXES AND THE PATH TO THE IMAGES THEY HAVE BEEN FOUND AT
In [9]:
Copied!
from apb_spatial_computer_vision.main import duckdb_2_gdf
duckdb_2_gdf(text_to_bbox_lowres_complete(input_image=input_image,
text_prompt=segmentation_name,
output=vector_file,
sam=lang_sam),'geom')
from apb_spatial_computer_vision.main import duckdb_2_gdf
duckdb_2_gdf(text_to_bbox_lowres_complete(input_image=input_image,
text_prompt=segmentation_name,
output=vector_file,
sam=lang_sam),'geom')
d:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\out\building_jupyter.geojson
Out[9]:
NAME | geometry | |
---|---|---|
0 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428638.042 4574817.455, 428638.042 4... |
1 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427214.558 4574956.192, 427214.558 4... |
2 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428638.397 4574826.907, 428638.397 4... |
3 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427973.197 4574881.399, 427973.197 4... |
4 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427866.367 4575027.679, 427866.367 4... |
5 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427469.993 4575142.903, 427469.993 4... |
6 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428061.974 4574919.016, 428061.974 4... |
7 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428638.342 4575179.429, 428638.342 4... |
8 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428167.549 4574869.303, 428167.549 4... |
9 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428575.326 4575059.784, 428575.326 4... |
10 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428278.614 4575069.406, 428278.614 4... |
11 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428376.824 4575030.034, 428376.824 4... |
12 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428261.809 4574855.56, 428261.809 45... |
13 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427931.001 4575301.758, 427931.001 4... |
14 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428151.57 4575258.505, 428151.57 457... |
15 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427764.539 4574956.828, 427764.539 4... |
16 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428220.827 4575384.81, 428220.827 45... |
17 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427386.567 4574944.69, 427386.567 45... |
18 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427579.714 4574943.428, 427579.714 4... |
19 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427080.694 4574972.862, 427080.694 4... |
20 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428120.161 4575132.936, 428120.161 4... |
21 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((428568.699 4575253.958, 428568.699 4... |
22 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427545.272 4575279.46, 427545.272 45... |
23 | D:\VICTOR_PACHECO\CUARTO\PROCESADO_IMAGEN\data... | POLYGON ((427725.891 4574947.451, 427725.891 4... |
RUNNING SAM SEGMENTATION
In [10]:
Copied!
pyramid_sam_apply(input_image=input_image,
detections=vector_file,
geometry_column='geom',
segmentation_name=segmentation_name,
sam=sam,
#min_expected_element_area=0.5,
#lowest_pixel_size=1024,
)
pyramid_sam_apply(input_image=input_image,
detections=vector_file,
geometry_column='geom',
segmentation_name=segmentation_name,
sam=sam,
#min_expected_element_area=0.5,
#lowest_pixel_size=1024,
)
Some coordinates are out of the image boundary. Some coordinates are out of the image boundary. Some coordinates are out of the image boundary.
FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))
FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))
VISUALIZE OUR RESULTS
In [ ]:
Copied!
import geopandas as gpd
result_dino=gpd.read_file(vector_file)
result_sam=gpd.read_file(os.path.join(input_image.folder,f'sam_results_{segmentation_name}',f'first_iteration_{segmentation_name}.geojson'))
map.add_gdf(result_dino,layer_name='DINO RESULT',style={'fillColor': 'black','color': 'black','weight': 1,'fillOpacity': 0.2})
map.add_gdf(result_sam)
map.to_html('assets/building.html')
import geopandas as gpd
result_dino=gpd.read_file(vector_file)
result_sam=gpd.read_file(os.path.join(input_image.folder,f'sam_results_{segmentation_name}',f'first_iteration_{segmentation_name}.geojson'))
map.add_gdf(result_dino,layer_name='DINO RESULT',style={'fillColor': 'black','color': 'black','weight': 1,'fillOpacity': 0.2})
map.add_gdf(result_sam)
map.to_html('assets/building.html')