opencv - OpenCV重播保存的流并对其进行处理

标签 opencv

我的代码处理一个框架,需要花费几秒钟的时间来执行。如果我从相机流式传输,我自然会丢帧并每隔几秒钟获得一帧,对吗?我想在重放视频文件时模拟同样的事情。

通常,当您调用vidcap.read()时,您将获得视频中的下一帧。这实质上会减慢视频的播放速度,并且不会丢失任何帧。这不像处理实时摄像机流。有没有办法在处理过程中(例如处理摄像机流时)处理视频文件和丢帧?

我想到的解决方案是自己跟踪时间,并在每个vidcap.set(cv2.CAP_PROP_POS_MSEC, currentTime)之前调用vidcap.read()。这是我应该怎么做,还是有更好的方法?

最佳答案

一种方法是跟踪处理时间并跳过该数量的帧:

import cv2, time, math
# Capture saved video that is used as a stand-in for webcam
cap = cv2.VideoCapture('/home/stephen/Desktop/source_vids/ss(6,6)_id_146.MP4')
# Get the frames per second of the source video
fps = 120
# Iterate through video
while True:
    # Record your start time for the frame
    start = time.time()
    # Read the frame
    _, img = cap.read()
    # Show the image
    cv2.imshow('img', img)
    # What ever processing that is going to slow things down should go here
    k = cv2.waitKey(0)
    if k == 27: break
    # Calculate the time it took to process this frame
    total = time.time() - start
    # Print out how many frames to skip
    print(total*fps)
    # Skip the frames
    for skip_frame in range(int(total*fps)):  _, _ = cap.read()
cv2.destroyAllWindows()

这可能总比没有好,但是它不能正确模拟丢帧的方式。似乎在处理期间,网络摄像头数据已写入缓冲区(直到缓冲区填满)。更好的方法是使用虚拟过程捕获视频。此处理器密集的虚拟过程将导致丢帧:
import cv2, time, math
# Capture webcam
cap = cv2.VideoCapture(0)
# Create video writer
vid_writer = cv2.VideoWriter('/home/stephen/Desktop/drop_frames.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30, (640,480))
# Iterate through video
while True:
    # Read the frame
    _, img = cap.read()
    # Show the image
    cv2.imshow('img', img)
    k = cv2.waitKey(1)
    if k == 27: break
    # Do some processing to simulate your program
    for x in range(400):
        for y in range(40):
            for i in range(2):
                dummy = math.sqrt(i+img[x,y][0])
    # Write the video frame
    vid_writer.write(img)
cap.release()
cv2.destroyAllWindows()

关于opencv - OpenCV重播保存的流并对其进行处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55384508/

相关文章:

c++ - 将 numpy 的数组 reshape 转换为等效的 OpenCV

c++ - cvGet2D 替代方案

c++ - 如何在 OpenCV 中用零填充矩阵?

opencv - 设置cv::Mat类型而不设置矩阵大小

c++ - opencv 和 visual studio 2010 无法从子目录加载图像

opencv - 使用 OPENCV 拍摄图像之前的自动对焦网络摄像头

python - 使用 houghCircles (OpenCV) 检测小圆圈

python - 使用Opencv创建幻灯片

c++ - 用于 float 的 openCV fincontours

image-processing - OpenCV 中的图像边缘/形状检测