我正在尝试以60 FPS的720p录制视频或以30 FPS的1080p录制视频,但是在python上使用C920网络摄像头和OpenCV时,我只能在720p上获得大约10 fps,而在1080p上只能获得5fps。
我为openCV尝试了很多不同的设置,但是都没有改变输出的FPS。
y=676
我希望它能输出60fps,但只能提供9或10fps
答案 0 :(得分:0)
您可以使用cv2.VideoWriter()
。这是函数头
cv2.VideoWriter([filename, fourcc, fps, frameSize[, isColor]])
例如,将FPS设置为30
cv2.VideoWriter('output.avi', self.codec, 30, (frame_width, frame_height))
这是一个小部件,可用于流式传输和录制网络摄像头中的帧。
from threading import Thread
import cv2
class WebcamRecordWidget(object):
def __init__(self, src=0):
# Create a VideoCapture object
self.capture = cv2.VideoCapture(src)
# Default resolutions of the frame are obtained (system dependent)
self.frame_width = int(self.capture.get(3))
self.frame_height = int(self.capture.get(4))
# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc('M','J','P','G')
self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height))
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
def show_frame(self):
# Display frames in main program
if self.status:
cv2.imshow('frame', self.frame)
# Press Q on keyboard to stop recording
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
self.output_video.release()
cv2.destroyAllWindows()
exit(1)
def save_frame(self):
# Save obtained frame into video output file
self.output_video.write(self.frame)
if __name__ == '__main__':
webcam_record_widget = WebcamRecordWidget()
while True:
try:
webcam_record_widget.show_frame()
webcam_record_widget.save_frame()
except AttributeError:
pass
答案 1 :(得分:0)
OpenCV自动选择第一个可用的捕获后端(see here)。可能是它没有自动使用V4L2。
如果从源代码构建,也请同时设置-D WITH_V4L=ON
和-D WITH_LIBV4L=ON
。
可能,OpenCV选择的像素格式不支持所需分辨率的所需帧速率。在Linux上,您可以使用v4l2-ctl --list-formats-ext
和v4l2-ctl --all
查看设置。
要设置要使用的像素格式,请设置捕获的CAP_PROP_FOURCC
属性:
capture = cv2.VideoCapture(cam_id, cv2.CAP_V4L2)
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))