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

查看:6633
本文介绍了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 !!!

主持人:Raspberry Pi

Host: Raspberry Pi

语言: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 ::检索()。此功能等待抓取下一帧,然后解码并返回图像。等待时间取决于您的相机fps

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天全站免登陆