Error: (-215:Assertion failed) !dsize.empty() in function 'resize'

Hello! I’m trying to use OpenCV’s contouring methods to detect a hand and identify the ASL sign it is portraying (real time ASL-translator). However, when I run this program, I keep getting this error:

error: OpenCV(4.5.3) Error: (-215:Assertion failed) !dsize.empty() in function 'resize'.

Honestly, I am not quite sure what this error means. I’ve seen online that it means that I’m feeding OpenCV an empty image, but I’m feeding it real-time video data. How do I fix this problem?
Here is my code:

import numpy as np
import cv2

background = None
accumulated_weight = 0.5
ROI_top = 100
ROI_bottom = 300
ROI_right = 150
ROI_left = 350
CLASSES = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "space", "t", "u", "v", "w", "x", "y", "z"]

weightsPath = "/home/Downloads/frozen_graph1.pb"

cvNet = cv2.dnn.readNet(weightsPath)

def cal_accum_avg(frame, accumulated_weight):

    global background
    if background is None:
        background = frame.copy().astype("float")
        return None

    cv2.accumulateWeighted(frame, background, accumulated_weight)
def segment_hand(frame, threshold=2):
    global background
    diff = cv2.absdiff(background.astype("uint8"), frame)

    _ , thresholded = cv2.threshold(diff, threshold, 255,
     #Fetching contours in the frame (These contours can be of hand

    contours, hierarchy = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

    # If length of contours list = 0, means we didn't get any
    if len(contours) == 0:
        return None
        # The largest external contour should be the hand
        hand_segment_max_cont = max(contours, key=cv2.contourArea)
        # Returning the hand segment(max contour) and the
        return (thresholded, hand_segment_max_cont)

cam = cv2.VideoCapture(0)
num_frames =0
while True:
    ret, frame =

    # flipping the frame to prevent inverted image of captured
    frame = cv2.flip(frame, 1)

    frame_copy = frame.copy()

    # ROI from the frame
    roi = frame[ROI_top:ROI_bottom, ROI_right:ROI_left]

    gray_frame = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    gray_frame = cv2.GaussianBlur(gray_frame, (9, 9), 0)

    if num_frames < 70:
        cal_accum_avg(gray_frame, accumulated_weight)
        # segmenting the hand region

        hand = segment_hand(gray_frame)
        # Checking if we are able to detect the hand...
        if hand is not None:
            thresholded, hand_segment = hand

            # Drawing contours around hand segment
            cv2.drawContours(frame_copy, [hand_segment + (ROI_right,
      ROI_top)], -1, (255, 0, 0),1)
            cv2.imshow("Thesholded Hand Image", thresholded)
            thresholded = cv2.cvtColor(thresholded,
            thresholded = np.reshape(thresholded,
            b=cv2.dnn.blobFromImage(thresholded, size=(150, 150), swapRB=True, crop=False)
            d = cvNet.forward()
            cv2.putText(frame_copy, CLASSES[gesture_id],
(170, 45), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
    # Draw ROI on frame_copy
    cv2.rectangle(frame_copy, (ROI_left, ROI_top), (ROI_right,
    ROI_bottom), (255,128,0), 3)

    # incrementing the number of frames for tracking
    num_frames += 1

    # Display the frame with segmented hand
    cv2.imshow("Sign Detection", frame_copy)

    # Close windows with Esc
    k = cv2.waitKey(1) & 0xFF

    if k == 27:

# Release the camera and destroy all the windows

You start solving the puzzle by first figuring out the purpose or variable ret in line
ret, frame =

…and how that could help you chck that your frames are good. The apply similar tactic of verifying every function call for success #programming

1 Like

please add the full stacktrace of the error, so we can try to find out, where this is coming from

there is no explicit resize() in the code you show, but e.g. there’s one hidden in imshow(), so check, what you feed into that, please

Traceback (most recent call last):
File "/home/Downloads/", line 95, in <module>
b=cv2.dnn.blobFromImage(thresholded, size=(150, 150), swapRB=True, crop=False)
cv2.error: OpenCV(4.5.3) /tmp/pip-wheel-hwcmjluw/opencv-python_dc56ddd000dd4893b1f852d88d4a5959/opencv/modules/imgproc/src/resize.cpp:3688: error: (-215:Assertion failed) !dsize.empty() in function 'resize'

aand ? thresholded.shape is what ?

however, before we try to fix your current errors – what exactly did you train your network on ? color images ? then you must use the same for prediction, so most probably you should remove all that preprocessing

Tried that earlier with the blobFromImage function, still didn’t work

throw it out. blobFromImage() expects [HWC] input, that’s why the (internal) resize() goes wrong

1 Like

yea, i remember, we had ALL OF THIS before…