python Video Picture Capture by Frame Tool

  • 2021-07-26 08:17:09
  • OfStack

In this paper, we share the specific code of python video interception tool by frame for your reference. The specific contents are as follows

Description: Capture a large number of pictures in frames from a video stream

Purpose: AI data set production, get a large number of pictures, and then label them

Where to change

1. default--The number of frames spaced 2. input/output--The path of the input video, the path of the captured picture (just put the path in the following ''), and add r before it to represent the absolute path eg:


args = parser.parse_args(['--input',r'F:\data_video\IMG_4395.MOV','--output',r'F:data_rgb_pic\7video'])

Direct code


import cv2
import argparse
import os
def parse_args():
 """
 Parse input arguments
 """
 parser = argparse.ArgumentParser(description='Process pic')
 parser.add_argument('--input', help='video to process', dest='input', default=None, type=str)
 parser.add_argument('--output', help='pic to store', dest='output', default=None, type=str)
 #default How many frames are intercepted for interval 1 A picture 
 parser.add_argument('--skip_frame', dest='skip_frame', help='skip number of video', default=100, type=int)
 #input Is the path of the input video   , output Path to store pictures for output 
 args = parser.parse_args(['--input','','--output',''])
 return args
 
def process_video(i_video, o_video, num):
 cap = cv2.VideoCapture(i_video)
 num_frame = cap.get(cv2.CAP_PROP_FRAME_COUNT)
 expand_name = '.jpg'
 if not cap.isOpened():
  print("Please check the path.")
 cnt = 0
 count = 0
 while 1:
  ret, frame = cap.read()
  cnt += 1
  # how
  # many
  # frame
  # to
  # cut
  if cnt % num == 0:
   count += 1
   cv2.imwrite(os.path.join(o_video, str(count) + expand_name), frame)
 
  if not ret:
   break
 
if __name__ == '__main__':
 args = parse_args()
 if not os.path.exists(args.output):
  os.makedirs(args.output)
 print('Called with args:')
 print(args)
 process_video(args.input, args.output, args.skip_frame)

Related articles: