Skip to main content

Tree detection from aerial imagery in Python.

Project description

PyPI version fury.io Conda Version Documentation Status tests pre-commit.ci status codecov GitHub license DOI DOI

DetecTree

Overview

DetecTree is a Pythonic library to perform semantic segmentation of aerial imagery into tree/non-tree pixels, following the methods of Yang et al. [1]. A pre-trained model is available at Hugging Face hub, which can be used as follows:

from urllib import request

import detectree as dtr
import matplotlib.pyplot as plt
import rasterio as rio
from rasterio import plot

# download a tile from the SWISSIMAGE WMS
tile_url = (
    "https://wms.geo.admin.ch/?SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&"
    "FORMAT=image/png&LAYERS=ch.swisstopo.images-swissimage&CRS=EPSG:2056"
    "&BBOX=2532980,1152150,2533380,1152450&WIDTH=800&HEIGHT=600"
)
tile_filename = "tile.png"
request.urlretrieve(tile_url, tile_filename)

# use the pre-trained model to segment the image into tree/non-tree-pixels
y_pred = dtr.Classifier().predict_img(tile_filename)

# side-by-side plot of the tile and the predicted tree/non-tree pixels
figwidth, figheight = plt.rcParams["figure.figsize"]
fig, axes = plt.subplots(1, 2, figsize=(2 * figwidth, figheight))
with rio.open(tile_filename) as src:
    plot.show(src, ax=axes[0])
axes[1].imshow(y_pred)

Pre-trained example

Alternatively, you can use detectree to train your own model on your aerial imagery dataset:

import detectree as dtr
import matplotlib.pyplot as plt
import rasterio as rio
from rasterio import plot

# select the training tiles from the tiled aerial imagery dataset
ts = dtr.TrainingSelector(img_dir='data/tiles')
split_df = ts.train_test_split(method='cluster-I')

# train a tree/non-tree pixel classifier
clf = dtr.ClassifierTrainer().train_classifier(
    split_df=split_df, response_img_dir='data/response_tiles')

# use the trained classifier to predict the tree/non-tree pixels
test_filepath = split_df[~split_df['train'].sample(1).iloc[0]['img_filepath']
y_pred = dtr.Classifier(clf=clf).classify_img(test_filepath)

# side-by-side plot of the tile and the predicted tree/non-tree pixels
figwidth, figheight = plt.rcParams['figure.figsize']
fig, axes = plt.subplots(1, 2, figsize=(2 * figwidth, figheight))

with rio.open(img_filepath) as src:
    plot.show(src.read(), ax=axes[0])
axes[1].imshow(y_pred)

Example

A full example application of DetecTree to predict a tree canopy map for the Aussersihl district in Zurich is available as a Jupyter notebook. See also the API reference documentation and the examples repository for more information on the background and some example notebooks.

The target audience is researchers and practitioners in GIS that are interested in two-dimensional aspects of trees, such as their proportional abundance and spatial distribution throughout a region of study. These measurements can be used to assess important aspects of urban planning such as the provision of urban ecosystem services. The approach is of special relevance when LIDAR data is not available or it is too costly in monetary or computational terms.

Citation

Bosch M. 2020. “DetecTree: Tree detection from aerial imagery in Python”. Journal of Open Source Software, 5(50), 2172. doi.org/10.21105/joss.02172

Note that DetecTree is based on the methods of Yang et al. [1], therefore it seems fair to reference their work too. An example citation in an academic paper might read as follows:

The classification of tree pixels has been performed with the Python library DetecTree (Bosch, 2020), which is based on the approach of Yang et al. (2009).

Installation

With conda

The easiest way to install detectree is with conda as in:

conda install -c conda-forge detectree

With pip

You can install detectree with pip as in:

pip install detectree

If you want to be able to read compressed LAZ files, you will need the Python bindings for laszip. Note that the latter require [laszip], which can be installed using conda (which is automatically handled when installing detectree with conda as shown above) or downloaded from laszip.org. Then, detectree and the Python bindings for laszip can be installed with pip as in:

pip install detectree[laszip]

Development install

To install a development version of detectree, you can first use conda to create an environment with all the dependencies - with the environment-dev.yml file - and activate it as in:

conda env create -f environment-dev.yml
conda activate detectree-dev

and then clone the repository and use pip to install it in development mode

git clone git@github.com:martibosch/detectree.git
cd detectree/
pip install -e .

This will also install the dependencies required for running tests, linting the code and building the documentation. Additionally, you can activate pre-commit so that the latter are run as pre-commit hooks as in:

pre-commit install

See also

Acknowledgments

  • With the support of the École Polytechnique Fédérale de Lausanne (EPFL)

References

  1. Yang, L., Wu, X., Praun, E., & Ma, X. (2009). Tree detection from aerial imagery. In Proceedings of the 17th ACM SIGSPATIAL International Conference on Advances in Geographic Information Systems (pp. 131-137). ACM.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

detectree-0.6.0.tar.gz (47.0 kB view details)

Uploaded Source

Built Distribution

detectree-0.6.0-py3-none-any.whl (44.1 kB view details)

Uploaded Python 3

File details

Details for the file detectree-0.6.0.tar.gz.

File metadata

  • Download URL: detectree-0.6.0.tar.gz
  • Upload date:
  • Size: 47.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for detectree-0.6.0.tar.gz
Algorithm Hash digest
SHA256 16ada0fa7ee03dcd7b7075980640b229f289c24c976332337680720626a76fbd
MD5 965837487091003915b252f8a439455a
BLAKE2b-256 49efdc6d0f382df48a2bac22953559e2483c15c1cb9f07f405212e6486a98691

See more details on using hashes here.

Provenance

The following attestation bundles were made for detectree-0.6.0.tar.gz:

Publisher: release.yml on martibosch/detectree

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file detectree-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: detectree-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 44.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for detectree-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5526cc38d24d435b525b1c22de246d7abeff7913e56599dceadbe7bdecba4c0c
MD5 6910de1ff57d615dc50f10d0457f2cf0
BLAKE2b-256 33a215329e1ceb9faf909efb1a83ce1c1d66dc37067acd18aebcb6a0e0c8e193

See more details on using hashes here.

Provenance

The following attestation bundles were made for detectree-0.6.0-py3-none-any.whl:

Publisher: release.yml on martibosch/detectree

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page