python - EAST文本检测-215:断言失败(OpenCV Python)

标签 python opencv ocr

尝试在Windows 10上的Python中使用OpenCV在某些图像上使用EAST text detector时,出现以下错误:

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:835: error: (-215:Assertion failed) ld.inputBlobs[0]->total() == total(shapes[index]) in function 'cv::dnn::dnn4_v20180917::BlobManager::allocateBlobsForLayer'

(奇怪的是,该路径在我的文件系统上不存在)

我从tutorial从出色的Adrian Rosebrock开始。这是一个代码片段(在最后一行失败):

# sFileName is the path to the image, previously set
oInputImage = cv.imread(sFileName)
aiShape = oInputImage.shape
(iH, iW) = aiShape[:2]
iRequiredUnit = 32

# check if the image height is enough
iHr = iH % iRequiredUnit
iBottom = 0
iHr = iH % iRequiredUnit
if 0 < iHr:
    # calculate how much padding is necessary
    iBottom = iRequiredUnit - iHr

# check if the image width is enough
iRight = 0
iWr = iW % iRequiredUnit
if 0 < iWr:
    # calculate how much padding is necessary
    iRight = iRequiredUnit - iWr

if iBottom > 0 or iRight > 0:
    # add padding to make the image proportions correct
    oImage = cv.copyMakeBorder(
        src=oInputImage,
        top=iTop,
        bottom=iBottom,
        left=iLeft,
        right=iRight,
        borderType=cv.BORDER_CONSTANT,
        value=[0, 0, 0]
        )
    else:
    # no need to add padding
    oImage = oInputImage.copy()

(iH, iW) = oImage.shape[:2])

ib, ig, ir, _ = cv.mean(oImage)
oBlob = cv.dnn.blobFromImage(
        oImage, 1.0, (iW, iH), (ib, ig, ir),
        swapRB=True, crop=False
)

# load the EAST network
# EAST_path initialized appropriately previously
oNet = cv.dnn.readNet(EAST_path)
oNet.setInput(oBlob)
asLayerNames = [
        "feature_fusion/Conv_7/Sigmoid",
        "feature_fusion/concat_3"]
(afScores, aoGeometry) = oNet.forward(asLayerNames)

我做了一些修改,例如我重新计算平均值,而不是使用示例中显示的硬编码值。我也尝试不带平均值(或示例中的默认值)和blobFromImage调用 swapRB=False ,但错误不断发生。

该问题在某些文件(here's an example)中系统地发生,而在其他文件中则没有,EAST可以在其上平稳运行。我无法确定造成图像麻烦的特征,但是我倾向于认为该错误与调整图像大小无关,因为必须能够调整没有问题的大多数图像。

我还没有找到有关该问题的任何文档,也无法从源代码(我想是this)轻松地重构该问题。

如何防止错误?

最佳答案

我在另一个应用程序中遇到了类似的问题。这与输入大小有关。我使用了到目前为止尚未失败的以下解决方法:

    orig_height, orig_width = image.shape[:2]
    while True:
        height, width = image.shape[:2]

        blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(width, height),mean=(104.00698793, 116.66876762, 122.67891434),swapRB=False, crop=False)
        self.net.setInput(blob)
        try:
            prediction = self.net.forward()
            break
        except:
            pass

        if width*height  < 100:
            raise
        image = cv2.resize(image, (int(width*0.9), int(height*0.9)))

    prediction = cv2.resize(prediction[0, 0], (orig_width, orig_height))

关于python - EAST文本检测-215:断言失败(OpenCV Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56026132/

相关文章:

Python:使用 surveymonkey.com 请求的 SSLError

python - 为什么在导入 datetime 类后不能使用此引用?

c++ - OpenCv 填充单 channel 图像的像素并显示

c - 如何将帧复制到帧

c# - emgucv:C#中的Pan Card不当偏斜检测

python - 在 python 中使用 selenium send_keys 复制文本

python - 如何将图像从 scikit-image 转换为 opencv2 和其他库?

c++ - 与 cv::Mat 元素一起存储附加信息

ocr - 向 Tesseract 3 添加新字体

javascript - ImageCapture API 的替代方案以获得更好的浏览器支持