tensorflow/models · error · ImageReadError

OpenCV could not read the image at

Error message

OpenCV could not read the image at {image_path!s}. The file may be missing, corrupt, or in an unsupported format.

What it means

ImageReadError from load_and_resize_image: cv2.imread returned None, i.e. OpenCV could not decode the file at image_path. Causes: the path does not exist, the file is corrupt or truncated, or the format is unsupported by this OpenCV build. Note imread fails silently (returns None rather than raising), so this guard converts that None into an explicit, path-carrying error.

Solutions

  1. Verify the path exists and is readable; inspect image_path for typos or wrong extensions
  2. Skip or quarantine corrupt files; a zero-byte or truncated download decodes to None
  3. Convert images to a widely supported format (JPEG/PNG) if the extension is exotic or the codec is missing
  4. Filter the input listing to known image extensions before processing
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/projects/waste_identification_ml/model_inference_with_tracking/sam3_dinov3_tracking_pipeline/image_ops.py:60 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/d5ba98c182ab2820. Report an issue: GitHub.

Appendix: source

Thrown at official/projects/waste_identification_ml/model_inference_with_tracking/sam3_dinov3_tracking_pipeline/image_ops.py:60

  implementation. The two implementations are not bit-identical (PIL and
  OpenCV use slightly different Lanczos filter parameters) but are visually
  equivalent and produce nearly identical downstream detections.

  Args:
    image_path: Path to the image file on disk.
    max_short_side: Maximum allowed length of the shortest side. If the image is
      larger, it is downscaled preserving aspect ratio.

  Returns:
    The loaded and (optionally) resized image as a PIL RGB Image.

  Raises:
    ImageReadError: If OpenCV is unable to decode the file (missing, corrupt,
      or unsupported format).
  """
  bgr_image = cv2.imread(str(image_path))
  if bgr_image is None:
    raise ImageReadError(
        f"OpenCV could not read the image at {image_path!s}. The file may be "
        "missing, corrupt, or in an unsupported format."
    )
  height, width = bgr_image.shape[:2]
  new_width, new_height = _compute_resized_dimensions(
      width=width, height=height, max_short_side=max_short_side
  )
  if (new_width, new_height) != (width, height):
    bgr_resized = cv2.resize(
        bgr_image,
        (new_width, new_height),
        interpolation=cv2.INTER_LANCZOS4,
    )
  else:
    bgr_resized = bgr_image
  rgb_resized = cv2.cvtColor(bgr_resized, cv2.COLOR_BGR2RGB)
  return Image.fromarray(rgb_resized)

View on GitHub (pinned to e006f5f0d5)