From 868473e2844c0afb69fe7e1b1bfdc214cabf387d Mon Sep 17 00:00:00 2001 From: Oscar Pocock Date: Sat, 26 Mar 2022 03:50:02 +0000 Subject: [PATCH] Added basic pipeline --- src/autophotographer.py | 87 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/autophotographer.py diff --git a/src/autophotographer.py b/src/autophotographer.py new file mode 100644 index 0000000..09c8650 --- /dev/null +++ b/src/autophotographer.py @@ -0,0 +1,87 @@ +import cv2 +import sys +import argparse +import os +import pathlib +import numpy +import time + +# 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(): + startTime = time.time() + print("Converting 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 + endTime = time.time() + totalTime = endTime - startTime + print("Exported " + str(count) + " frames in " + str(totalTime) + " seconds.") + +# 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(): +def order_frames_by_filesize(): + frames = os.listdir(outputfolder) + frames = sorted(frames, key = lambda x: os.stat(os.path.join(outputfolder, x)).st_size, reverse = True) + for frame in frames: + filesize = os.stat(os.path.join(outputfolder, frame)).st_size + if filesize > 1024: + filesize = filesize / 1024 + print(frame + ": " + str(filesize) + " KB") + else: + print(frame + ": " + str(filesize)) + +def order_frames_by_brightness(): + frames_path = os.listdir(outputfolder) + frames_path = sorted(frames_path, key = return_frame_brightness) + for frame_path in frames_path: + print(frame_path + ": " + str(return_frame_brightness(frame_path))) + +def return_frame_brightness(frame_path): + frame = cv2.imread(os.path.join(outputfolder, frame_path)) + hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + h, s, v = cv2.split(hsv_frame) + average_v = numpy.average(v) + return average_v + +args = parse_arguments() +inputfile = str(args.inputfile.absolute()) +outputfolder = str(args.outputfolder.absolute()) + +# Convert video to frames +video_to_frames() + +display_file_sizes() +#order_frames_by_filesize() +#order_frames_by_brightness() \ No newline at end of file