0

我正在尝试在 Ubuntu 16.4 上使用 Python 中的 OpenCV 录制视频。我正在使用 Logitec 920 相机。我的问题是,当我改变房间照明(从白光到红光)或改变帧速率时,录制视频的持续时间会根据情况而变快或变慢。这是我正在使用的代码:

import numpy as np
import cv2
import time     

cam = cv2.VideoCapture(0) # select the camera

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID') # (*'XVID')
out = cv2.VideoWriter('teste6.avi',fourcc, 30.0, (640,480))

# Start time in seconds
t0 = time.time()

while (cam.isOpened()):
    ret, frame=cam.read() # read frames
    if ret == True:

    out.write(frame) 
    cv2.imshow('video',frame) # plot frames       

    t1 = time.time() # Current time
    dur = t1-t0; # diff time

    if dur > 60:
        out.release() # Stop video recording  
        print('end')

    if cv2.waitKey(1) & 0xFF== ord('q'): # close window pressing 'q' key
        break        

cam.release()
cv2.destroyAllWindows()

有什么帮助吗?

4

1 回答 1

0

乍一看,你不应该在你的记录时间中加入写作/输出。您假设写入每一帧和 imshow 总是会在相同的时间内返回。如果您想要一致的读取持续时间,您应该至少缓存您的读取帧,然后在完成读取后将它们写出来。如果要同时显示,则必须进行更复杂的线程冒险。

于 2018-04-05T20:21:47.750 回答