How to intercept videos at specified intervals and save pictures?

In deep learning ,it is often necessary to intercept photos from video streams for training models , so how to do it?

opencvpython
2 votesavatar15.00
1 Answers
avatar15.00
2

There are certainly many ways to do that, Here is a way to use OpenCV:

  This function realizes the function of intercepting video frames according to a certain distance and saving them as pictures, and calculates the time spent and the total number of pictures retained.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import os
import time

def Video2photo():
    vidcap = cv2.VideoCapture(video_path)
    # judge whether it opens properly
    if vidcap.isOpened():
        rval, image = vidcap.read()
    else:
        rval = False

    # 
    time_F = 1
    count = 1
    # video frame count interval frequency
    while rval:
        rval, image = vidcap.read()
        # storage operations per time_F frame
        if (count % time_F == 0):
            # store as an image
            cv2.imwrite(photo_path + str(count) + '.jpg', image)
        count = count + 1
        cv2.waitKey(1)
    vidcap.release()



if __name__ == '__main__':
    start_time = time.time()
    # read video file
    video_path = 'video/True_video.mp4'
    photo_path = 'photo/'
    res = os.path.isdir(photo_path)
    if not res:
        os.mkdir(photo_path)
    Video2photo()
    stop_time = time.time()
    print("all convert time is %s"%(stop_time-start_time))
    PhotoNumber = len(os.listdir(photo_path))
    print("There are %s photos in total"%PhotoNumber)
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?