0

我当前使用 opencv 录制视频的模式看起来像这样

import cv    

capture=cv.CaptureFromCAM(self.deviceID)
img_size=cv.GetSize(cv.QueryFrame(capture))

#create a writer object for the video
writer=cv.CreateVideoWriter(
        filename='temp.avi',
        fourcc=cv.CV_FOURCC('M','J','P','G'),
        fps=15,
        frame_size=img_size,
        is_color=1)

while True:
    #grab the frame
    img=cv.QueryFrame(capture)

    #write frame to video
    cv.WriteFrame(writer,img)


    #if 'Enter' or 'Esc' key is pressed, end video capture
    c = cv.WaitKey(7) % 0x100
    if c == 27 or c == 10:
        break

问题是我必须提前手动设置每秒帧数。我已经将图像存储在一个列表中并跟踪已经过去了多少时间,但这只是导出一个没有帧的文件:

import cv
import time

capture=cv.CaptureFromCAM(self.deviceID)
img_size=cv.GetSize(cv.QueryFrame(capture))

imgList=[]

zero=time.time()
while True:
    #grab the frame
    img=cv.QueryFrame(capture)

    imgList.append(img)

    cv.ShowImage("Window1",img)

    #if 'Enter' or 'Esc' key is pressed, end video capture
    c = cv.WaitKey(7) % 0x100
    if c == 27 or c == 10:
        break
duration=time.time()-zero


framesPerSecond=len(imgList)/duration)
writer=cv.CreateVideoWriter(
        filename='temp.avi',
        fourcc=cv.CV_FOURCC('M','J','P','G'),
        fps=framesPerSecond,
        frame_size=img_size,
        is_color=1)

for i in imgList:
    cv.writeFrame(writer,i)

有点卡住。任何关于替代方法的建议或对我现有代码的调整都会非常有帮助。

4

0 回答 0