-215 opencv assertion_error ai_generated true

cv::error: (-215:Assertion failed) !blob.empty() in function 'cv::dnn::blobFromImage'

ID: opencv/dnn-blob-from-image-empty

Also available as: JSON · Markdown · 中文
90%Fix Rate
83%Confidence
1Evidence
2023-11-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

blobFromImage failed because the input image is empty (not loaded or corrupted), or the preprocessing parameters (e.g., size or mean subtraction) caused an empty blob.

generic

中文

blobFromImage 失败,因为输入图像为空(未加载或损坏),或者预处理参数(如尺寸或均值减法)导致 blob 为空。

Official Documentation

https://docs.opencv.org/4.x/d6/d0f/group__dnn.html#ga29f34df9376379a603acd8df581ac8d7

Workarounds

  1. 95% success Verify image loading: img = cv2.imread('path'); if img is None: raise ValueError('Image not loaded'). Then check img.shape before calling blobFromImage.
    Verify image loading: img = cv2.imread('path'); if img is None: raise ValueError('Image not loaded'). Then check img.shape before calling blobFromImage.
  2. 90% success Ensure the blob size matches the model input. Example: blob = cv2.dnn.blobFromImage(img, 1.0, (224, 224), (104, 117, 123), swapRB=True, crop=False). Print blob.shape to confirm non-empty.
    Ensure the blob size matches the model input. Example: blob = cv2.dnn.blobFromImage(img, 1.0, (224, 224), (104, 117, 123), swapRB=True, crop=False). Print blob.shape to confirm non-empty.
  3. 85% success If using swapRB=True, ensure the image is in BGR order (default for imread). If the image is already RGB, set swapRB=False to avoid channel mismatch.
    If using swapRB=True, ensure the image is in BGR order (default for imread). If the image is already RGB, set swapRB=False to avoid channel mismatch.

中文步骤

  1. 验证图像加载:img = cv2.imread('path'); if img is None: raise ValueError('Image not loaded')。然后在调用 blobFromImage 前检查 img.shape。
  2. 确保 blob 尺寸匹配模型输入。示例:blob = cv2.dnn.blobFromImage(img, 1.0, (224, 224), (104, 117, 123), swapRB=True, crop=False)。打印 blob.shape 确认非空。
  3. 如果使用 swapRB=True,确保图像是 BGR 顺序(imread 默认)。如果图像已经是 RGB,设置 swapRB=False 以避免通道不匹配。

Dead Ends

Common approaches that don't work:

  1. 35% fail

    Changing the mean subtraction values arbitrarily without checking if the image was loaded correctly

  2. 25% fail

    Assuming the error is from the model architecture and re-downloading the model files

  3. 20% fail

    Increasing the blob size to a large value like (1, 3, 1000, 1000) hoping it will bypass the empty check