python - 播放视频后 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
任何帮助是极大的赞赏。谢谢!
解决方案
试一试:
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()
推荐阅读
- javascript - 上传 csv 文件并将多个标记添加到谷歌地图
- python - 根据名称 pandas python 对某些列进行乘法和求和
- swift - 更改所需属性将字段留空
- reactjs - 服务器端渲染中的 React-router 和 Link
- colors - 使用 Seaborn stripplot 中的一系列值设置色调
- rest - 在 Lagom 中查询参数
- javascript - 获取元素和所有后代/子元素的样式
- java - Spring父项目拥有bean - 孩子可以访问它吗?
- c# - 如果没有对它的引用,任务是否会被垃圾收集中断?
- javascript - Javascript在程序外保存变量