detectree2 module¶
Tree crown delineation using detectree2.
This module provides a high-level interface for automatic tree crown delineation in aerial RGB imagery using the detectree2 library, which is based on Mask R-CNN (Detectron2 implementation).
Reference
Ball, J.G.C., et al. (2023). Accurate delineation of individual tree crowns in tropical forests from aerial RGB imagery using Mask R-CNN. Remote Sens Ecol Conserv. 9(5):641-655. https://doi.org/10.1002/rse2.332
Repository: https://github.com/PatBall1/detectree2
TreeCrownDelineator
¶
Class for automatic tree crown delineation using detectree2.
This class provides methods for detecting and delineating individual tree crowns in aerial RGB imagery using pre-trained or custom Mask R-CNN models.
Attributes:
| Name | Type | Description |
|---|---|---|
model_path |
str
|
Path to the trained model weights. |
device |
str
|
Device to run inference on ('cuda' or 'cpu'). |
cfg |
str
|
Detectron2 configuration object. |
predictor |
str
|
Detectron2 DefaultPredictor instance. |
Example
from samgeo.detectree2 import TreeCrownDelineator delineator = TreeCrownDelineator() delineator.predict("orthomosaic.tif", "crowns.gpkg")
Source code in samgeo/detectree2.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
__init__(model_path=None, model_name='default', device=None, confidence_threshold=0.5, nms_threshold=0.3)
¶
Initialize the TreeCrownDelineator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
Optional[str]
|
Path to a trained model file (.pth). If None, downloads a pre-trained model based on model_name. |
None
|
model_name
|
str
|
Name of pre-trained model to use if model_path is None. Options: 'paracou', 'sepilok', 'danum', 'default'. |
'default'
|
device
|
Optional[str]
|
Device for inference ('cuda' or 'cpu'). If None, auto-detects. |
None
|
confidence_threshold
|
float
|
Minimum confidence score for predictions (0-1). |
0.5
|
nms_threshold
|
float
|
IoU threshold for non-maximum suppression (0-1). |
0.3
|
Source code in samgeo/detectree2.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
predict(image_path, output_path, tile_width=40, tile_height=40, buffer=30, simplify_tolerance=0.3, min_confidence=0.5, iou_threshold=0.6, output_format='gpkg', cleanup=True, **kwargs)
¶
Detect and delineate tree crowns in an orthomosaic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
str
|
Path to the input orthomosaic (GeoTIFF). |
required |
output_path
|
str
|
Path for the output crown polygons. |
required |
tile_width
|
int
|
Width of prediction tiles in meters. |
40
|
tile_height
|
int
|
Height of prediction tiles in meters. |
40
|
buffer
|
int
|
Buffer size around tiles in meters (for edge handling). |
30
|
simplify_tolerance
|
float
|
Tolerance for simplifying crown geometries. |
0.3
|
min_confidence
|
float
|
Minimum confidence score to keep predictions. |
0.5
|
iou_threshold
|
float
|
IoU threshold for removing overlapping crowns. |
0.6
|
output_format
|
str
|
Output format ('gpkg', 'shp', 'geojson'). |
'gpkg'
|
cleanup
|
bool
|
Whether to remove temporary files after prediction. |
True
|
**kwargs
|
Any
|
Additional arguments passed to tile_data. |
{}
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame containing the detected tree crown polygons. |
Source code in samgeo/detectree2.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
predict_tiles(tiles_dir, output_dir=None)
¶
Run predictions on pre-tiled images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tiles_dir
|
str
|
Directory containing tiled images. |
required |
output_dir
|
Optional[str]
|
Directory to save predictions. If None, saves in tiles_dir. |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of paths to prediction files. |
Source code in samgeo/detectree2.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
download_sample_data(output_dir='./detectree2_sample')
¶
Download sample data for testing detectree2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str
|
Directory to save the sample data. |
'./detectree2_sample'
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the output directory. |
Source code in samgeo/detectree2.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
list_pretrained_models()
¶
List available pre-trained models.
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dictionary mapping model names to their download URLs. |
Source code in samgeo/detectree2.py
465 466 467 468 469 470 471 | |
prepare_training_data(image_path, crowns_path, output_dir, tile_width=40, tile_height=40, buffer=30, threshold=0.6, test_fraction=0.15, mode='rgb')
¶
Prepare training and test data for detectree2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
str
|
Path to the input orthomosaic (GeoTIFF). |
required |
crowns_path
|
str
|
Path to manually delineated crown polygons. |
required |
output_dir
|
str
|
Directory to save the training data. |
required |
tile_width
|
int
|
Width of tiles in meters. |
40
|
tile_height
|
int
|
Height of tiles in meters. |
40
|
buffer
|
int
|
Buffer size around tiles in meters. |
30
|
threshold
|
float
|
Minimum crown coverage to keep a tile. |
0.6
|
test_fraction
|
float
|
Fraction of data to use for testing (0-1). |
0.15
|
mode
|
str
|
Image mode ('rgb' or 'ms' for multispectral). |
'rgb'
|
Returns:
| Type | Description |
|---|---|
Tuple[str, str]
|
Tuple of (train_dir, test_dir) paths. |
Source code in samgeo/detectree2.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
stitch_predictions(geo_predictions_dir, output_path, iou_threshold=0.6, min_confidence=0.5, simplify_tolerance=0.3, output_format='gpkg')
¶
Stitch and clean tile predictions into a single crown map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geo_predictions_dir
|
str
|
Directory containing geo-referenced predictions. |
required |
output_path
|
str
|
Path for the output crown polygons. |
required |
iou_threshold
|
float
|
IoU threshold for removing overlapping crowns. |
0.6
|
min_confidence
|
float
|
Minimum confidence score to keep predictions. |
0.5
|
simplify_tolerance
|
float
|
Tolerance for simplifying crown geometries. |
0.3
|
output_format
|
str
|
Output format ('gpkg', 'shp', 'geojson'). |
'gpkg'
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame containing the stitched and cleaned crown polygons. |
Source code in samgeo/detectree2.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
tile_orthomosaic(image_path, output_dir, tile_width=40, tile_height=40, buffer=30, crowns_path=None, threshold=0.6, mode='rgb', **kwargs)
¶
Tile an orthomosaic for training or prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
str
|
Path to the input orthomosaic (GeoTIFF). |
required |
output_dir
|
str
|
Directory to save the tiles. |
required |
tile_width
|
int
|
Width of tiles in meters. |
40
|
tile_height
|
int
|
Height of tiles in meters. |
40
|
buffer
|
int
|
Buffer size around tiles in meters. |
30
|
crowns_path
|
Optional[str]
|
Path to crown polygons (for training data preparation). |
None
|
threshold
|
float
|
Minimum crown coverage to keep a tile (when crowns provided). |
0.6
|
mode
|
str
|
Image mode ('rgb' or 'ms' for multispectral). |
'rgb'
|
**kwargs
|
Any
|
Additional arguments passed to tile_data. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the output directory containing tiles. |
Source code in samgeo/detectree2.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |