OpenCV视频中的回放循环选项

2024-09-27 07:19:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为OpenCV视频构建一个回放循环选项。我的程序使用Python多处理,并有一个按钮通过queue4发送loopswitch调用来启用或禁用循环选项。我的具体问题是,我的视频冻结在最后一帧,我想知道行vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1)是否正确使用了cv2.VideoCapture.set()方法,并且确实应该将视频带回到第1帧并重新播放(我认为应该)。

编辑

修改了我的代码后,现在触发了运行时C++错误,但是没有给出其他的精确性。 根据this answer,使用cv2.VideoCapture.set()在帧之间跳转似乎是错误的。有人管理过吗?

谢谢你

我的捕获进程代码(queuequeue2在进出队列中):

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

Tags: falsetrue视频ifcv2cvcapset
3条回答

我通过将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

import numpy as np
import cv2
cap = cv2.VideoCapture('intro.mp4')
while(cap.isOpened()):

    ret, frame = cap.read() 
    #cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
    #cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

    if ret:
        cv2.imshow("Image", frame)
    else:
       print('no video')
       cap.set(cv2.CAP_PROP_POS_FRAMES, 0)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

当帧数达到cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)时,我可以使用if语句获得循环视频播放,然后将帧数和cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num)重置为相同的值。下面的例子一直在为我循环视频。

import cv2

cap = cv2.VideoCapture('path/to/video') 
frame_counter = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame_counter += 1
    #If the last frame is reached, reset the capture and the frame_counter
    if frame_counter == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        frame_counter = 0 #Or whatever as long as it is the same as next line
        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 0)
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

它还可以重新捕获视频,而不是重置帧数:

if frame_counter == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
    frame_counter = 0
    cap = cv2.VideoCapture(video_name)

所以至少我可以用cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num)循环视频。如果重置为第零帧而不是第一帧(如avi方法),会发生什么情况?

相关问题 更多 >

    热门问题