首页 > 解决方案 > 播放视频后 OpenCV/Python 崩溃

问题描述

我将通过教程学习 OpenCV。我有一个问题。当我运行此代码时:

import cv2
cap = cv2.VideoCapture('C:\Users\wg\174037210.avi')
while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('Video', frame)
    if cv2.waitKey(75) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

这是普通的香草视频显示代码,视频结束后我收到此错误:

Traceback(最近一次通话最后):文件“C:/Users/wg/python/video-test.py”,第 15 行,在 cv2.imshow('Video', frame) cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow '

环境如下:
Windows 7 Professional
Python 3.6.5
OpenCV 3.4.3

任何帮助是极大的赞赏。谢谢!

标签: pythonopencv

解决方案


试一试:

import cv2

video = cv2.VideoCapture(filePath)   
frames_counter = 1

while True:
    frames_counter = frames_counter + 1
    check, frame = video.read()
    # print(frame)
    # print(check)
    if check:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow("Capturing", gray)
        key = cv2.waitKey(1)
    else:
        break

print("Number of frames in the video: ", frames_counter)
video.release()
cv2.destroyAllWindows()

推荐阅读