【问题标题】:Python script showing error: (-215:Assertion failed) when running through cmdPython 脚本显示错误:(-215:Assertion failed) 通过 cmd 运行时
【发布时间】:2020-11-18 09:58:03
【问题描述】:

我正在使用 python 学习 OpenCV。我正在尝试运行以下代码以使用 OpenCV 捕获快照。我能够通过 Anaconda spyder 正确运行脚本,但是在使用命令提示符时出现错误。我提到了关于stackoverflow的问题Assertion failure : size.width>0 && size.height>0 in function imshow 但是,在这里我没有加载任何外部图像。 我的代码是:

import cv2

videoObject = cv2.VideoCapture(0)  #0== integrated webcam 1==External webcam
i=0
while True:
    check, frame = videoObject.read()
    cv2.imshow("Webcam Shot",frame)
    key = cv2.waitKey(1)
    # 'c' button to capture the image
    # the 'q' button is set as the quit
    if key == ord('c'):
        cv2.imwrite('Image_'+str(i)+'.png', frame)
        i+=1
        print('Image saved')
    if key == ord('q'):
        break

# shutdown the camera
videoObject.release()
cv2.destroyAllWindows()

我得到的错误:

cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

请帮忙。

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

    来自教程链接:

    有时,cap 可能没有初始化捕获。在这种情况下, 此代码显示错误。您可以检查它是否已初始化 通过方法 cap.isOpened()。如果是真的,好的。否则打开 使用 cap.open()。

    cap.read() 返回一个布尔值(真/假)。如果帧被正确读取,它将 是真的。所以你可以通过检查这个返回值来检查视频的结束。

    从上面看,这是在调用 imshow 之前使用读取结果的代码。

    import cv2
    videoObject = cv2.VideoCapture(0)  #0== integrated webcam 1==External webcam
    i=0
    while True:
        check, frame = videoObject.read()
        if not check:
          continue
        cv2.imshow("Webcam Shot",frame)
        ...
    

    【讨论】:

    • 我试过但得到同样的错误甚至警告:[WARN:1] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (386) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -1072875772 [WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\ src\cap_msmf.cpp (906) CvCapture_MSMF::grabFrame videoio(MSMF): 无法抓取帧。错误:-1072875772
    猜你喜欢
    • 2021-04-09
    • 2019-11-12
    • 2021-11-02
    • 2019-06-26
    • 2020-09-17
    • 2019-12-07
    • 2020-10-13
    • 2021-04-14
    • 2021-04-19
    相关资源
    最近更新 更多