gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:31:33 31 4
gpt4 key购买 nike

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

通常,当您调用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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com