From 7d7ea1196fbb0269b35232bcb854ada7b92a3a5b Mon Sep 17 00:00:00 2001 From: Oscar Pocock Date: Mon, 14 Feb 2022 12:03:49 +0000 Subject: [PATCH] Filter frames by filesize #1 --- src/autophotographer/autophotographer.py | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/autophotographer/autophotographer.py diff --git a/src/autophotographer/autophotographer.py b/src/autophotographer/autophotographer.py new file mode 100644 index 0000000..9bd87f8 --- /dev/null +++ b/src/autophotographer/autophotographer.py @@ -0,0 +1,56 @@ +import cv2 +import sys +import argparse +import os +import pathlib + +# Process arguments +def parse_arguments(argv=None): + parser = argparse.ArgumentParser() + parser.add_argument('-i', '--input', dest='inputfile', type=pathlib.Path, help='Specify a video file') + parser.add_argument('-o', '--output', dest='outputfolder', type=pathlib.Path, help='Specify a folder to save frames to') + return parser.parse_args() + +# Convert video to frames +def video_to_frames(): + capture = cv2.VideoCapture(str(inputfile)) + success,image = capture.read() + count = 0 + while success: + outputfile = outputfolder + "/frame%d.jpg" % count + print(outputfile) + cv2.imwrite(outputfile, image) + success,image = capture.read() + count +=1 + +# Shrink set based on filesize +def display_file_sizes(): + filesizes = [] + for filename in os.listdir(outputfolder): + filepath = outputfolder + "/" + filename + filesize = os.path.getsize(filepath) + print(filepath + ": " + str(filesize)) + filesizes.append(filesize) + # work out average + average = sum(filesizes)/len(filesizes) + print ("Average is: " + str(average)) + # delete files below average + count = 0 + for filename in os.listdir(outputfolder): + filepath = outputfolder + "/" + filename + if filesizes[count] < average: + # print(filepath + ": " + str(filesizes[count])) + os.remove(filepath) + count += 1 + +#def remove_similar_frames(): +# sad + +args = parse_arguments() +inputfile = str(args.inputfile.absolute()) +outputfolder = str(args.outputfolder.absolute()) + +# Convert video to frames +video_to_frames() + +display_file_sizes() \ No newline at end of file