-215 opencv assertion_error ai_generated true

cv::error: (-215:断言失败) !blob.empty() 在函数 'cv::dnn::blobFromImage' 中

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

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

其他格式: JSON · Markdown 中文 · English
90%修复率
83%置信度
1证据数
2023-11-05首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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 以避免通道不匹配。

无效尝试

常见但无效的做法:

  1. 35% 失败

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

  2. 25% 失败

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

  3. 20% 失败

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