Python 在opencv中使用倒带

Python 在opencv中使用倒带,python,opencv,Python,Opencv,在opencv中按指定键时是否可以倒带?我有下面的视频,但它没有回放 def run(self, filepath, fps, width, height, monochrome=False): video_file = self.read_file(filepath) previous_frame = None while (video_file.isOpened()): ret, frame = video_file.read()

在opencv中按指定键时是否可以倒带?我有下面的视频,但它没有回放

def run(self, filepath, fps, width, height, monochrome=False):

    video_file = self.read_file(filepath)
    previous_frame = None


    while (video_file.isOpened()):
        ret, frame = video_file.read()

        if previous_frame is not None:
            pass

        previous_frame = frame

        if ret:

            frame = self.color(frame, monochrome)

            final_frame = self.resolution(frame, width, height)

            delaytime = self.frame_per_second(fps)


            cv2.imshow('frame', final_frame)
            key = cv2.waitKey(delaytime)

            if key & 0xFF == ord("p"):
                cv2.waitKey(234320)
                if key & 0xFF == ord("r"):
                    cv2.set(cv2.CV_CAP_PROP_POS_FRAMES(2, previous_frame))


    video_file.release()
    cv2.destroyAllWindows()

上面的代码将其移到上一帧,但不播放。

理想的解决方案是设置暂停视频时需要播放的帧数,以实现回放效果。这可以通过以下方式实现:

cv2.VideoCapture.set(propId,值)

CAP_PROP_POS_FRAMES基于0的下一个要解码/捕获的帧索引

有关有用的示例,请访问“”

这是一个源代码示例,它暂停视频,并提供一些控制,让您回放到上一帧或跳转到零帧,然后重新启动视频:

import cv2
import sys

# load input video
cap = cv2.VideoCapture('TheMandalorian.mkv')
if (cap.isOpened() == False):
    print("!!! Failed cap.isOpened()")
    sys.exit(-1)

# retrieve the total number of frames
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

# loop to read every frame of the video
while (cap.isOpened()):

    # capture a frame
    ret, frame = cap.read()
    if ret == False:
        print("!!! Failed cap.read()")
        break

    cv2.imshow('video', frame)

    # check if 'p' was pressed and wait for a 'b' press
    key = cv2.waitKey(int(frame_count/1000))
    if (key & 0xFF == ord('p')):

        # sleep here until a valid key is pressed
        while (True):
            key = cv2.waitKey(0)

            # check if 'p' is pressed and resume playing
            if (key & 0xFF == ord('p')):
                break

            # check if 'b' is pressed and rewind video to the previous frame, but do not play
            if (key & 0xFF == ord('b')):
                cur_frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES)
                print('* At frame #' + str(cur_frame_number))

                prev_frame = cur_frame_number
                if (cur_frame_number > 1):
                    prev_frame -= 1

                print('* Rewind to frame #' + str(prev_frame))
                cap.set(cv2.CAP_PROP_POS_FRAMES, prev_frame)

            # check if 'r' is pressed and rewind video to frame 0, then resume playing
            if (key & 0xFF == ord('r')):
                cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
                break

    # exit when 'q' is pressed to quit
    elif (key & 0xFF == ord('q')):
        break

# release resources
cap.release()
cv2.destroyAllWindows()
按键选项:

  • q
    退出应用程序
  • p
    暂停
  • 暂停后,再次按
    p
    继续播放
  • 暂停时,按
    b
    倒带单个帧。您必须按
    p
    重新开始播放
  • 暂停时,按
    r
    将回放到第0帧并自动恢复播放

如果只需要上一帧,可以将其存储在临时数组中。 例如:

prev_frame = curr_image.copy()

嘿我试图这么做,但正如你所看到的,我指定了“p”来暂停视频。当视频暂停时,我尝试按“b”返回一帧,它就会再次开始播放。我认为“b”不起作用。不知何故,他无法达到那种效果。你认为在另一个“如果”中使用“如果”循环怎么样?还有其他方法吗?我想您可能缺少一个额外的
waitKey()
来捕获
b
按键。