You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
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为间隔多少帧截取一张图片
parser . add_argument ( ' --skip_frame ' , dest = ' skip_frame ' , help = ' skip number of video ' , default = 10 , type = int )
# input为输入视频的路径 , output为输出存放图片的路径
args = parser . parse_args ( [ ' --input ' , r ' C: \ Users \ xinluo \ PycharmProjects \ pythonProject \ 1234.mp4 ' , ' --output ' , r ' C: \ Users \ xinluo \ PycharmProjects \ pythonProject \ Saved Pictures ' ] )
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 )