播放视频后 OpenCV/Python 崩溃 [英] OpenCV/Python crashes after playing a video
问题描述
我将通过教程来学习 OpenCV.我有一个问题.当我运行此代码时:
I am going thru the tutorials to learn OpenCV. And I have a problem. When I run this code:
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()
这是普通的视频显示代码,我在视频结束后收到此错误:
which is plain vanilla video display code I get this error after the video finishes:
回溯(最近一次调用最后一次):文件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:错误: (-215:Assertion failed) size.width>0 &&尺寸.高度>0 英寸函数 'cv::imshow'
Traceback (most recent call last): File "C:/Users/wg/python/video-test.py", line 15, in 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 专业版
Python 3.6.5
OpenCV 3.4.3
非常感谢任何帮助.谢谢!
Any help is greatly appreciated. Thanks!
推荐答案
试试这个:
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()
这篇关于播放视频后 OpenCV/Python 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!