{"record":{"id":"2b497099565ec9ba","repo":"roboflow/supervision","slug":"coco-rle-counts-cannot-be-empty","errorCode":null,"errorMessage":"COCO RLE counts cannot be empty.","messagePattern":"COCO RLE counts cannot be empty\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/detection/compact_mask.py","lineNumber":411,"sourceCode":"        else:\n            # Convert to int64 first, then range-check against int32 bounds before\n            # narrowing. A direct int32 cast wraps silently on some numpy versions\n            # and raises on others; this makes overflow detection deterministic.\n            counts_arr64 = np.asarray(counts, dtype=np.int64)\n            int32_info = np.iinfo(np.int32)\n            if counts_arr64.size and (\n                counts_arr64.max() > int32_info.max\n                or counts_arr64.min() < int32_info.min\n            ):\n                raise ValueError(\"COCO RLE counts exceed int32 range.\")\n            counts_arr = counts_arr64.astype(np.int32)\n    except (TypeError, ValueError, OverflowError) as exc:\n        raise ValueError(\"Invalid COCO RLE counts.\") from exc\n\n    if counts_arr.ndim != 1:\n        raise ValueError(\"COCO RLE counts must be one-dimensional.\")\n    if counts_arr.size == 0:\n        raise ValueError(\"COCO RLE counts cannot be empty.\")\n    if np.any(counts_arr < 0):\n        raise ValueError(\"COCO RLE counts must be non-negative.\")\n    return counts_arr\n\n\ndef _rle_resize(\n    rle: npt.NDArray[np.int32],\n    crop_h: int,\n    crop_w: int,\n    new_crop_h: int,\n    new_crop_w: int,\n) -> npt.NDArray[np.int32]:\n    \"\"\"Resize an F-order RLE-encoded crop via nearest-neighbour resampling.\n\n    Manipulates run lengths directly without decoding to a full 2D boolean\n    array.  Delegates to :func:`_rle_split_cols`, :func:`_rle_scale_col`,\n    and :func:`_rle_join_cols`.\n","sourceCodeStart":393,"sourceCodeEnd":429,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/detection/compact_mask.py#L393-L429","documentation":"Raised when the COCO RLE counts array parsed for CompactMask.from_coco_rle is empty (size 0). An RLE must contain at least one run count to describe a mask, and the code uses counts to verify total area, so an empty array is meaningless and rejected. This check runs after the one-dimensionality check.","triggerScenarios":"Passing rle['counts'] = [] or an empty numpy array to CompactMask.from_coco_rle; or a COCO annotation whose segmentation counts field is an empty list/string.","commonSituations":"Placeholder annotations created for empty masks (COCO uses iscrowd/empty segmentation for no mask); truncated JSON; a serializer that dropped the counts field content; building RLEs programmatically and forgetting to fill counts.","solutions":["If the annotation genuinely has no mask, skip it instead of passing an empty RLE — filter annotations where not ann.get('segmentation').","If a mask exists, generate a correct RLE with pycocotools.mask.encode on the binary mask so counts is populated.","Check the JSON source for truncation if counts should not be empty."],"exampleFix":"# before\nrles = [{\"size\": [4, 4], \"counts\": []}]\n\n# after (skip empty masks)\nrles = [r for r in coco_rles if len(r[\"counts\"]) > 0]","handlingStrategy":"validation","validationCode":"rles = [r for r in rles if len(r.get(\"counts\", [])) > 0]\n# or for compressed payloads: if r.get(\"counts\")]","typeGuard":"def has_nonempty_counts(rle) -> bool:\n    c = rle.get(\"counts\")\n    return bool(c) and (not isinstance(c, (list, tuple)) or len(c) > 0)","tryCatchPattern":"try:\n    cm = sv.CompactMask.from_coco_rle(rles, xyxy, image_shape=shape)\nexcept ValueError as e:\n    if \"cannot be empty\" in str(e):\n        rles = [r for r in rles if r[\"counts\"]]\n        cm = sv.CompactMask.from_coco_rle(rles, xyxy, image_shape=shape)\n    else:\n        raise","preventionTips":["Treat empty segmentation as 'no mask' and filter such annotations before building the RLE list.","When serializing masks, skip zero-area masks instead of emitting empty counts.","Keep rles and xyxy filtered in the same comprehension so indexes stay aligned."],"tags":["coco","rle","compact-mask","empty-input","validation"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}