Added basic pipeline

This commit is contained in:
Oscar Blue 2022-03-26 03:50:02 +00:00
parent 9846c60f2c
commit 868473e284

87
src/autophotographer.py Normal file
View file

@ -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()