DetecTree
A Pythonic library to classify tree/non-tree pixels from aerial imagery.
https://github.com/martibosch/detectree
Category: Biosphere
Sub Category: Forest Remote Sensing
Keywords
image-segmentation python remote-sensing tree-canopy tree-detection tree-pixels
Keywords from Contributors
archiving measurements observation transformer conversion projections optimizer climate-model thread profiles
Last synced: about 20 hours ago
JSON representation
Repository metadata
Tree detection from aerial imagery in Python
- Host: GitHub
- URL: https://github.com/martibosch/detectree
- Owner: martibosch
- License: gpl-3.0
- Created: 2019-07-31T07:52:10.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-21T20:25:59.000Z (7 days ago)
- Last Synced: 2025-04-21T21:34:26.605Z (6 days ago)
- Topics: image-segmentation, python, remote-sensing, tree-canopy, tree-detection, tree-pixels
- Language: Python
- Homepage: https://doi.org/10.21105/joss.02172
- Size: 4.86 MB
- Stars: 253
- Watchers: 6
- Forks: 33
- Open Issues: 6
- Releases: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
README.md
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)
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)
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 [email protected]: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
- lausanne-tree-canopy: example computational workflow to get the tree canopy of Lausanne with DetecTree
- A video of a talk about DetecTree in the Applied Machine Learning Days of EPFL (2020) and its respective slides
Acknowledgments
- With the support of the École Polytechnique Fédérale de Lausanne (EPFL)
References
- 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.
Owner metadata
- Name: Martí Bosch
- Login: martibosch
- Email:
- Kind: user
- Description: Doctor in civil and environmental engineering. Urban sprawl, Python, and a bit of landscape ecology and complexity
- Website: https://fosstodon.org/@martibosch
- Location: Lausanne
- Twitter: mortybosch
- Company: EPFL
- Icon url: https://avatars.githubusercontent.com/u/5831581?u=bbb7524d7940ede763089006bffe9f92e22608ce&v=4
- Repositories: 83
- Last ynced at: 2024-06-11T15:35:50.373Z
- Profile URL: https://github.com/martibosch
GitHub Events
Total
- Create event: 1
- Issues event: 2
- Release event: 1
- Watch event: 23
- Issue comment event: 1
- Push event: 50
- Fork event: 2
Last Year
- Create event: 1
- Issues event: 2
- Release event: 1
- Watch event: 23
- Issue comment event: 1
- Push event: 50
- Fork event: 2
Committers metadata
Last synced: 7 days ago
Total Commits: 108
Total Committers: 2
Avg Commits per committer: 54.0
Development Distribution Score (DDS): 0.009
Commits in past year: 48
Committers in past year: 1
Avg Commits per committer in past year: 48.0
Development Distribution Score (DDS) in past year: 0.0
Name | Commits | |
---|---|---|
Martí Bosch | m****h@p****m | 107 |
Kristen Thyng | k****g@g****m | 1 |
Committer domains:
Issue and Pull Request metadata
Last synced: 2 days ago
Total issues: 13
Total pull requests: 14
Average time to close issues: 2 months
Average time to close pull requests: 5 days
Total issue authors: 9
Total pull request authors: 4
Average comments per issue: 3.0
Average comments per pull request: 0.57
Merged pull request: 4
Bot issues: 0
Bot pull requests: 12
Past year issues: 2
Past year pull requests: 0
Past year average time to close issues: 5 months
Past year average time to close pull requests: N/A
Past year issue authors: 2
Past year pull request authors: 0
Past year average comments per issue: 1.0
Past year average comments per pull request: 0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- rmsare (3)
- MisterB92 (2)
- easz (2)
- jehontan (1)
- ligaofeng0901 (1)
- wittylife (1)
- Ayushri (1)
- tadolphi (1)
- pedromarcolin (1)
Top Pull Request Authors
- dependabot[bot] (9)
- pre-commit-ci[bot] (3)
- kthyng (1)
- martibosch (1)
Top Issue Labels
Top Pull Request Labels
- dependencies (9)
Package metadata
- Total packages: 2
-
Total downloads:
- pypi: 797 last-month
- Total dependent packages: 0 (may contain duplicates)
- Total dependent repositories: 4 (may contain duplicates)
- Total versions: 14
- Total maintainers: 1
pypi.org: detectree
Tree detection from aerial imagery in Python.
- Homepage:
- Documentation: https://detectree.readthedocs.io/
- Licenses: GPL-3.0
- Latest release: 0.6.0 (published 4 months ago)
- Last Synced: 2025-04-26T13:01:47.525Z (2 days ago)
- Versions: 10
- Dependent Packages: 0
- Dependent Repositories: 3
- Downloads: 797 Last month
-
Rankings:
- Stargazers count: 5.52%
- Dependent packages count: 7.31%
- Forks count: 7.6%
- Dependent repos count: 9.143%
- Average: 10.101%
- Downloads: 20.93%
- Maintainers (1)
conda-forge.org: detectree
- Homepage: https://github.com/martibosch/detectree
- Licenses: GPL-3.0-or-later
- Latest release: 0.4.2 (published over 2 years ago)
- Last Synced: 2025-04-01T02:09:58.893Z (27 days ago)
- Versions: 4
- Dependent Packages: 0
- Dependent Repositories: 1
-
Rankings:
- Dependent repos count: 24.103%
- Stargazers count: 31.36%
- Forks count: 35.258%
- Average: 35.565%
- Dependent packages count: 51.54%
Dependencies
- actions/checkout v4 composite
- actions/download-artifact v3 composite
- actions/setup-python v4 composite
- actions/upload-artifact v3 composite
- heinrichreimer/github-changelog-generator-action v2.1.1 composite
- pypa/gh-action-pypi-publish release/v1 composite
- softprops/action-gh-release v1 composite
- actions/checkout v4 composite
- codecov/codecov-action v3 composite
- mamba-org/setup-micromamba v1 composite
- dask [delayed,distributed]
- joblib *
- laspy >= 2.0.0
- numpy >= 1.15
- opencv-python >= 4.0.0
- pandas >= 0.23
- pymaxflow >= 1.0.0
- rasterio >= 1.0.0
- scikit-image *
- scikit-learn *
- scipy >= 1.0.0
- tqdm *
- m2r2 ==0.3.3
- pydata-sphinx-theme ==0.15.2
- sphinx ==7.2.6
- sphinx_click ==5.01
- sphinxcontrib.bibtex ==2.6.2
Score: 12.937083850125997