我试图为OpenCV视频构建一个回放循环选项。我的程序使用Python多处理,并有一个按钮通过queue4
发送loopswitch
调用来启用或禁用循环选项。我的具体问题是,我的视频冻结在最后一帧,我想知道行vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1)
是否正确使用了cv2.VideoCapture.set()
方法,并且确实应该将视频带回到第1帧并重新播放(我认为应该)。
修改了我的代码后,现在触发了运行时C++错误,但是没有给出其他的精确性。
根据this answer,使用cv2.VideoCapture.set()
在帧之间跳转似乎是错误的。有人管理过吗?
谢谢你
我的捕获进程代码(queue
和queue2
在进出队列中):
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文件。用于python3、opencv3.1.0、覆盆子皮3
当帧数达到
cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
时,我可以使用if语句获得循环视频播放,然后将帧数和cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num)
重置为相同的值。下面的例子一直在为我循环视频。它还可以重新捕获视频,而不是重置帧数:
所以至少我可以用
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num)
循环视频。如果重置为第零帧而不是第一帧(如avi方法),会发生什么情况?相关问题 更多 >
编程相关推荐