Assertion failure : size.width>0 && size.height>0 in function imshow

前端 未结 10 1248
失恋的感觉
失恋的感觉 2020-11-30 12:19

i am using opencv2 and python on raspberry pi. and i am new with python and opencv. i tried to read a jpeg image and display image it shows the following error:

<         


        
10条回答
  •  抹茶落季
    2020-11-30 13:07

    I am also getting a similar error, so instead of opening a new question, I thought maybe it would be a good idea to gather it all here since there's already some helpful answers...

    My code (textbook code to open a video using OpenCV in Python):

    import cv2 as cv
    import os
    
    path = 'C:/Users/username/Google Drive/Master/THESIS/uva_nemo_db/videos/'
    os.chdir(path)
    video_file = '001_deliberate_smile_2.mp4'
    cap = cv.VideoCapture(video_file) 
    
    
    if not cap.isOpened():
        print("Error opening Video File.")
    
    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
        cv.imshow('frame',frame)
    
        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    
        # if frame is read correctly, ret is True
        if not ret:
            print("Can't retrieve frame - stream may have ended. Exiting..")
            break
    
    # When everything done, release the capture
    cap.release()
    cv.destroyAllWindows()
    

    The reason why I am dumbfounded is that I am getting the same error - BUT - the video is actually played... When running the code the Python interpreter opens up an instance of Python running the video. Once the video ends, it breaks out of the loop, closes the video and throws the error:

    Traceback (most recent call last): File "C:/Users/username/Documents/smile-main/video-testing.py", line 24, in cv.imshow('frame',frame) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wwma2wne\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

    I'd appreciate any input!

    **

    EDIT: How I fixed my error!

    **

    I encased my code in a try/except like this:

    # Import required libraries
    import cv2 as cv
    import os
    
    path = 'C:/Users/username/Google Drive/Master/THESIS/uva_nemo_db/videos/'
    # test_path = 'C:/Users/username/Downloads'
    os.chdir(path)
    os.getcwd()
    video_file = '001_deliberate_smile_2.mp4'
    
    cap = cv.VideoCapture(video_file) #cap for "Video Capture Object"
       
    
    if not cap.isOpened():
        print("Error opening Video File.")
    try:
        while True:
            # Capture frame-by-frame
            ret, frame = cap.read()
            cv.imshow('frame',frame)
    
            if cv.waitKey(1) & 0xFF == ord('q'):
                break
    
            # if frame is read correctly, ret is True
            if not ret:
                print("Can't retrieve frame - stream may have ended. Exiting..")
                break
    except:
        print("Video has ended.")
    
    
    # When everything done, release the capture
    cap.release()
    cv.destroyAllWindows()
    

    I'd still appreciate any input on why this error popped up even though the video played fine, and why the try/except eliminated it.

    Thank you!

提交回复
热议问题