gpt4 book ai didi

python - Opencv2 : Python: cv2. VideoWriter

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:35 25 4
gpt4 key购买 nike

为什么下面的代码没有保存视频?另外,网络摄像头的帧速率是否必须与 VideoWriter 帧大小完全匹配?

import numpy as np
import cv2
import time

def videoaufzeichnung(video_wdth, video_hight, video_fps, seconds):
cap = cv2.VideoCapture(6)
cap.set(3, video_wdth) # wdth
cap.set(4, video_hight) #hight
cap.set(5, video_fps) #hight

# Define the codec and create VideoWriter object
fps = cap.get(5)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, video_fps, (video_wdth, video_hight))
#out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

start = time.time()
zeitdauer = 0
while(zeitdauer < seconds):
end = time.time()
zeitdauer = end - start
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame, 180)
# write the flipped frame
out.write(frame)

cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

videoaufzeichnung.videoaufzeichnung(1024, 720, 10, 30)

最佳答案

我怀疑您正在使用 libv4l 版本的 OpenCV 进行视频 I/O。 OpenCV 的 libv4l API 中存在一个错误,该错误会阻止 VideoCapture::set 方法更改视频分辨率。查看链接 1 , 23 .如果您执行以下操作:

...
frame = cv2.flip(frame,180)
print(frame.shape[:2] # check to see frame size
out.write(frame)
...

您会注意到帧大小没有被修改以匹配函数参数中提供的分辨率。克服此限制的一种方法是手动调整框架大小以匹配分辨率参数。

...
frame = cv2.flip(frame,180)
frame = cv2.resize(frame,(video_wdth,video_hight)) # manually resize frame
print(frame.shape[:2] # check to see frame size
out.write(frame)
...

关于python - Opencv2 : Python: cv2. VideoWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com