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()

        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()

上面的代码把它带到上一帧但不播放。

最佳答案

理想的解决方案是设置视频暂停时需要播放的帧数,以达到倒带效果。这可以通过 video capture properties 完成:

cv2.VideoCapture.set(propId, value)

CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.



Jumping between frames in video files ”中提供了有用的示例。

这是一个 源代码示例 暂停视频并提供一些控制,让您倒带到前一帧或跳到第 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 帧并自动恢复播放。
  • 关于python - 在opencv中使用倒带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59964279/

    相关文章:

    Python Flask 强制重新加载缓存的 JS 文件

    python - 导入 sklearn 时出现段错误

    python - 如何在 Python 中隐藏 FFmpeg 的控制台输出?

    c++ - 为什么我似乎无法使用带有 C++ 的 OpenCV 获得与 knnMatch 的任何匹配?

    python - TensorFlow 优化器中的 _get_hyper 和 _set_hyper 是什么?

    python - 如何使用Python用曲线生成x和n之间的数字?

    opencv - 使用单相机进行标记位姿估计的错误

    java - 在openCV Android中为ListOfPoints的每个元素添加常量

    opencv - 如何对相机拍摄坐标中的点进行反畸变,得到相应的反畸变图像坐标?

    python - 使用python opencv跟踪白色