Skip to content

utilities.read_file raises KeyError: 'segmentation' when loading valid COCO bbox annotations with segmentation #1281

@kiplimock

Description

@kiplimock

Describe the bug
When using deepforest.utilities.read_file (DeepForest 2.0.0) to load a valid COCO detection JSON (bbox-only, no segmentation), the function raises a KeyError: 'segmentation'.

The JSON follows standard COCO detection format:

  • Top-level keys: info, licenses, images, annotations, categories
  • Each annotation has: id, image_id, category_id, bbox, area, iscrowd
  • No segmentation field (which is optional in COCO when you only use bounding boxes)

However, internally utilities.read_file assumes that a segmentation key is always present in each annotation and fails with KeyError when it is not.

This makes it impossible to directly use standard COCO detection datasets with DeepForest via utilities.read_file without first modifying the COCO file.

To Reproduce

  1. Create a minimal COCO JSON file (e.g., annotations.json) with:
  • One image:
     "images": [
       {
         "id": 1,
         "file_name": "2024_08_1.tif",
         "height": 4325,
         "width": 4325
       }
     ]
  • One bbox-only annotation (no segmentation key):
     "annotations": [
       {
         "id": 1,
         "image_id": 1,
         "category_id": 1,
         "bbox": [1364.38, 1586.75, 115.60, 119.88],
         "area": 13859.10,
         "iscrowd": 0
       }
     ]
  • One category:
     "categories": [
       {
         "id": 1,
         "name": "tree",
         "supercategory": "object"
       }
     ]
  1. In a notebook or script
   from deepforest import utilities

   root_dir = r"some_directory"
   annotations_file = "annotations.json"

   annotations = utilities.read_file(annotations_file, root_dir=root_dir)
  1. Observe that utilities.read_file raises:

Environment:

  • OS: Windows 11 Home 64-bit (10.0, Build 26200)
  • Python version and environment: 3.12
  • DeepForest: Version 2.0.0
  • Installation: Conda, version 24.11.1

Screenshots and Context

Image

User Story
As a researcher working with aerial tree detection datasets using standard COCO bbox annotations, I want to load my existing COCO JSON files directly with deepforest.utilities.read_file (or an equivalent DeepForest loader) without having to modify them, so that I can quickly plug DeepForest into my existing COCO-labelled workflows and tools without adding custom pre-processing steps or deviating from the COCO standard.

Metadata

Metadata

Labels

Feature RequestNew feature or requestin progressSomeone is currently working on this. Check with the assigned user before working on it.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions