为什么下面的代码没有保存视频?
另外,网络摄像头的帧速率是否必须与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
用于视频 I/O 的 OpenCV 版本。 OpenCV 中存在一个错误libv4l
阻止的 APIVideoCapture::set
改变视频分辨率的方法。查看链接1 https://stackoverflow.com/questions/16390810/opencv-cant-set-resolution-of-video-capture, 2 http://answers.opencv.org/question/11427/decreasing-capture-resolution-of-webcam/ and 3 http://answers.opencv.org/question/30062/error-setting-resolution-of-video-capture-device/。如果您执行以下操作:
...
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)
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)