【问题标题】:cv2.VideoWriter in RPi3 is faster than actualRPi3 中的 cv2.VideoWriter 比实际更快
【发布时间】:2018-12-08 09:53:25
【问题描述】:

我正在尝试录制 Logitech 网络摄像头视频。凸轮能够记录它,但 40 秒的视频仅以 nX 速度记录 6 秒。我参考了以下link 的解决方案,但它无法解决 RPi 中的问题。重要的是代码可以在 Ubuntu 桌面上找到,但可能是 RPi 处理速度较慢。

这是我的代码 sn-p:

fourcc = cv2.cv.CV_FOURCC(*'XVID')
videoOut = cv2.VideoWriter("video_clip.avi", fourcc, 20.0, (640, 480))
start_time = time.time()
frame_count = 0
while True:
    ret, frame = cap.read()
    videoOut.write(frame)  # write each frame to make video clip
    frame_count += 1

    print int(time.time()-start_time)  # print the seconds
    if int(time.time()-start_time) == 10:
        videoOut.release()
        break
        # get out of loop after 10 sec video
print 'frame count =', frame_count 
# gives me 84 but expected is 20.0 * 10 = 200

【问题讨论】:

  • 您是否在桌面和 RPI 上使用相同的相机?
  • 我不明白:是不是录了40秒但是播放太快把40秒缩成6秒?还是在 6 秒后停止录制?你相机的帧率是多少?你能在有和没有编码/写入的情况下测量 x 秒捕获的帧速率以发现瓶颈吗?
  • 可能 rpi 编码 xvid 的速度不够快。
  • @zindarod 是的,两个摄像头都与罗技 c270 相同
  • @Micka 播放速度太快,40 秒缩短为 6 秒,帧率为 20 fps

标签: python opencv raspberry-pi3


【解决方案1】:

前段时间我也有同样的问题。我做了很多搜索,但没有找到解决方案。问题是通过的 fps 是视频将被播放的速率。这并不意味着视频将以该 FPS录制。 AFAIK,没有直接的方法来设置记录的 FPS。如果您记录的 FPS 太高,您可以下采样(即每个时间段仅保留 1 帧)。但从你描述的情况来看,它似乎比要求的要低得多。这是硬件限制,对此无能为力。

关于设置录制的 FPS,我找到了一种解决方法。我在捕获列表中的 所有 帧后创建了 videoWriter。这样我就可以计算出录制的FPS,在创建的时候传给VideoWriter。

【讨论】:

  • 它就像一个魅力。首先制作一个框架列表,然后编写它们。谢谢
【解决方案2】:

如果内存不足,可能无法创建帧列表。另一种方法是动态计算 fps,然后 remux the video with the new fps using ffmpeg

import numpy as np
from skvideo import io
import cv2, sys
import time
import os

if __name__ == '__main__':

    file_name = 'video_clip.avi'

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    videoOut = cv2.VideoWriter(file_name, fourcc, 30.0, (640, 480))
    cap = cv2.VideoCapture(0)

    if not cap.isOpened() or not videoOut.isOpened():
        exit(-1)

    start_time = time.time()
    frame_count = 0
    duration = 0
    while True:
        ret, frame = cap.read()
        if not ret:
            print('empty frame')
            break
        videoOut.write(frame)  # write each frame to make video clip
        frame_count += 1

        duration = time.time()-start_time
        if int(duration) == 10:
            videoOut.release()
            cap.release()
            break

    actualFps = np.ceil(frame_count/duration)

    os.system('ffmpeg -y -i {} -c copy -f h264 tmp.h264'.format(file_name))
    os.system('ffmpeg -y -r {} -i tmp.h264 -c copy {}'.format(actualFps,file_name))

【讨论】:

    猜你喜欢
    • 2018-09-09
    • 2020-10-10
    • 2019-05-10
    • 2021-05-07
    • 2022-01-01
    • 1970-01-01
    • 2017-08-11
    • 2012-03-06
    • 2013-03-13
    相关资源
    最近更新 更多