cv2.VideoWriter: Asks for a tuple as Size argument, then rejects it

邮差的信 提交于 2020-06-25 17:28:17

问题


I'm using OpenCV 4.0 and Python 3.7 to create a timelapse video.

When constructing a VideoWriter object, the documentation says the Size argument should be a tuple.

When I give it a tuple it rejects it. When I try to replace it with something else, it won't accept it because it says the argument isn't a tuple.

When Size not a tuple:

out = cv2.VideoWriter('project.avi', 1482049860, 30, height, width)
SystemError: new style getargs format but argument is not a tuple

When I changed Size to a tuple:

out = cv2.VideoWriter('project.avi', 1482049860, 30, (height, width))
TypeError: must be real number, not tuple

I just want to create a simple cv2.VideoWriter object.


回答1:


I was trying to take the height and width from an uploaded video using:

width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

I changed it to:

width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

Now I don't get that error. It doesn't work yet as a whole, but that particular error isn't there anymore.




回答2:


I encountered the same situation, here is my solution:

It seems like cv2.VideoWriter() tend to "drop" the argument that has the wrong type. In my case I used pathlib.Path for the first output file name argument,like:

p=Path('project.avi')

out = cv2.VideoWriter(p, 1482049860, 30, frameSize=(height, width))

But cv2.VideoWriter() only accepts str, so it "dropped" the p, and it received:

out = cv2.VideoWriter(1482049860, 30, frameSize=(height, width))

Then (height, width) is actually at the fps position, it requires real but gets tuple

So I suggest you check every argument to make sure they have the right type.




回答3:


Try updating to opencv v4.1.0. Not seeing that issue there.

Else try:

out = cv2.VideoWriter('project.avi', 1482049860, 30, frameSize=(height, width))


来源:https://stackoverflow.com/questions/56370802/cv2-videowriter-asks-for-a-tuple-as-size-argument-then-rejects-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!