In [ ]:
Copied!
# %pip install geoai-py
# %pip install geoai-py
Import libraries¶
In [ ]:
Copied!
import geoai
import geoai
Download sample data¶
We will download a sample image from Hugging Face Hub to use for car detection. You can find more high-resolution images from OpenAerialMap.
In [ ]:
Copied!
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/cars_7cm.tif"
)
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/cars_7cm.tif"
)
In [ ]:
Copied!
raster_path = geoai.download_file(raster_url)
raster_path = geoai.download_file(raster_url)
Visualize the image¶
In [ ]:
Copied!
geoai.view_raster(raster_url)
geoai.view_raster(raster_url)
Initialize the model¶
In [ ]:
Copied!
detector = geoai.CarDetector()
detector = geoai.CarDetector()
Extract cars¶
Extract cars from the image using the model and save the output image.
In [ ]:
Copied!
mask_path = detector.generate_masks(
raster_path=raster_path,
output_path="cars_masks.tif",
confidence_threshold=0.3,
mask_threshold=0.5,
overlap=0.25,
chip_size=(400, 400),
)
mask_path = detector.generate_masks(
raster_path=raster_path,
output_path="cars_masks.tif",
confidence_threshold=0.3,
mask_threshold=0.5,
overlap=0.25,
chip_size=(400, 400),
)
Convert the image masks to polygons and save the output GeoJSON file.
In [ ]:
Copied!
gdf = detector.vectorize_masks(
masks_path="cars_masks.tif",
output_path="cars.geojson",
min_object_area=100,
max_object_area=2000,
)
gdf = detector.vectorize_masks(
masks_path="cars_masks.tif",
output_path="cars.geojson",
min_object_area=100,
max_object_area=2000,
)
Add geometric properties¶
In [ ]:
Copied!
gdf = geoai.add_geometric_properties(gdf)
gdf = geoai.add_geometric_properties(gdf)
Visualize initial results¶
In [ ]:
Copied!
geoai.view_vector_interactive(gdf, column="confidence", tiles=raster_url)
geoai.view_vector_interactive(gdf, column="confidence", tiles=raster_url)
Filter cars by area¶
In [ ]:
Copied!
gdf_filter = gdf[
(gdf["area_m2"] > 8) & (gdf["area_m2"] < 60) & (gdf["minor_length_m"] > 1)
]
gdf_filter = gdf[
(gdf["area_m2"] > 8) & (gdf["area_m2"] < 60) & (gdf["minor_length_m"] > 1)
]
In [ ]:
Copied!
len(gdf_filter)
len(gdf_filter)
Visualiza final results¶
In [ ]:
Copied!
geoai.view_vector_interactive(gdf_filter, column="confidence", tiles=raster_url)
geoai.view_vector_interactive(gdf_filter, column="confidence", tiles=raster_url)
In [ ]:
Copied!
geoai.view_vector_interactive(gdf_filter, tiles=raster_url)
geoai.view_vector_interactive(gdf_filter, tiles=raster_url)