我将通过教程学习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()
这是普通的 Vanilla 视频显示代码,视频播放完后出现此错误:
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
任何帮助是极大的赞赏。谢谢!
最佳答案
试一试:
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()
关于python - 播放视频后,OpenCV/Python崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54104304/