4.0.0 new argument parsing mechanism and project structure

This commit is contained in:
k4yt3x
2020-05-04 17:12:41 -04:00
parent 88299d404a
commit 89860f22cb
7 changed files with 223 additions and 107 deletions

View File

@@ -4,13 +4,14 @@
Name: Waifu2x Caffe Driver
Author: K4YT3X
Date Created: May 3, 2020
Last Modified: May 3, 2020
Last Modified: May 4, 2020
Description: This class is a high-level wrapper
for waifu2x-caffe.
"""
# built-in imports
import argparse
import os
import shlex
import subprocess
@@ -20,7 +21,7 @@ import threading
from avalon_framework import Avalon
class Anime4kCpp:
class WrapperMain:
""" Anime4K CPP wrapper
"""
@@ -28,6 +29,31 @@ class Anime4kCpp:
self.driver_settings = driver_settings
self.print_lock = threading.Lock()
@staticmethod
def parse_arguments(arguments):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)
parser.add_argument('--help', action='help', help='show this help message and exit')
# parser.add_argument('-i', '--input', type=pathlib.Path, help='File for loading')
# parser.add_argument('-o', '--output', type=pathlib.Path, help='File for outputting')
parser.add_argument('-p', '--passes', type=int, help='Passes for processing')
parser.add_argument('-n', '--pushColorCount', type=int, help='Limit the number of color pushes')
parser.add_argument('-c', '--strengthColor', type=float, help='Strength for pushing color,range 0 to 1,higher for thinner')
parser.add_argument('-g', '--strengthGradient', type=float, help='Strength for pushing gradient,range 0 to 1,higher for sharper')
parser.add_argument('-z', '--zoomFactor', type=float, help='zoom factor for resizing')
parser.add_argument('-t', '--threads', type=int, help='Threads count for video processing')
parser.add_argument('-f', '--fastMode', action='store_true', help='Faster but maybe low quality')
# parser.add_argument('-v', '--videoMode', action='store_true', help='Video process')
parser.add_argument('-s', '--preview', action='store_true', help='Preview image')
parser.add_argument('-b', '--preProcessing', action='store_true', help='Enable pre processing')
parser.add_argument('-a', '--postProcessing', action='store_true', help='Enable post processing')
parser.add_argument('-r', '--preFilters', type=int, help='Enhancement filter, only working when preProcessing is true,there are 5 options by binary:Median blur=0000001, Mean blur=0000010, CAS Sharpening=0000100, Gaussian blur weak=0001000, Gaussian blur=0010000, Bilateral filter=0100000, Bilateral filter faster=1000000, you can freely combine them, eg: Gaussian blur weak + Bilateral filter = 0001000 | 0100000 = 0101000 = 40(D)')
parser.add_argument('-e', '--postFilters', type=int, help='Enhancement filter, only working when postProcessing is true,there are 5 options by binary:Median blur=0000001, Mean blur=0000010, CAS Sharpening=0000100, Gaussian blur weak=0001000, Gaussian blur=0010000, Bilateral filter=0100000, Bilateral filter faster=1000000, you can freely combine them, eg: Gaussian blur weak + Bilateral filter = 0001000 | 0100000 = 0101000 = 40(D), so you can put 40 to enable Gaussian blur weak and Bilateral filter, which also is what I recommend for image that < 1080P, 48 for image that >= 1080P, and for performance I recommend to use 72 for video that < 1080P, 80 for video that >=1080P')
parser.add_argument('-q', '--GPUMode', action='store_true', help='Enable GPU acceleration')
parser.add_argument('-l', '--listGPUs', action='store_true', help='list GPUs')
parser.add_argument('-h', '--platformID', type=int, help='Specify the platform ID')
parser.add_argument('-d', '--deviceID', type=int, help='Specify the device ID')
return parser.parse_args(arguments)
def upscale(self, input_file, output_file, zoom_factor, threads):
"""This is the core function for WAIFU2X class
@@ -52,7 +78,7 @@ class Anime4kCpp:
value = self.driver_settings[key]
# is executable key or null or None means that leave this option out (keep default)
# null or None means that leave this option out (keep default)
if value is None or value is False:
continue
else:

View File

@@ -4,13 +4,14 @@
Name: SRMD NCNN Vulkan Driver
Creator: K4YT3X
Date Created: April 26, 2020
Last Modified: April 26, 2020
Last Modified: May 4, 2020
Description: This class is a high-level wrapper
for srmd_ncnn_vulkan.
"""
# built-in imports
import argparse
import os
import shlex
import subprocess
@@ -20,7 +21,7 @@ import threading
from avalon_framework import Avalon
class SrmdNcnnVulkan:
class WrapperMain:
"""This class communicates with SRMD NCNN Vulkan engine
An object will be created for this class, containing information
@@ -40,6 +41,22 @@ class SrmdNcnnVulkan:
self.print_lock = threading.Lock()
@staticmethod
def parse_arguments(arguments):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)
parser.add_argument('--help', action='help', help='show this help message and exit')
parser.add_argument('-v', action='store_true', help='verbose output')
# parser.add_argument('-i', type=pathlib.Path, help='input image path (jpg/png) or directory')
# parser.add_argument('-o', type=pathlib.Path, help='output image path (png) or directory')
parser.add_argument('-n', type=int, choices=range(-1, 11), help='denoise level')
parser.add_argument('-s', type=int, choices=range(2, 5), help='upscale ratio')
parser.add_argument('-t', type=int, help='tile size (>=32)')
parser.add_argument('-m', type=str, help='srmd model path')
parser.add_argument('-g', type=int, help='gpu device to use')
parser.add_argument('-j', type=str, help='thread count for load/proc/save')
parser.add_argument('-x', action='store_true', help='enable tta mode')
return parser.parse_args(arguments)
def upscale(self, input_directory, output_directory, scale_ratio):
"""This is the core function for SRMD ncnn Vulkan class
@@ -52,7 +69,7 @@ class SrmdNcnnVulkan:
# overwrite config file settings
self.driver_settings['i'] = input_directory
self.driver_settings['o'] = output_directory
self.driver_settings['s'] = int(scale_ratio)
self.driver_settings['s'] = scale_ratio
# list to be executed
# initialize the list with the binary path as the first element
@@ -62,15 +79,18 @@ class SrmdNcnnVulkan:
value = self.driver_settings[key]
# is executable key or null or None means that leave this option out (keep default)
if key == 'path' or value is None or value is False:
# null or None means that leave this option out (keep default)
if value is None or value is False:
continue
else:
if len(key) == 1:
execute.append(f'-{key}')
else:
execute.append(f'--{key}')
execute.append(str(value))
# true means key is an option
if value is not True:
execute.append(str(value))
# return the Popen object of the new process created
self.print_lock.acquire()

View File

@@ -4,13 +4,14 @@
Name: Waifu2x Caffe Driver
Author: K4YT3X
Date Created: Feb 24, 2018
Last Modified: May 3, 2020
Last Modified: May 4, 2020
Description: This class is a high-level wrapper
for waifu2x-caffe.
"""
# built-in imports
import argparse
import os
import shlex
import subprocess
@@ -20,7 +21,7 @@ import threading
from avalon_framework import Avalon
class Waifu2xCaffe:
class WrapperMain:
"""This class communicates with waifu2x cui engine
An object will be created for this class, containing information
@@ -29,16 +30,36 @@ class Waifu2xCaffe:
the upscale function.
"""
def __init__(self, driver_settings, model_dir, bit_depth):
def __init__(self, driver_settings):
self.driver_settings = driver_settings
self.driver_settings['model_dir'] = model_dir
self.driver_settings['output_depth'] = bit_depth
# arguments passed through command line overwrites config file values
self.model_dir = model_dir
self.print_lock = threading.Lock()
def upscale(self, input_directory, output_directory, scale_ratio, scale_width, scale_height, image_format):
@staticmethod
def parse_arguments(arguments):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)
parser.add_argument('--help', action='help', help='show this help message and exit')
parser.add_argument('-t', '--tta', type=int, choices=range(2), help='8x slower and slightly high quality')
parser.add_argument('--gpu', type=int, help='gpu device no')
parser.add_argument('-b', '--batch_size', type=int, help='input batch size')
parser.add_argument('--crop_h', type=int, help='input image split size(height)')
parser.add_argument('--crop_w', type=int, help='input image split size(width)')
parser.add_argument('-c', '--crop_size', type=int, help='input image split size')
parser.add_argument('-d', '--output_depth', type=int, help='output image chaneel depth bit')
parser.add_argument('-q', '--output_quality', type=int, help='output image quality')
parser.add_argument('-p', '--process', choices=['cpu', 'gpu', 'cudnn'], help='process mode')
parser.add_argument('--model_dir', type=str, help='path to custom model directory (don\'t append last / )')
parser.add_argument('-h', '--scale_height', type=int, help='custom scale height')
parser.add_argument('-w', '--scale_width', type=int, help='custom scale width')
parser.add_argument('-s', '--scale_ratio', type=float, help='custom scale ratio')
parser.add_argument('-n', '--noise_level', type=int, choices=range(4), help='noise reduction level')
parser.add_argument('-m', '--mode', choices=['noise', 'scale', 'noise_scale'], help='image processing mode')
parser.add_argument('-e', '--output_extension', type=str, help='extention to output image file when output_path is (auto) or input_path is folder')
parser.add_argument('-l', '--input_extention_list', type=str, help='extention to input image file when input_path is folder')
# parser.add_argument('-o', '--output', type=pathlib.Path, help='path to output image file (when input_path is folder, output_path must be folder)')
# parser.add_argument('-i', '--input_file', type=pathlib.Path, help='(required) path to input image file')
return parser.parse_args(arguments)
def upscale(self, input_directory, output_directory, scale_ratio, scale_width, scale_height, image_format, bit_depth):
"""This is the core function for WAIFU2X class
Arguments:
@@ -59,6 +80,7 @@ class Waifu2xCaffe:
self.driver_settings['scale_height'] = scale_height
self.driver_settings['output_extention'] = image_format
self.driver_settings['output_depth'] = bit_depth
# list to be executed
# initialize the list with waifu2x binary path as the first element
@@ -68,7 +90,7 @@ class Waifu2xCaffe:
value = self.driver_settings[key]
# is executable key or null or None means that leave this option out (keep default)
# null or None means that leave this option out (keep default)
if value is None or value is False:
continue
else:

View File

@@ -4,13 +4,14 @@
Name: Waifu2x Converter CPP Driver
Author: K4YT3X
Date Created: February 8, 2019
Last Modified: May 3, 2020
Last Modified: May 4, 2020
Description: This class is a high-level wrapper
for waifu2x-converter-cpp.
"""
# built-in imports
import argparse
import os
import pathlib
import shlex
@@ -21,7 +22,7 @@ import threading
from avalon_framework import Avalon
class Waifu2xConverterCpp:
class WrapperMain:
"""This class communicates with waifu2x cui engine
An object will be created for this class, containing information
@@ -30,11 +31,40 @@ class Waifu2xConverterCpp:
the upscale function.
"""
def __init__(self, driver_settings, model_dir):
def __init__(self, driver_settings):
self.driver_settings = driver_settings
self.driver_settings['model_dir'] = model_dir
self.print_lock = threading.Lock()
@staticmethod
def parse_arguments(arguments):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)
parser.add_argument('--help', action='help', help='show this help message and exit')
parser.add_argument('--list-supported-formats', action='store_true', help='dump currently supported format list')
parser.add_argument('--list-opencv-formats', action='store_true', help='(deprecated. Use --list-supported-formats) dump opencv supported format list')
parser.add_argument('-l', '--list-processor', action='store_true', help='dump processor list')
parser.add_argument('-f', '--output-format', choices=['png', 'jpg'], help='The format used when running in recursive/folder mode\nSee --list-supported-formats for a list of supported formats/extensions.')
parser.add_argument('-c', '--png-compression', type=int, choices=range(10), help='Set PNG compression level (0-9), 9 = Max compression (slowest & smallest)')
parser.add_argument('-q', '--image-quality', type=int, choices=range(100), help='JPEG & WebP Compression quality (0-101, 0 being smallest size and lowest quality), use 101 for lossless WebP')
parser.add_argument('--block-size', type=int, help='block size')
parser.add_argument('--disable-gpu', action='store_true', help='disable GPU')
parser.add_argument('--force-OpenCL', action='store_true', help='force to use OpenCL on Intel Platform')
parser.add_argument('-p', '--processor', type=int, help='set target processor')
parser.add_argument('-j', '--jobs', type=int, help='number of threads launching at the same time')
parser.add_argument('--model-dir', type=str, help='path to custom model directory (don\'t append last / )')
parser.add_argument('--scale-ratio', type=float, help='custom scale ratio')
parser.add_argument('--noise-level', type=int, choices=range(4), help='noise reduction level')
parser.add_argument('-m', '--mode', choices=['noise', 'scale', 'noise-scale'], help='image processing mode')
parser.add_argument('-v', '--log-level', type=int, choices=range(5), help='Set log level')
parser.add_argument('-s', '--silent', action='store_true', help='Enable silent mode. (same as --log-level 1)')
parser.add_argument('-t', '--tta', type=int, choices=range(2), help='Enable Test-Time Augmentation mode.')
parser.add_argument('-g', '--generate-subdir', type=int, choices=range(2), help='Generate sub folder when recursive directory is enabled.')
parser.add_argument('-a', '--auto-naming', type=int, choices=range(2), help='Add postfix to output name when output path is not specified.\nSet 0 to disable this.')
parser.add_argument('-r', '--recursive-directory', type=int, choices=range(2), help='Search recursively through directories to find more images to process.')
# parser.add_argument('-o', '--output', type=pathlib.Pathh, help='path to output image file or directory (you should use the full path)')
# parser.add_argument('-i', '--input', type=pathlib.Path, help='(required) path to input image file or directory (you should use the full path)')
parser.add_argument('--version', action='store_true', help='Displays version information and exits.')
return parser.parse_args(arguments)
def upscale(self, input_directory, output_directory, scale_ratio, jobs, image_format):
""" Waifu2x Converter Driver Upscaler
This method executes the upscaling of extracted frames.

View File

@@ -4,7 +4,7 @@
Name: Waifu2x NCNN Vulkan Driver
Creator: SAT3LL
Date Created: June 26, 2019
Last Modified: November 15, 2019
Last Modified: May 4, 2020
Editor: K4YT3X
Last Modified: February 22, 2020
@@ -14,6 +14,7 @@ for waifu2x_ncnn_vulkan.
"""
# built-in imports
import argparse
import os
import shlex
import subprocess
@@ -23,7 +24,7 @@ import threading
from avalon_framework import Avalon
class Waifu2xNcnnVulkan:
class WrapperMain:
"""This class communicates with waifu2x ncnn vulkan engine
An object will be created for this class, containing information
@@ -43,6 +44,22 @@ class Waifu2xNcnnVulkan:
self.print_lock = threading.Lock()
@staticmethod
def parse_arguments(arguments):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)
parser.add_argument('--help', action='help', help='show this help message and exit')
parser.add_argument('-v', action='store_true', help='verbose output')
# parser.add_argument('-i', type=pathlib.Path, help='input image path (jpg/png) or directory')
# parser.add_argument('-o', type=pathlib.Path, help='output image path (png) or directory')
parser.add_argument('-n', type=int, choices=range(-1, 4), help='denoise level')
parser.add_argument('-s', type=int, choices=range(1, 3), help='upscale ratio')
parser.add_argument('-t', type=int, help='tile size (>=32)')
parser.add_argument('-m', type=str, help='waifu2x model path')
parser.add_argument('-g', type=int, help='gpu device to use')
parser.add_argument('-j', type=str, help='thread count for load/proc/save')
parser.add_argument('-x', action='store_true', help='enable tta mode')
return parser.parse_args(arguments)
def upscale(self, input_directory, output_directory, scale_ratio):
"""This is the core function for WAIFU2X class
@@ -59,21 +76,24 @@ class Waifu2xNcnnVulkan:
# list to be executed
# initialize the list with waifu2x binary path as the first element
execute = [str(self.driver_settings['path'])]
execute = [self.driver_settings.pop('path')]
for key in self.driver_settings.keys():
value = self.driver_settings[key]
# is executable key or null or None means that leave this option out (keep default)
if key == 'path' or value is None or value is False:
# null or None means that leave this option out (keep default)
if value is None or value is False:
continue
else:
if len(key) == 1:
execute.append(f'-{key}')
else:
execute.append(f'--{key}')
execute.append(str(value))
# true means key is an option
if value is not True:
execute.append(str(value))
# return the Popen object of the new process created
self.print_lock.acquire()