播放视频后,OpenCV / Python崩溃

时间:2019-01-09 06:23:30

标签: python opencv

我将通过教程来学习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()

这是普通的原始视频显示代码,在视频播放结束后出现此错误:

  

回溯(最近通话最近):文件   “ C:/Users/wg/python/video-test.py”,第15行   cv2.imshow('视频',帧)cv2.error:OpenCV(3.4.3)   C:\ projects \ opencv-python \ opencv \ modules \ highgui \ src \ window.cpp:356:   错误:(-215:声明失败)size.width> 0 && size.height> 0 in   函数'cv :: imshow'

环境如下:
Windows 7专业版
Python 3.6.5
OpenCV 3.4.3

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

试一试:

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()