Package: detectree
DetecTreeOverviewDetecTree is a Pythonic library to classify tree/non-tree pixels from aerial imagery, following the methods of Yang et al. [1]. 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.importdetectreeasdtrimportmatplotlib.pyplotaspltimportrasterioasriofromrasterioimportplot# select the training tiles from the tiled aerial imagery datasetts=dtr.TrainingSelector(img_dir='data/tiles')split_df=ts.train_test_split(method='cluster-I')# train a tree/non-tree pixel classfierclf=dtr.ClassifierTrainer().train_classifier(split_df=split_df,response_img_dir='data/response_tiles')# use the trained classifier to predict the tree/non-tree pixelstest_filepath=split_df[~split_df['train'].sample(1).iloc[0]['img_filepath']y_pred=dtr.Classifier().classify_img(test_filepath,clf)# side-by-side plot of the tile and the predicted tree/non-tree pixelsfigwidth,figheight=plt.rcParams['figure.figsize']fig,axes=plt.subplots(1,2,figsize=(2*figwidth,figheight))withrio.open(img_filepath)assrc: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 Zurichis available as a Jupyter notebook. See alsothe API reference documentationand theexample repositoryfor more information on the background and some example notebooks.CitationBosch M. 2020. “DetecTree: Tree detection from aerial imagery in Python”.Journal of Open Source Software, 5(50), 2172.doi.org/10.21105/joss.02172Note 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).InstallationWith condaThe easiest way to installdetectreeis with conda as in:condainstall-cconda-forgedetectreeWith pipYou can installdetectreewith pip as in:pipinstalldetectreeIf you want to be able to read compressed LAZ files, you will needthe Python bindings forlaszip. Note that the latter require [laszip], which can be installed using conda (which is automatically handled when installingdetectreewith conda as shown above) or downloaded fromlaszip.org. Then, detectree and the Python bindings forlaszipcan be installed with pip as in:pipinstalldetectree[laszip]Development installTo install a development version of detectree, you can first use conda to create an environment with all the dependencies - with theenvironment-dev.ymlfile- and activate it as in:condaenvcreate-fenvironment-dev.yml
condaactivatedetectree-devand then clone the repository and use pip to install it in development modegitclonegit@github.com:martibosch/detectree.gitcddetectree/
pipinstall-e.This will also install the dependencies required for running tests, linting the code and building the documentation. Additionally, you can activatepre-commitso that the latter are run as pre-commit hooks as in:pre-commitinstallSee alsolausanne-tree-canopy: example computational workflow to get the tree canopy of Lausanne with DetecTreeA video of a talk about DetecTreein theApplied Machine Learning Days of EPFL (2020)andits respective slidesAcknowledgmentsWith the support of the École Polytechnique Fédérale de Lausanne (EPFL)ReferencesYang, 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.