2

When trying to use EAST text detector on some images, with OpenCV in Python on Windows 10, I get the following error:

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'

(Strangely, that path does not exist on my file system)

I started from the excellent tutorial by Adrian Rosebrock. Here is a code snippet (it fails on the last line):

# 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)

I made some modifications, e.g. I recalculate the mean instead of using the hardcoded value shown in the example. I also tried to call blobFromImage without mean (or with the default from the example) and with swapRB=False, but the error keeps occurring.

The problem happens systematically with some files (here's an example), whereas it doesn't with others, on which EAST instead runs smoothly. I cannot identify the characteristics that make an image troublesome, however I am inclined to think that the error is independent of the need to resize the image, since most of the images that can be analyzed without issues must be resized anyway.

I haven't found any documentation specific to the issue and cannot reconstruct the problem easily from the source (which, I guess, is this).

How can I prevent the error?

kr1zz
  • 116
  • 2
  • 8

1 Answers1

1

I faced a similar problem with another application. This has something to do with the input size. I used the following workaround that did not fail so far:

    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))
nkoerb
  • 11
  • 1