{"id":13724679,"url":"https://github.com/martibosch/detectree","last_synced_at":"2025-05-16T00:06:48.351Z","repository":{"id":44877269,"uuid":"199807436","full_name":"martibosch/detectree","owner":"martibosch","description":"Tree detection from aerial imagery in Python","archived":false,"fork":false,"pushed_at":"2025-05-12T20:35:37.000Z","size":5100,"stargazers_count":255,"open_issues_count":6,"forks_count":33,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-05-13T21:41:48.787Z","etag":null,"topics":["image-segmentation","python","remote-sensing","tree-canopy","tree-detection","tree-pixels"],"latest_commit_sha":null,"homepage":"https://doi.org/10.21105/joss.02172","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/martibosch.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-07-31T07:52:10.000Z","updated_at":"2025-05-01T09:29:03.000Z","dependencies_parsed_at":"2024-05-02T04:59:55.239Z","dependency_job_id":"4061de75-052f-4c37-82a9-0334edece933","html_url":"https://github.com/martibosch/detectree","commit_stats":{"total_commits":92,"total_committers":4,"mean_commits":23.0,"dds":0.04347826086956519,"last_synced_commit":"eb1313637e928b6ce216cbec17d3d313c28c138c"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martibosch%2Fdetectree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martibosch%2Fdetectree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martibosch%2Fdetectree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martibosch%2Fdetectree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martibosch","download_url":"https://codeload.github.com/martibosch/detectree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254432826,"owners_count":22070368,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["image-segmentation","python","remote-sensing","tree-canopy","tree-detection","tree-pixels"],"created_at":"2024-08-03T01:02:01.679Z","updated_at":"2025-05-16T00:06:48.248Z","avatar_url":"https://github.com/martibosch.png","language":"Python","funding_links":[],"categories":["Python","Biosphere"],"sub_categories":["Forest Remote Sensing"],"readme":"[![PyPI version fury.io](https://badge.fury.io/py/detectree.svg)](https://pypi.python.org/pypi/detectree/)\n[![Conda Version](https://img.shields.io/conda/vn/conda-forge/detectree.svg)](https://anaconda.org/conda-forge/detectree)\n[![Documentation Status](https://readthedocs.org/projects/detectree/badge/?version=latest)](https://detectree.readthedocs.io/en/latest/?badge=latest)\n[![tests](https://github.com/martibosch/detectree/actions/workflows/tests.yml/badge.svg)](https://github.com/martibosch/detectree/blob/main/.github/workflows/tests.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/martibosch/detectree/main.svg)](https://results.pre-commit.ci/latest/github/martibosch/detectree/main)\n[![codecov](https://codecov.io/gh/martibosch/detectree/branch/main/graph/badge.svg?token=ZTZK2LFR6T)](https://codecov.io/gh/martibosch/detectree)\n[![GitHub license](https://img.shields.io/github/license/martibosch/detectree.svg)](https://github.com/martibosch/detectree/blob/master/LICENSE)\n[![DOI](https://joss.theoj.org/papers/10.21105/joss.02172/status.svg)](https://doi.org/10.21105/joss.02172)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.3908338.svg)](https://doi.org/10.5281/zenodo.3908338)\n\n# DetecTree\n\n## Overview\n\nDetecTree 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](https://huggingface.co/martibosch/detectree), which can be used as follows:\n\n```python\nfrom urllib import request\n\nimport detectree as dtr\nimport matplotlib.pyplot as plt\nimport rasterio as rio\nfrom rasterio import plot\n\n# download a tile from the SWISSIMAGE WMS\ntile_url = (\n    \"https://wms.geo.admin.ch/?SERVICE=WMS\u0026REQUEST=GetMap\u0026VERSION=1.3.0\u0026\"\n    \"FORMAT=image/png\u0026LAYERS=ch.swisstopo.images-swissimage\u0026CRS=EPSG:2056\"\n    \"\u0026BBOX=2532980,1152150,2533380,1152450\u0026WIDTH=800\u0026HEIGHT=600\"\n)\ntile_filename = \"tile.png\"\nrequest.urlretrieve(tile_url, tile_filename)\n\n# use the pre-trained model to segment the image into tree/non-tree-pixels\ny_pred = dtr.Classifier().predict_img(tile_filename)\n\n# side-by-side plot of the tile and the predicted tree/non-tree pixels\nfigwidth, figheight = plt.rcParams[\"figure.figsize\"]\nfig, axes = plt.subplots(1, 2, figsize=(2 * figwidth, figheight))\nwith rio.open(tile_filename) as src:\n    plot.show(src, ax=axes[0])\naxes[1].imshow(y_pred)\n```\n\n![Pre-trained example](figures/example-pre-trained.png)\n\nAlternatively, you can use detectree to train your own model on your aerial imagery dataset:\n\n```python\nimport detectree as dtr\nimport matplotlib.pyplot as plt\nimport rasterio as rio\nfrom rasterio import plot\n\n# select the training tiles from the tiled aerial imagery dataset\nts = dtr.TrainingSelector(img_dir='data/tiles')\nsplit_df = ts.train_test_split(method='cluster-I')\n\n# train a tree/non-tree pixel classifier\nclf = dtr.ClassifierTrainer().train_classifier(\n    split_df=split_df, response_img_dir='data/response_tiles')\n\n# use the trained classifier to predict the tree/non-tree pixels\ntest_filepath = split_df[~split_df['train'].sample(1).iloc[0]['img_filepath']\ny_pred = dtr.Classifier(clf=clf).classify_img(test_filepath)\n\n# side-by-side plot of the tile and the predicted tree/non-tree pixels\nfigwidth, figheight = plt.rcParams['figure.figsize']\nfig, axes = plt.subplots(1, 2, figsize=(2 * figwidth, figheight))\n\nwith rio.open(img_filepath) as src:\n    plot.show(src.read(), ax=axes[0])\naxes[1].imshow(y_pred)\n```\n\n![Example](figures/example.png)\n\nA full example application of DetecTree to predict a tree canopy map for the Aussersihl district in Zurich [is available as a Jupyter notebook](https://github.com/martibosch/detectree-examples/blob/main/notebooks/aussersihl-canopy.ipynb). See also [the API reference documentation](https://detectree.readthedocs.io/en/latest/?badge=latest) and the [examples repository](https://github.com/martibosch/detectree-examples) for more information on the background and some example notebooks.\n\nThe 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.\n\n## Citation\n\nBosch M. 2020. “DetecTree: Tree detection from aerial imagery in Python”. *Journal of Open Source Software, 5(50), 2172.* [doi.org/10.21105/joss.02172](https://doi.org/10.21105/joss.02172)\n\nNote 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:\n\n\u003e 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).\n\n## Installation\n\n### With conda\n\nThe easiest way to install `detectree` is with conda as in:\n\n```bash\nconda install -c conda-forge detectree\n```\n\n### With pip\n\nYou can install `detectree` with pip as in:\n\n```bash\npip install detectree\n```\n\nIf you want to be able to read compressed LAZ files, you will need [the Python bindings for `laszip`](https://github.com/tmontaigu/laszip-python). 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](https://laszip.org/). Then, detectree and the Python bindings for `laszip` can be installed with pip as in:\n\n```bash\npip install detectree[laszip]\n```\n\n### Development install\n\nTo 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](https://github.com/martibosch/detectree/blob/main/environment-dev.yml) - and activate it as in:\n\n```bash\nconda env create -f environment-dev.yml\nconda activate detectree-dev\n```\n\nand then clone the repository and use pip to install it in development mode\n\n```bash\ngit clone git@github.com:martibosch/detectree.git\ncd detectree/\npip install -e .\n```\n\nThis will also install the dependencies required for running tests, linting the code and building the documentation. Additionally, you can activate [pre-commit](https://pre-commit.com/) so that the latter are run as pre-commit hooks as in:\n\n```bash\npre-commit install\n```\n\n## See also\n\n- [lausanne-tree-canopy](https://github.com/martibosch/lausanne-tree-canopy): example computational workflow to get the tree canopy of Lausanne with DetecTree\n- [A video of a talk about DetecTree](https://www.youtube.com/watch?v=USwF2KyxVjY) in the [Applied Machine Learning Days of EPFL (2020)](https://appliedmldays.org/) and [its respective slides](https://martibosch.github.io/detectree-amld-2020)\n\n## Acknowledgments\n\n- With the support of the École Polytechnique Fédérale de Lausanne (EPFL)\n\n## References\n\n1. Yang, L., Wu, X., Praun, E., \u0026 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartibosch%2Fdetectree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartibosch%2Fdetectree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartibosch%2Fdetectree/lists"}