unified all "folder/directory" into "directory" to end this mess

This commit is contained in:
k4yt3x 2019-04-29 00:06:54 -04:00
parent 0af68bb39c
commit c7353c4bf2
8 changed files with 93 additions and 94 deletions

View File

@ -4,7 +4,7 @@
Name: FFMPEG Class
Author: K4YT3X
Date Created: Feb 24, 2018
Last Modified: April 21, 2019
Last Modified: April 28, 2019
Description: This class handles all FFMPEG related
operations.
@ -74,7 +74,7 @@ class Ffmpeg:
Arguments:
input_video {string} -- input video path
extracted_frames {string} -- video output folder
extracted_frames {string} -- video output directory
"""
execute = [
self.ffmpeg_binary,
@ -93,7 +93,7 @@ class Ffmpeg:
Arguments:
framerate {float} -- target video framerate
resolution {string} -- target video resolution
upscaled_frames {string} -- source images folder
upscaled_frames {string} -- source images directory
"""
execute = [
self.ffmpeg_binary,

View File

@ -5,7 +5,7 @@ Name: Video2X Image Cleaner
Author: BrianPetkovsek
Author: K4YT3X
Date Created: March 24, 2019
Last Modified: April 21, 2019
Last Modified: April 28, 2019
Description: This class is to remove the extracted frames
that have already been upscaled.
@ -27,11 +27,11 @@ class ImageCleaner(threading.Thread):
threading.Thread
"""
def __init__(self, input_folder, output_folder, num_threads):
def __init__(self, input_directory, output_directory, threads):
threading.Thread.__init__(self)
self.input_folder = input_folder
self.output_folder = output_folder
self.num_threads = num_threads
self.input_directory = input_directory
self.output_directory = output_directory
self.threads = threads
self.running = False
def run(self):
@ -53,23 +53,23 @@ class ImageCleaner(threading.Thread):
""" remove frames that have already been upscaled
This method compares the files in the extracted frames
folder with the upscaled frames folder, and removes
directory with the upscaled frames directory, and removes
the frames that has already been upscaled.
"""
# list all images in the extracted frames
output_frames = [f for f in os.listdir(self.output_folder) if os.path.isfile(os.path.join(self.output_folder, f))]
output_frames = [f for f in os.listdir(self.output_directory) if os.path.isfile(os.path.join(self.output_directory, f))]
# compare and remove frames downscaled images that finished being upscaled
# within each thread's extracted frames folder
for i in range(self.num_threads):
dir_path = os.path.join(self.input_folder, str(i))
# within each thread's extracted frames directory
for i in range(self.threads):
dir_path = os.path.join(self.input_directory, str(i))
# for each file within all the folders
# for each file within all the directories
for f in os.listdir(dir_path):
file_path = os.path.join(dir_path, f)
# if file also exists in the output folder, then the file
# if file also exists in the output directory, then the file
# has already been processed, thus not needed anymore
if os.path.isfile(file_path) and f in output_frames:
os.remove(file_path)

View File

@ -4,7 +4,7 @@
Name: Video2X Upscaler
Author: K4YT3X
Date Created: December 10, 2018
Last Modified: April 21, 2019
Last Modified: April 28, 2019
Licensed under the GNU General Public License Version 3 (GNU GPL v3),
available at: https://www.gnu.org/licenses/gpl-3.0.txt
@ -30,7 +30,7 @@ import time
class Upscaler:
""" An instance of this class is a upscaler that will
upscale all images in the given folder.
upscale all images in the given directory.
Raises:
Exception -- all exceptions
@ -52,19 +52,19 @@ class Upscaler:
self.scale_ratio = None
self.model_dir = None
self.threads = 5
self.video2x_cache_folder = f'{tempfile.gettempdir()}\\video2x'
self.video2x_cache_directory = f'{tempfile.gettempdir()}\\video2x'
self.image_format = 'png'
self.preserve_frames = False
def create_temp_folders(self):
"""create temporary folder/directories
def create_temp_directories(self):
"""create temporary directory
"""
self.extracted_frames = tempfile.mkdtemp(dir=self.video2x_cache_folder)
self.extracted_frames = tempfile.mkdtemp(dir=self.video2x_cache_directory)
Avalon.debug_info(f'Extracted frames are being saved to: {self.extracted_frames}')
self.upscaled_frames = tempfile.mkdtemp(dir=self.video2x_cache_folder)
self.upscaled_frames = tempfile.mkdtemp(dir=self.video2x_cache_directory)
Avalon.debug_info(f'Upscaled frames are being saved to: {self.upscaled_frames}')
def cleanup(self):
def cleanup_temp_directories(self):
"""delete temp directories when done
"""
if not self.preserve_frames:
@ -89,18 +89,18 @@ class Upscaler:
elif not self.method:
raise ArgumentError('You need to specify the enlarging processing unit')
def _progress_bar(self, extracted_frames_folders):
def _progress_bar(self, extracted_frames_directories):
""" This method prints a progress bar
This method prints a progress bar by keeping track
of the amount of frames in the input directory/folder
and the output directory/folder. This is originally
of the amount of frames in the input directory
and the output directory. This is originally
suggested by @ArmandBernard.
"""
# get number of extracted frames
total_frames = 0
for folder in extracted_frames_folders:
total_frames += len([f for f in os.listdir(folder) if f[-4:] == '.png'])
for directory in extracted_frames_directories:
total_frames += len([f for f in os.listdir(directory) if f[-4:] == '.png'])
with tqdm(total=total_frames, ascii=True, desc='Upscaling Progress') as progress_bar:
@ -170,25 +170,25 @@ class Upscaler:
if len(frames) < self.threads:
self.threads = len(frames)
# create a folder for each thread and append folder
# create a directory for each thread and append directory
# name into a list
thread_pool = []
thread_folders = []
thread_directories = []
for thread_id in range(self.threads):
thread_folder = f'{self.extracted_frames}\\{str(thread_id)}'
thread_folders.append(thread_folder)
thread_directory = f'{self.extracted_frames}\\{str(thread_id)}'
thread_directories.append(thread_directory)
# delete old folders and create new folders
if os.path.isdir(thread_folder):
shutil.rmtree(thread_folder)
os.mkdir(thread_folder)
# delete old directories and create new directories
if os.path.isdir(thread_directory):
shutil.rmtree(thread_directory)
os.mkdir(thread_directory)
# append folder path into list
thread_pool.append((thread_folder, thread_id))
# append directory path into list
thread_pool.append((thread_directory, thread_id))
# evenly distribute images into each folder
# until there is none left in the folder
# evenly distribute images into each directory
# until there is none left in the directory
for image in frames:
# move image
shutil.move(image, thread_pool[0][0])
@ -208,7 +208,7 @@ class Upscaler:
upscaler_threads.append(thread)
# start progress bar in a different thread
progress_bar = threading.Thread(target=self._progress_bar, args=(thread_folders,))
progress_bar = threading.Thread(target=self._progress_bar, args=(thread_directories,))
progress_bar.start()
# create the clearer and start it

View File

@ -62,12 +62,13 @@
"-map": "1?",
"-c": "copy",
"-map": "-1:v?",
"-pix_fmt": "yuv420p",
"-hwaccel": "auto",
"-y": true
}
},
"video2x": {
"video2x_cache_folder": null,
"video2x_cache_directory": null,
"image_format": "png",
"preserve_frames": false
}

View File

@ -13,7 +13,7 @@ __ __ _ _ ___ __ __
Name: Video2X Controller
Author: K4YT3X
Date Created: Feb 24, 2018
Last Modified: April 21, 2019
Last Modified: April 28, 2019
Licensed under the GNU General Public License Version 3 (GNU GPL v3),
available at: https://www.gnu.org/licenses/gpl-3.0.txt
@ -78,7 +78,7 @@ def process_arguments():
upscaler_options = parser.add_argument_group('Upscaler Options')
upscaler_options.add_argument('-m', '--method', help='Upscaling method', action='store', default='gpu', choices=['cpu', 'gpu', 'cudnn'], required=True)
upscaler_options.add_argument('-d', '--driver', help='Waifu2x driver', action='store', default='waifu2x_caffe', choices=['waifu2x_caffe', 'waifu2x_converter'])
upscaler_options.add_argument('-y', '--model_dir', help='Folder containing model JSON files', action='store')
upscaler_options.add_argument('-y', '--model_dir', help='directory containing model JSON files', action='store')
upscaler_options.add_argument('-t', '--threads', help='Number of threads to use for upscaling', action='store', type=int, default=5)
upscaler_options.add_argument('-c', '--config', help='Video2X config file location', action='store', default=f'{os.path.dirname(os.path.abspath(sys.argv[0]))}\\video2x.json')
upscaler_options.add_argument('-b', '--batch', help='Enable batch mode (select all default values to questions)', action='store_true')
@ -183,24 +183,24 @@ def absolutify_paths(config):
Returns:
dict -- configuration file dictionary
"""
current_folder = os.path.dirname(os.path.abspath(sys.argv[0]))
current_directory = os.path.dirname(os.path.abspath(sys.argv[0]))
# check waifu2x-caffe path
if not re.match('^[a-z]:', config['waifu2x_caffe']['waifu2x_caffe_path'], re.IGNORECASE):
config['waifu2x_caffe']['waifu2x_caffe_path'] = f'{current_folder}\\{config["waifu2x_caffe"]["waifu2x_caffe_path"]}'
config['waifu2x_caffe']['waifu2x_caffe_path'] = f'{current_directory}\\{config["waifu2x_caffe"]["waifu2x_caffe_path"]}'
# check waifu2x-converter-cpp path
if not re.match('^[a-z]:', config['waifu2x_converter']['waifu2x_converter_path'], re.IGNORECASE):
config['waifu2x_converter']['waifu2x_converter_path'] = f'{current_folder}\\{config["waifu2x_converter"]["waifu2x_converter_path"]}'
config['waifu2x_converter']['waifu2x_converter_path'] = f'{current_directory}\\{config["waifu2x_converter"]["waifu2x_converter_path"]}'
# check ffmpeg path
if not re.match('^[a-z]:', config['ffmpeg']['ffmpeg_path'], re.IGNORECASE):
config['ffmpeg']['ffmpeg_path'] = f'{current_folder}\\{config["ffmpeg"]["ffmpeg_path"]}'
config['ffmpeg']['ffmpeg_path'] = f'{current_directory}\\{config["ffmpeg"]["ffmpeg_path"]}'
# check video2x cache path
if config['video2x']['video2x_cache_folder']:
if not re.match('^[a-z]:', config['video2x']['video2x_cache_folder'], re.IGNORECASE):
config['video2x']['video2x_cache_folder'] = f'{current_folder}\\{config["video2x"]["video2x_cache_folder"]}'
if config['video2x']['video2x_cache_directory']:
if not re.match('^[a-z]:', config['video2x']['video2x_cache_directory'], re.IGNORECASE):
config['video2x']['video2x_cache_directory'] = f'{current_directory}\\{config["video2x"]["video2x_cache_directory"]}'
return config
@ -256,26 +256,26 @@ elif args.driver == 'waifu2x_converter':
ffmpeg_settings = config['ffmpeg']
# load video2x settings
video2x_cache_folder = config['video2x']['video2x_cache_folder']
video2x_cache_directory = config['video2x']['video2x_cache_directory']
image_format = config['video2x']['image_format'].lower()
preserve_frames = config['video2x']['preserve_frames']
# create temp directories if they don't exist
if not video2x_cache_folder:
video2x_cache_folder = f'{tempfile.gettempdir()}\\video2x'
if not video2x_cache_directory:
video2x_cache_directory = f'{tempfile.gettempdir()}\\video2x'
if video2x_cache_folder and not os.path.isdir(video2x_cache_folder):
if not os.path.isfile(video2x_cache_folder) and not os.path.islink(video2x_cache_folder):
Avalon.warning(f'Specified cache folder/directory {video2x_cache_folder} does not exist')
if Avalon.ask('Create folder/directory?', default=True, batch=args.batch):
if os.mkdir(video2x_cache_folder) is None:
Avalon.info(f'{video2x_cache_folder} created')
if video2x_cache_directory and not os.path.isdir(video2x_cache_directory):
if not os.path.isfile(video2x_cache_directory) and not os.path.islink(video2x_cache_directory):
Avalon.warning(f'Specified cache directory {video2x_cache_directory} does not exist')
if Avalon.ask('Create directory?', default=True, batch=args.batch):
if os.mkdir(video2x_cache_directory) is None:
Avalon.info(f'{video2x_cache_directory} created')
else:
Avalon.error(f'Unable to create {video2x_cache_folder}')
Avalon.error(f'Unable to create {video2x_cache_directory}')
Avalon.error('Aborting...')
exit(1)
else:
Avalon.error('Specified cache folder/directory is a file/link')
Avalon.error('Specified cache directory is a file/link')
Avalon.error('Unable to continue, exiting...')
exit(1)
@ -293,7 +293,7 @@ try:
# check for input output format mismatch
if os.path.isdir(args.output):
Avalon.error('Input and output path type mismatch')
Avalon.error('Input is single file but output is folder')
Avalon.error('Input is single file but output is directory')
raise Exception('input output path type mismatch')
if not re.search('.*\..*$', args.output):
Avalon.error('No suffix found in output file path')
@ -309,19 +309,19 @@ try:
upscaler.scale_ratio = args.ratio
upscaler.model_dir = args.model_dir
upscaler.threads = args.threads
upscaler.video2x_cache_folder = video2x_cache_folder
upscaler.video2x_cache_directory = video2x_cache_directory
upscaler.image_format = image_format
upscaler.preserve_frames = preserve_frames
# run upscaler
upscaler.create_temp_folders()
upscaler.create_temp_directories()
upscaler.run()
upscaler.cleanup()
upscaler.cleanup_temp_directories()
# if input specified is a folder
# if input specified is a directory
elif os.path.isdir(args.input):
""" Upscale videos in a folder/directory """
Avalon.info(f'Upscaling videos in folder/directory: {args.input}')
""" Upscale videos in a directory """
Avalon.info(f'Upscaling videos in directory: {args.input}')
for input_video in [f for f in os.listdir(args.input) if os.path.isfile(os.path.join(args.input, f))]:
output_video = f'{args.output}\\{input_video}'
upscaler = Upscaler(input_video=os.path.join(args.input, input_video), output_video=output_video, method=args.method, waifu2x_settings=waifu2x_settings, ffmpeg_settings=ffmpeg_settings)
@ -333,26 +333,26 @@ try:
upscaler.scale_ratio = args.ratio
upscaler.model_dir = args.model_dir
upscaler.threads = args.threads
upscaler.video2x_cache_folder = video2x_cache_folder
upscaler.video2x_cache_directory = video2x_cache_directory
upscaler.image_format = image_format
upscaler.preserve_frames = preserve_frames
# run upscaler
upscaler.create_temp_folders()
upscaler.create_temp_directories()
upscaler.run()
upscaler.cleanup()
upscaler.cleanup_temp_directories()
else:
Avalon.error('Input path is neither a file nor a folder/directory')
raise FileNotFoundError(f'{args.input} is neither file nor folder/directory')
Avalon.error('Input path is neither a file nor a directory')
raise FileNotFoundError(f'{args.input} is neither file nor directory')
Avalon.info(f'Program completed, taking {round((time.time() - begin_time), 5)} seconds')
except Exception:
Avalon.error('An exception has occurred')
traceback.print_exc()
finally:
# remove Video2X Cache folder
# remove Video2X Cache directory
try:
if not preserve_frames:
shutil.rmtree(video2x_cache_folder)
shutil.rmtree(video2x_cache_directory)
except FileNotFoundError:
pass

View File

@ -4,7 +4,7 @@
Name: Video2X Setup Script
Author: K4YT3X
Date Created: November 28, 2018
Last Modified: April 21, 2019
Last Modified: April 28, 2019
Licensed under the GNU General Public License Version 3 (GNU GPL v3),
available at: https://www.gnu.org/licenses/gpl-3.0.txt
@ -167,9 +167,7 @@ class Video2xSetup:
template_dict['waifu2x_converter']['waifu2x_converter_path'] = f'{local_app_data}\\video2x\\waifu2x-converter-cpp'
template_dict['ffmpeg']['ffmpeg_path'] = f'{local_app_data}\\video2x\\ffmpeg-latest-win64-static\\bin'
template_dict['ffmpeg']['ffmpeg_hwaccel'] = 'auto'
template_dict['ffmpeg']['extra_arguments'] = []
template_dict['video2x']['video2x_cache_folder'] = None
template_dict['video2x']['video2x_cache_directory'] = None
template_dict['video2x']['preserve_frames'] = False
# Write configuration into file

View File

@ -4,7 +4,7 @@
Name: Waifu2x Caffe Driver
Author: K4YT3X
Date Created: Feb 24, 2018
Last Modified: April 21, 2019
Last Modified: April 28, 2019
Description: This class is a high-level wrapper
for waifu2x-caffe.
@ -33,20 +33,20 @@ class Waifu2xCaffe:
self.model_dir = model_dir
self.print_lock = threading.Lock()
def upscale(self, input_folder, output_folder, scale_ratio, scale_width, scale_height, image_format, upscaler_exceptions):
def upscale(self, input_directory, output_directory, scale_ratio, scale_width, scale_height, image_format, upscaler_exceptions):
"""This is the core function for WAIFU2X class
Arguments:
input_folder {string} -- source folder path
output_folder {string} -- output folder path
input_directory {string} -- source directory path
output_directory {string} -- output directory path
width {int} -- output video width
height {int} -- output video height
"""
try:
# overwrite config file settings
self.waifu2x_settings['input_path'] = input_folder
self.waifu2x_settings['output_path'] = output_folder
self.waifu2x_settings['input_path'] = input_directory
self.waifu2x_settings['output_path'] = output_directory
if scale_ratio:
self.waifu2x_settings['scale_ratio'] = scale_ratio

View File

@ -4,7 +4,7 @@
Name: Waifu2x Converter CPP Driver
Author: K4YT3X
Date Created: February 8, 2019
Last Modified: April 21, 2019
Last Modified: April 28, 2019
Description: This class is a high-level wrapper
for waifu2x-converter-cpp.
@ -28,26 +28,26 @@ class Waifu2xConverter:
self.waifu2x_settings['model_dir'] = model_dir
self.print_lock = threading.Lock()
def upscale(self, input_folder, output_folder, scale_ratio, jobs, image_format, upscaler_exceptions):
def upscale(self, input_directory, output_directory, scale_ratio, jobs, image_format, upscaler_exceptions):
""" Waifu2x Converter Driver Upscaler
This method executes the upscaling of extracted frames.
Arguments:
input_folder {string} -- source folder path
output_folder {string} -- output folder path
input_directory {string} -- source directory path
output_directory {string} -- output directory path
scale_ratio {int} -- frames' scale ratio
threads {int} -- number of threads
"""
try:
# overwrite config file settings
self.waifu2x_settings['input'] = input_folder
self.waifu2x_settings['output'] = output_folder
self.waifu2x_settings['input'] = input_directory
self.waifu2x_settings['output'] = output_directory
# temporary fix for https://github.com/DeadSix27/waifu2x-converter-cpp/issues/109
"""
self.waifu2x_settings['i'] = input_folder
self.waifu2x_settings['o'] = output_folder
self.waifu2x_settings['i'] = input_directory
self.waifu2x_settings['o'] = output_directory
self.waifu2x_settings['input'] = None
self.waifu2x_settings['output'] = None
"""