Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Detect and Segment Tree Crowns from Satellite Imagery Using QGIS

Detecting individual tree crowns from satellite or aerial imagery is useful for forest inventory, urban canopy monitoring, and ecological research. The DeepForest Python package provides pre-trained models that can detect trees, birds, and other objects directly from imagery. I have integrated DeepForest into the GeoAI QGIS plugin, so you can now run tree crown detection with a few clicks inside QGIS without writing any Python code.

In this tutorial, I walk through the full workflow: setting up the environment with Pixi, installing the plugin, loading sample data, running tree detection, and exporting results in multiple formats.

Video tutorial: Detect and Segment Tree Crowns with the GeoAI QGIS Plugin

What You Will Need

Set Up the Environment with Pixi

The GeoAI plugin requires specific versions of PyTorch, CUDA, and other deep learning libraries. Using Pixi ensures these dependencies do not conflict with your existing QGIS installation. Do not use your system QGIS for this workflow.

Create a Pixi project

Open a terminal and create a new Pixi project:

pixi init geo
cd geo

This creates a geo directory with a pixi.toml configuration file inside.

Configure CUDA dependencies

Check your CUDA version by running:

nvidia-smi

Look for the CUDA version in the output (e.g., 12.x or 13.x). Then open the pixi.toml file and paste the appropriate dependency block from the GeoAI plugin installation instructions. Save the file.

Install the base environment

pixi install

This installs QGIS, PyTorch, and all required dependencies into the isolated Pixi environment. It may take 5 to 10 minutes depending on your internet speed.

Verify PyTorch and CUDA

You can verify that PyTorch and CUDA are working correctly by running the verification command from the installation instructions. The output should show your PyTorch version and confirm that CUDA and your GPU are available.

Install DeepForest

Due to dependency conflicts between DeepForest and some of its transitive dependencies (NumPy and Transformers version mismatches), DeepForest needs to be installed separately with pip rather than through Pixi:

pixi run pip install deepforest

After installing DeepForest, upgrade NumPy and Transformers back to their compatible versions:

pixi run pip install --upgrade "numpy>=2.0" "transformers>=4.0"

You may see some dependency warnings, but these can be safely ignored.

Install the GeoAI Plugin

Launch QGIS through Pixi:

pixi run qgis

Then install the GeoAI plugin from the QGIS Plugin Manager:

  1. Go to Plugins > Manage and Install Plugins.

  2. Search for geoai.

  3. Click Install Plugin.

After installation, the GeoAI menu appears in the menu bar and a tree icon appears in the toolbar. If you had a previous version installed, click GeoAI > Check for Updates to get the latest version, then restart QGIS.

Load Sample Data

DeepForest ships with built-in sample data you can use for testing. Navigate to the DeepForest package directory inside your Pixi environment:

geo/.pixi/envs/default/lib/python3.12/site-packages/deepforest/data/

Inside the data folder, you will find sample GeoTIFF files. Drag the sample file (e.g., OSBS_029.tif) into the QGIS map canvas to load it.

If you already have your own satellite or aerial imagery, you can use that instead.

Run Tree Crown Detection

  1. Click the tree icon in the toolbar to open the DeepForest panel.

  2. Under Model, select deepforest-tree (the default tree segmentation model). Other models are available for detecting birds and livestock.

  3. Click Load Model. The plugin downloads the model from Hugging Face (first time only) and loads it onto your GPU. A green confirmation message appears when the model is ready.

  4. Select your image layer from the dropdown. If you added the layer after opening the panel, click Refresh to update the list.

  5. Click Set Image from Layer to set the input image.

  6. Under Predict, select Single Image mode for small images or Large Image mode for bigger GeoTIFFs that benefit from tiled processing.

  7. Adjust the Score Threshold (default: 0.5). A higher threshold produces fewer but more confident detections. A lower threshold captures more trees but may include false positives.

  8. Click Run Prediction.

The prediction typically completes in a few seconds for small images. The detected tree crowns appear as bounding box polygons on the map.

Export Results

The plugin supports multiple output formats:

Inspect Detection Results

Open the attribute table of the vector layer to see each detection’s properties:

You can sort by score to identify low-confidence detections and filter them out. This is useful for cleaning up results before downstream analysis.

Export Training Data

If the pre-trained model does not perform well enough on your specific imagery, you can fine-tune it with your own training data. The plugin makes it easy to export detection results as training datasets:

  1. Go to the Export section in the DeepForest panel.

  2. Select a format (e.g., COCO).

  3. Choose an output directory and click Export.

The exported dataset includes image chips and annotation files that you can use to train or fine-tune a DeepForest model for your study area.

Tips for Better Results

Resources

DeepForest’s pre-trained models provide a quick starting point for tree detection without requiring you to train your own model. While results may not be perfect for every type of imagery, the combination of fast detection and easy export makes it a practical tool for generating initial results or bootstrapping training data. Give it a try with your own imagery and feel free to open an issue on GitHub if you run into any problems.