简体   繁体   English

提取关键帧 | 蟒蛇| OpenCV

[英]Extracting keyframes | Python | Opencv

I am currently working on keyframe extraction from videos.我目前正在从视频中提取关键帧。

Code :代码 :

while success:
    success, currentFrame = vidcap.read()
    isDuplicate = False
    limit = count if count <= 10  else (count - 10)
    for img in xrange(limit, count):
        previusFrame = cv2.imread("%sframe-%d.png" % (outputDir, img))
        try:
            difference = cv2.subtract(currentFrame, previusFrame)
        except:
            pass

This gives me huge amounts of frames.这给了我大量的帧。 Expected ouput: Calculate pixel difference between frames and then compare it with a threshold value and store unique keyframes.预期输出:计算帧之间的像素差异,然后将其与阈值进行比较并存储唯一的关键帧。

Working on videos for the first time.第一次处理视频。 please guide on how to proceed to achieve the expected output请指导如何继续实现预期的输出

Here is a script to extract I-frames with ffprobe and OpenCV:这是一个使用 ffprobe 和 OpenCV 提取 I 帧的脚本:

import os
import cv2
import subprocess

filename = '/home/andriy/Downloads/video.mp4'

def get_frame_types(video_fn):
    command = 'ffprobe -v error -show_entries frame=pict_type -of default=noprint_wrappers=1'.split()
    out = subprocess.check_output(command + [video_fn]).decode()
    frame_types = out.replace('pict_type=','').split()
    return zip(range(len(frame_types)), frame_types)

def save_i_keyframes(video_fn):
    frame_types = get_frame_types(video_fn)
    i_frames = [x[0] for x in frame_types if x[1]=='I']
    if i_frames:
        basename = os.path.splitext(os.path.basename(video_fn))[0]
        cap = cv2.VideoCapture(video_fn)
        for frame_no in i_frames:
            cap.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
            ret, frame = cap.read()
            outname = basename+'_i_frame_'+str(frame_no)+'.jpg'
            cv2.imwrite(outname, frame)
            print ('Saved: '+outname)
        cap.release()
    else:
        print ('No I-frames in '+video_fn)

if __name__ == '__main__':
    save_i_keyframes(filename)

You can change 'I' to 'P' if you need to extract P-frames.如果需要提取 P 帧,可以将'I'更改为'P'

You may use the following code snippet which iterated through all the frames and checks for the difference between the frames as:您可以使用以下代码片段,它遍历所有帧并检查帧之间的差异,如下所示:

import cv2
import numpy as np

video_path = "/Users/anmoluppal/Downloads/SampleVideo_1280x720_1mb.mp4"
p_frame_thresh = 300000 # You may need to adjust this threshold

cap = cv2.VideoCapture(video_path)
# Read the first frame.
ret, prev_frame = cap.read()

while ret:
    ret, curr_frame = cap.read()

    if ret:
        diff = cv2.absdiff(curr_frame, prev_frame)
        non_zero_count = np.count_nonzero(diff)
        if non_zero_count > p_frame_thresh:
            print "Got P-Frame"
        prev_frame = curr_frame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM