{"record":{"id":"591405baa5262976","repo":"facebookresearch/detectron2","slug":"bbox-has-to-has-length-4-or-5-got-bbox","errorCode":null,"errorMessage":"bbox has to has length 4 or 5. Got {bbox}.","messagePattern":"bbox has to has length 4 or 5\\. Got (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"detectron2/data/datasets/coco.py","lineNumber":371,"sourceCode":"            \"width\": int(image_dict[\"width\"]),\n            \"height\": int(image_dict[\"height\"]),\n            \"file_name\": str(image_dict[\"file_name\"]),\n        }\n        coco_images.append(coco_image)\n\n        anns_per_image = image_dict.get(\"annotations\", [])\n        for annotation in anns_per_image:\n            # create a new dict with only COCO fields\n            coco_annotation = {}\n\n            # COCO requirement: XYWH box format for axis-align and XYWHA for rotated\n            bbox = annotation[\"bbox\"]\n            if isinstance(bbox, np.ndarray):\n                if bbox.ndim != 1:\n                    raise ValueError(f\"bbox has to be 1-dimensional. Got shape={bbox.shape}.\")\n                bbox = bbox.tolist()\n            if len(bbox) not in [4, 5]:\n                raise ValueError(f\"bbox has to has length 4 or 5. Got {bbox}.\")\n            from_bbox_mode = annotation[\"bbox_mode\"]\n            to_bbox_mode = BoxMode.XYWH_ABS if len(bbox) == 4 else BoxMode.XYWHA_ABS\n            bbox = BoxMode.convert(bbox, from_bbox_mode, to_bbox_mode)\n\n            # COCO requirement: instance area\n            if \"segmentation\" in annotation:\n                # Computing areas for instances by counting the pixels\n                segmentation = annotation[\"segmentation\"]\n                # TODO: check segmentation type: RLE, BinaryMask or Polygon\n                if isinstance(segmentation, list):\n                    polygons = PolygonMasks([segmentation])\n                    area = polygons.area()[0].item()\n                elif isinstance(segmentation, dict):  # RLE\n                    area = mask_util.area(segmentation).item()\n                else:\n                    raise TypeError(f\"Unknown segmentation type {type(segmentation)}!\")\n            else:\n                # Computing areas using bounding boxes","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/data/datasets/coco.py#L353-L389","documentation":"COCO boxes must have 4 elements (XYWH axis-aligned) or 5 (XYWHA rotated). convert_to_coco_dict rejects bboxes of any other length with this ValueError.","triggerScenarios":"Passing annotation dicts with bbox of length 3, 6, or 8 (e.g. an 8-number polygon mistakenly stored in 'bbox'), or truncated arrays, to convert_to_coco_dict / convert_to_coco_json.","commonSituations":"Custom evaluators reusing polygon coordinates as bbox; half-written boxes from slicing bugs; mixing keypoint formats into bbox fields.","solutions":["Ensure each bbox is exactly [x,y,w,h] (or [cx,cy,w,h,angle] for rotated detection)","Validate before export: all(len(a['bbox']) in (4,5) for a in anns)","If the source is a polygon, compute its bounding box rather than copying raw coords"],"exampleFix":"# before\nann['bbox'] = polygon_coords  # 8 numbers\n# after\nxs, ys = polygon_coords[0::2], polygon_coords[1::2]\nann['bbox'] = [min(xs), min(ys), max(xs)-min(xs), max(ys)-min(ys)]","handlingStrategy":"validation","validationCode":"assert len(annotation['bbox']) in (4, 5), f\"bbox length must be 4 or 5, got {len(annotation['bbox'])}\"","typeGuard":"def valid_bbox_len(b) -> bool:\n    return isinstance(b, (list, tuple)) and len(b) in (4, 5)","tryCatchPattern":null,"preventionTips":["Never store raw polygon coordinates in bbox fields","Use 5-element boxes only for rotated detection configs","Unit-test exporters against COCO schema"],"tags":["detectron2","coco-export","bbox-length","validation"],"backgroundTag":"array-shape-mismatch","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}