Debadri Chowdhury
Debadri Chowdhury

Reputation: 437

Assertation failed image.depth() in blobFromImages

I'm trying to create a blob of multiple frames using blobFromImages in the dnn module.

def batch_process(self, frames):
    blob = cv.dnn.blobFromImages(frames, 1./255, (368, 368), (0, 0, 0), swapRB=False, crop=False)
    self.net.setInput(blob)
    out = self.net.forward()
    detected_points = np.zeros((frames.shape[0], 36))

    for i in range(frames.shape[0]):
        points = np.array([])
        for j in range(18):
            heatMap = out[i, j, :, :]
            _, conf, _, point = cv.minMaxLoc(heatMap)
            if conf > 0.1:
                points = np.append(points, [point[0], point[1]])
            else:
                points = np.append(points, [0, 0])
        detected_points[i] = points

    return detected_points

But when i call the function I get an error like this:-

OpenCV(3.4.1) Error: Assertion failed (image.depth() == 5) in blobFromImages, file /opt/opencv/modules/dnn/src/dnn.cpp

The blobFromImage() works fine on a similar single frame. As far as I understand, blobFromImages() want an array of frames. Hence, I'm passing a numpy array of shape (32, 480, 640, 3) as parameter. Can someone please help me find out what am I missing? I cannot seem to find examples using blobFromImages(). I'd want to use that as it might lessen my processing time than using blobFromImage().

Upvotes: 5

Views: 3575

Answers (1)

Dan Mašek
Dan Mašek

Reputation: 19041

First let's analyze the error message and translate it into something easy to understand.

Assertion failed (image.depth() == 5) in blobFromImages

Since this is coming from the C++ implementation of OpenCV, it is safe to assume that image is an instance of cv::Mat (we could inspect the source code to determine this). The documentation of cv::Mat::depth() says the following:

Returns the depth of a matrix element.

The method returns the identifier of the matrix element depth (the type of each individual channel). For example, for a 16-bit signed element array, the method returns CV_16S. A complete list of matrix types contains the following values:

  • CV_8U - 8-bit unsigned integers ( 0..255 )
  • CV_8S - 8-bit signed integers ( -128..127 )
  • CV_16U - 16-bit unsigned integers ( 0..65535 )
  • CV_16S - 16-bit signed integers ( -32768..32767 )
  • CV_32S - 32-bit signed integers ( -2147483648..2147483647 )
  • CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )
  • CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

OK, so it's the datatype of each individual element in the array. To decipher which datatype the value of 5 represents (hint: the above list is in ascending order and numbering starts at 0), we can refer to the documentation of Core HAL where the values are listed.

#define     CV_32F   5

So, the error message says:

I expected to get array of 32bit floats, but I got something else.


You don't show us how exactly you created frames, but it's safe to assume that it's an array of 8bit unsigned integers. To solve the problem, simply convert it to the right data type -- i.e. make the first argument of cv.dnn.blobFromImages be np.float32(frames).

Upvotes: 7

Related Questions