如何使用python和Opencv读取视频文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41441150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:57:55  来源:igfitidea点击:

How to read video files using python & Opencv

pythonopencv

提问by user6745741

I am reading an avi file usinh python 2.7 and opencv2.4.I am using windows 10.My sample code is

我正在阅读一个 avi 文件,使用 python 2.7 和 opencv2.4。我使用的是 windows 10。我的示例代码是

import numpy as np
import cv2
cap = cv2.VideoCapture('videos/wa.avi')
while(cap.isOpened()):
  ret, frame = cap.read()
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  cv2.imshow('frame',gray)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

When i run video is shown But the program ends with no Error

当我运行视频时显示但程序结束时没有错误

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ..\..\..\..\opencv\modules\highgui\src\window.cpp, line 261
Traceback (most recent call last):
File "C:/Users/Emmanu/PycharmProjects/VideoEventDetection/test.py", line 11, in <module>
cv2.imshow('frame',frame)
cv2.error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow

What i am doing wrong?How can i correct it?

我做错了什么?我该如何纠正?

采纳答案by abggcv

The problem is in this line:

问题出在这一行:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

This line expects frame to be a 3 channel or 4 channel Mat object but instead it got some empty Mat and that is why you are getting this assertion failed. You need to check if the frame exists in video and need to handle end of video properly.

这一行期望 frame 是一个 3 通道或 4 通道 Mat 对象,但它有一些空的 Mat ,这就是为什么你得到这个断言失败的原因。您需要检查视频中是否存在该帧并需要正确处理视频结尾。

cap.isOpened()will just check if the video file can be opened for reading but it will not return a false when end of video file is reached.

cap.isOpened()只会检查是否可以打开视频文件进行阅读,但在到达视频文件末尾时不会返回 false。

Try this

试试这个

回答by Rahul K P

When you put cap.isOpened()it check that, the video is read properly, So the whileloop is not working there.

当你cap.isOpened()检查它时,视频被正确读取,所以while循环在那里不起作用。

But when you changed to while Trueit will execute without proper reading that's why it's giving an error.

但是当你改变while True它时,它会在没有正确读取的情况下执行,这就是它给出错误的原因。

Make sure than you are properly reading the video file.

确保您正确阅读视频文件。

回答by ppasler

You took the tutorial from here: Playing Video from file

您从这里获取了教程:从文件播放视频

This question adresses your error: open cv error: (-215) scn == 3 || scn == 4 in function cvtColor

这个问题解决了你的错误:open cv error: (-215) scn == 3 || 函数 cvtColor 中的 scn == 4

Two things may work:

两件事可能有效:

  • Make sure the video is found!
  • And try to use cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  • 确保找到视频!
  • 并尝试使用 cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

回答by Pierre C.

Well I am guessing the capture is not open. That's why your program ends instantly when you use while(cap.isOpened()):.

好吧,我猜捕获未打开。这就是为什么当您使用while(cap.isOpened()):.

As stated in this docit happens that the capture is not implicitly opened when created.

如本文档所述,捕获在创建时并未隐式打开。

Sometimes, cap may not have initialized the capture. In that case, this code shows error. You can check whether it is initialized or not by the method cap.isOpened(). If it is True, OK. Otherwise open it using cap.open().

有时, cap 可能没有初始化捕获。在这种情况下,此代码显示错误。您可以通过 cap.isOpened() 方法检查它是否已初始化。如果是真的,好的。否则使用 cap.open() 打开它。

Try to explicitly open the capture like so :

尝试像这样显式打开捕获:

cap = cv2.VideoCapture('videos/wa.avi')
cap.open();
while(cap.isOpened()):
...

If this does not work you will have to check for the video file path.

如果这不起作用,您将不得不检查视频文件路径。