PhotoSorter/photoSort2.py

122 lines
3.9 KiB
Python
Raw Normal View History

2023-01-25 10:19:25 -07:00
##Made by Donavon McDowell
##A script used to sort through a large set of images and get rid of any bad ones aswell as put the new ones in a folder that contains only good images
## Read through the root file
## Open each file representing each month
## Open both Spillway and Toe Berm folder
## Read each individual image
## Get rid of any that are taken after dark
##Bad times 0600 and 1800 are dark
import os
import shutil
import cv2
##The folder that contains your unsorted images
folder_dir = "C:/Users/donav/OneDrive/Documents/MPE/images/AllSpillway"
##The desitination folder
base_folder_dir = "C:/Users/donav/OneDrive/Documents/MPE"
##Destination Folder Name
destination_folder_name = "Spillway Verified Images"
##Reject Folder Name
reject_destination_folder_name = "Spillway Rejected Images"
##Max brightness threshold
max_threshold = 190
##Min brightness threshold
min_threshold = 55
##Used for counting
global i
i = 0
##Used for counting
global x
x = 0
##Increment through folders
def checkFiles():
global x
##increment through the files in the folder
for files in os.listdir():
verifyImage(files)
x = x + 1
##Create the output folder to copy all of the files to
def createDestinationFolders():
##Create the good destination folder
path = os.path.join(base_folder_dir, destination_folder_name)
try:
##Check if the folder exists or not
if(os.path.exists(path)):
print("Folder already exists.")
os.remove(path)
os.mkdir(path)
print('Verified folder created!\n')
print('File Path \"' + path + '\"')
else:
os.mkdir(path)
print('Verified folder created!\n')
print('File Path \"' + path + '\"')
except OSError as error:
print(error)
##Create the reject destination folder
path = os.path.join(base_folder_dir, reject_destination_folder_name)
try:
if(os.path.exists(path)):
print("Folder already exists.")
os.remove(path)
os.mkdir(path)
print('Verified folder created!\n')
print('File Path \"' + path + '\"')
else:
os.mkdir(path)
print('Reject folder created!\n')
print('File Path \"' + path + '\"')
except OSError as error:
print(error)
##Change back to the default directory
os.chdir(folder_dir)
##Check that the image is good
def verifyImage(filename):
global i
if (('0700' not in filename) and ('0600' not in filename) and ('1800' not in filename)) and check_brightness(os.path.join(folder_dir, filename), min_threshold, max_threshold):
##Copy file to verified output folder
src = os.path.join(folder_dir, filename)
dstn = base_folder_dir + '/' + destination_folder_name + '/' + filename
##Copy the verified file to the output folder
shutil.copy(src, dstn)
else:
##reject file
print("Reject Detected. #" + str(i))
i = i + 1
##Copy file to reject output folder
src = os.path.join(folder_dir, filename)
dstn = base_folder_dir + '/' + reject_destination_folder_name + '/' + filename
##Copy the verified file to the output folder
shutil.copy(src, dstn)
##Check the image brightness
def check_brightness(image_file, min, max):
# Load the image
img = cv2.imread(image_file)
# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Calculate the average brightness
avg_brightness = gray.mean()
print(avg_brightness)
# Check if the average brightness is above or below the threshold
if (avg_brightness > min) and (avg_brightness < max) :
return True
else:
return False
##Step 1: Create output folder
createDestinationFolders()
##Step 2: Look through all existing folders
checkFiles()
print("Processing Complete.")
print("Total number of files processed: " + str(x))