Issue with displaying live stream video using cv2.imshow (error :-215)


I am currently working on a project where I am using OpenCV to capture frames from a video stream and display them using cv2.imshow. However, I am encountering an issue, and I would appreciate your assistance in resolving it.

The error message I’m facing is:

“(-215:assertion failed) size.width>0 && size.height>0 in function ‘cv::imshow’”

I have included the relevant code snippet below:

import cv2


# Open the video capture device (0 represents the default camera)
cap = cv2.VideoCapture(r"http://6*.2**.**1.**8/nphMotionJpeg?Resolution=320x240&Quality=Standard")

# Check if the camera opened successfully
    if not cap.isOpened():
        raise Exception("Error: Could not open camera.")
except Exception as e:

# Set color options (you can choose between cv2.COLOR_BGR2RGB or cv2.COLOR_BGR2GRAY)
color_option = cv2.COLOR_BGR2RGB  # Example: convert to RGB color space

# Loop to continuously capture and display frames
while True:
    # Read a frame from the camera
        ret, frame =

        # Check if the frame was read successfully
        if not ret:
            raise Exception("Error: Could not read frame.")

        # Check if the frame dimensions are valid
        if frame.shape[0] > 0 and frame.shape[1] > 0:
            # Apply color option if needed
            if color_option:
                frame = cv2.cvtColor(frame, color_option)

            # Display the captured frame
            cv2.imshow("Video Capture", frame)

            # Save the frame as an image file (example: JPEG format)
            # Change the file extension and parameters as needed
            #cv2.imwrite("captured_frame.jpg", frame)

            raise Exception("Error: Invalid frame dimensions.")

    except Exception as e:
        # If an error occurs, you can choose to continue or break the loop

    # Break the loop if 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):

# Release the video capture object and close the OpenCV window

After 2-3 min screen look like

I have implemented checks to ensure that the frame is read successfully and that its dimensions are valid before attempting to display it. Despite this, I am still encountering the assertion error mentioned above.

Any other way to ignore the frame which is unreadable.

Thank you in advance for your time and assistance.

Best regards,
Nipesh J.

After the size check you call cvtColor. That might fail and give you an empty frane

I agree with you, but I have removed ‘cvtColor’ and only checked the size, yet I still get the same error.

this defies logic.

please print frame.shape before every imshow() call. look at the last value before the exception happens.