OpenCV 视频中的播放循环选项 [英] Playback loop option in OpenCV videos
问题描述
我正在尝试为 OpenCV 视频构建一个播放循环选项.我的程序使用 Python 多处理,并有一个按钮通过 queue4
发送 loopswitch
调用来启用或禁用循环选项.我的具体问题是我的视频在最后一帧冻结,我想知道 vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1)
行是否正确使用cv2.VideoCapture.set()
方法,确实应该将视频带回到第 1 帧并重播(我认为应该如此).
I am trying to build a playback loop option for an OpenCV video. My program uses Python multiprocessing, and has a button send loopswitch
calls through queue4
to enable or disable the loop option. My specific problem is that my video freezes on the last frame, and I would like to know if the line vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1)
is a correct use of the cv2.VideoCapture.set()
method, and should indeed take the video back to frame 1 and replay it (as I think it should).
在修改我的代码后,它现在触发了运行时 C++ 错误,但没有给出其他精度.根据这个答案,似乎使用 cv2.VideoCapture.set()
跳转帧之间是越野车.有没有人管过?
After revising my code, it now triggers a runtime C++ error, but no other precisions are given.
According to this answer, it would seem that using cv2.VideoCapture.set()
to jump between frame is buggy. Has anyone managed it anyway?
谢谢,
我的捕获过程代码(queue
和queue2
是进出队列):
My code for the capture process (queue
and queue2
are in and out queues):
def image_capture(queue, con, queue2, queue4):
videopath = con.recv()
vidFile = cv2.VideoCapture(videopath)
fps = vidFile.get(cv2.cv.CV_CAP_PROP_FPS)
waitframe = 1/fps
con.send(waitframe)#sending waitkey duration through pipe to update_image()
loopswitch = False #init for playing video in a loop
while True:
if queue4.empty():
pass
else:
queueval = queue4.get()
if queueval=='loop':
if loopswitch==False:
loopswitch = True
elif loopswitch==True:
loopswitch = False
try:
flag, frame=vidFile.read()
if flag==0:
if loopswitch==False:
queue2.put(None)
break
elif loopswitch==True:
vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1)
continue
else:
queue2.put(frame)
cv2.waitKey(waitframe)
except:
continue
推荐答案
我通过将 vidFile.set (cv2.cv.CV_CAP_PROP_POS_FRAMES, 1)
替换为 vidFile.set(cv2.cv.CV_CAP_PROP_POS_AVI_RATIO, 0)
,虽然这仅适用于 .avi 文件.
I partially solved it by replacing vidFile.set (cv2.cv.CV_CAP_PROP_POS_FRAMES, 1)
by vidFile.set(cv2.cv.CV_CAP_PROP_POS_AVI_RATIO, 0)
, although this works for .avi files only.
这篇关于OpenCV 视频中的播放循环选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!