cv2.VideoWriter 的输出不正确.它更快 [英] The output of cv2.VideoWriter is incorrect. It's faster

查看:207
本文介绍了cv2.VideoWriter 的输出不正确.它更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 opencv 的 cv2.VideoWriter 录制特定时间的视频.问题是输出不正确.例如,10秒的视频只有2秒,而且播放速度更快.这是我的代码.欢迎任何建议或想法.此外,另一个问题是输出视频是静音的.谢谢!!!

I'm trying to use opencv's cv2.VideoWriter to record a video for certain time. The problem is the output is incorrect. For example, the video for 10 seconds only got 2 seconds, and it plays faster like speed up. Here's my code. Any suggestions or ideas are welcome. Also, another problem is that the output video is silence. Thanks !!!

主机:树莓派

语言:Python

import numpy as np
import cv2
import time

# Define the duration (in seconds) of the video capture here
capture_duration = 10

cap = cv2.VideoCapture(0)

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

start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

    else:
        break

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

推荐答案

您忽略了代码中的两个重要因素:

You're ignoring two important factors in your code:

while 循环中的帧数:

您想以每秒 20 帧 (fps) 的速度编写 10 秒的视频.这为整个视频提供了总共 200 帧.为此,您需要注意在捕获每一帧并将其写入文件之前 while 循环内的等待时间.如果忽略等待期,则:

You want to write 10 seconds of video at 20 frames per second (fps). This gives you a total of 200 frames for the whole video. For this to happen, you'd need to be conscious of the waiting time inside the while loop before each frame is captured and written to file. If you ignore the waiting period, then:

  frameCount = 0
  while( int(time.time() - start_time) < capture_duration ):
      # we assume that all the operations inside the loop take 0 seconds to accomplish.

      frameCount = frameCount+1

  print('Total frames: ',frameCount)

在上面的示例中,您会注意到,通过忽略等待时间,您将在 10 秒内将数千帧写入视频文件.现在 20 fps 的 10 秒帧将为您提供 200 帧,要达到此数量的帧,您需要在每帧写入文件之前等待 50 毫秒.

In the above example, you'll notice that by ignoring waiting time, you'd be writing thousands of frames to video file in 10 seconds. Now 10 seconds of frames at 20 fps would give you 200 frames, to achieve this number of frames you'd need 50 milliseconds of waiting period before each frame is written to the file.

  frameCount = 0
  while( int(time.time() - start_time) < capture_duration ):
      # wait 50 milliseconds before each frame is written.
      cv2.waitKey(50)

      frameCount = frameCount+1

  print('Total frames: ',frameCount)

在上述示例中,总帧数约为 200.

The total number of frames would be about 200 in the above example.

VideoCapture::read() 是一个阻塞 I/O 调用:

VideoCapture::read() is a blocking I/O call:

cap.read() 函数执行两个操作,分别是 VideoCapture::grab()VideoCapture::retrieve().该函数等待下一帧被抓取,然后解码并返回图像.等待时间取决于您的相机帧数.

The cap.read() function performs two operations, namely VideoCapture::grab() and VideoCapture::retrieve(). This function waits for next frame to be grabbed, then decodes and returns the image. The waiting period depends on your camera fps.

例如,如果您的相机 fps 为 6,那么在 10 秒内您将捕获 60 帧.您已将 20 fps 设置为 VideoWriter 属性;以 20 fps 播放 60 帧可为您提供大约 3 秒的视频.

So for example if your camera fps is 6, then in 10 secs you'd have captured 60 frames. You've set 20 fps as your VideoWriter property; 60 frames played as 20 fps gives you about 3 seconds of video.

要查看您的相机在 10 秒内捕获了多少帧:

To see how many frames are captured by your camera in 10 seconds:

  frameCount = 0
  while( int(time.time() - start_time) < capture_duration ):
      # wait for camera to grab next frame
      ret, frame = cap.read()
      # count number of frames
      frameCount = frameCount+1

  print('Total frames: ',frameCount)

这篇关于cv2.VideoWriter 的输出不正确.它更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆