{"record":{"id":"cccd9504aefe31b4","repo":"sgl-project/sglang","slug":"input-image-is-empty","errorCode":null,"errorMessage":"input image is empty","messagePattern":"input image is empty","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/utils/mesh3d_utils.py","lineNumber":978,"sourceCode":"        if image.shape[-1] == 4:\n            mask = image[..., 3]\n        else:\n            mask = np.ones_like(image[..., 0:1]) * 255\n            image = np.concatenate([image, mask], axis=-1)\n            mask = mask[..., 0]\n\n        height, width, channels = image.shape\n\n        size = max(height, width)\n        result = np.zeros((size, size, channels), dtype=np.uint8)\n\n        coords = np.nonzero(mask)\n        x_min, x_max = coords[0].min(), coords[0].max()\n        y_min, y_max = coords[1].min(), coords[1].max()\n        crop_h = x_max - x_min\n        crop_w = y_max - y_min\n        if crop_h == 0 or crop_w == 0:\n            raise ValueError(\"input image is empty\")\n        desired_size = int(size * (1 - border_ratio))\n        scale = desired_size / max(crop_h, crop_w)\n        scaled_h = int(crop_h * scale)\n        scaled_w = int(crop_w * scale)\n        x2_min = (size - scaled_h) // 2\n        x2_max = x2_min + scaled_h\n\n        y2_min = (size - scaled_w) // 2\n        y2_max = y2_min + scaled_w\n\n        result[x2_min:x2_max, y2_min:y2_max] = cv2.resize(\n            image[x_min:x_max, y_min:y_max],\n            (scaled_w, scaled_h),\n            interpolation=cv2.INTER_AREA,\n        )\n\n        bg = np.ones((result.shape[0], result.shape[1], 3), dtype=np.uint8) * 255\n","sourceCodeStart":960,"sourceCodeEnd":996,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/utils/mesh3d_utils.py#L960-L996","documentation":"Raised by recenter in mesh3d_utils.py when the nonzero bounding box of a mask collapses to zero height or width (crop_h == 0 or crop_w == 0). Because coords = np.nonzero(mask) on a fully-empty mask yields min/max over empty arrays producing a degenerate box, the function cannot build a centered crop.","triggerScenarios":"Calling load_image / recenter with an all-zero mask; masks where nonzero pixels form a single row or column after thresholding also trigger it (crop_h or crop_w becomes 0).","commonSituations":"Thresholding masks too aggressively (everything below threshold); matting/segmentation models returning empty masks for images with no detected subject; wrong mask channel or inverted mask passed in; grayscale images converted incorrectly so the mask is all background.","solutions":["Check mask.any() / mask.sum() > 0 before calling recenter","Verify the mask isn't inverted or thresholded too hard — inspect unique values np.unique(mask)","Confirm you're passing the correct mask array (right channel, right dtype, 0/255 vs 0/1 range)","Re-run or swap the upstream segmentation/matting model if it produces empty masks"],"exampleFix":"// before\nrecenter(image, mask, size=512)\n\n// after\nif not mask.any():\n    raise ValueError(\"empty mask; skipping recenter\")\nrecenter(image, mask, size=512)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef mask_is_usable(mask: np.ndarray) -> bool:\n    m = np.asarray(mask)\n    nz = np.nonzero(m)\n    return nz[0].size > 1 and nz[1].size > 1  # non-degenerate bbox\n\nif not mask_is_usable(mask):\n    raise ValueError(\"mask empty or degenerate; skip recenter\")","typeGuard":null,"tryCatchPattern":"try:\n    recenter(image, mask, size=size)\nexcept ValueError as e:\n    if \"input image is empty\" in str(e):\n        # fall back to plain resize without cropping\n        pass","preventionTips":["Sanitize masks (check .any() and bbox extent) before recenter","Standardize mask value range (0/1 vs 0/255) at pipeline boundaries","Test threshold values against a sample of real inputs"],"tags":["image-processing","mask","segmentation","mesh3d","validation"],"backgroundTag":"empty-image-input","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}