From 47471352ba2cead3d8b4a7b091ddf499e683c15f Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Thu, 15 Aug 2019 23:34:24 -0400 Subject: [PATCH] 2.10.0 added anime4k support --- bin/anime4k.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ bin/ffmpeg.py | 2 +- bin/upscaler.py | 16 +++++++-- bin/video2x.json | 4 +++ bin/video2x.py | 16 +++++++-- 5 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 bin/anime4k.py diff --git a/bin/anime4k.py b/bin/anime4k.py new file mode 100644 index 0000000..fe07f6b --- /dev/null +++ b/bin/anime4k.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Name: Anime4K Driver +Author: K4YT3X +Date Created: August 15, 2019 +Last Modified: August 15, 2019 + +Description: This class is a high-level wrapper +for Anime4k. +""" + +# built-in imports +import subprocess +import threading + +# third-party imports +from avalon_framework import Avalon + + +class Anime4k: + """This class communicates with Anime4K engine + + An object will be created for this class, containing information + about the binary address and the processing method. When being called + by the main program, other detailed information will be passed to + the upscale function. + """ + + def __init__(self, waifu2x_settings): + self.waifu2x_settings = waifu2x_settings + self.print_lock = threading.Lock() + + def upscale(self, input_directory, output_directory, scale_ratio, upscaler_exceptions, push_strength=None, push_grad_strength=None): + """ Anime4K wrapper + + Arguments: + file_in {string} -- input file path + file_out {string} -- output file path + + Keyword Arguments: + scale {int} -- scale ratio (default: {None}) + push_strength {int} -- residual push strength (default: {None}) + push_grad_strength {int} -- residual gradient push strength (default: {None}) + + Returns: + subprocess.Popen.returncode -- command line return value of execution + """ + try: + # return value is the sum of all execution return codes + return_value = 0 + + # get a list lof all image files in input_directory + extracted_frame_files = [f for f in input_directory.iterdir() if str(f).lower().endswith('.png') or str(f).lower().endswith('.jpg')] + + # upscale each image in input_directory + for image in extracted_frame_files: + + execute = [ + self.waifu2x_settings['java_path'], + '-jar', + self.waifu2x_settings['anime4k_path'], + str(image.absolute()), + str(output_directory / image.name), + str(scale_ratio) + ] + + # optional arguments + kwargs = [ + 'push_strength', + 'push_grad_strength' + ] + + # if optional argument specified, append value to execution list + for arg in kwargs: + if locals()[arg] is not None: + execute.extend([locals([arg])]) + + Avalon.debug_info(f'Executing: {execute}') + return_value += subprocess.run(execute, check=True).returncode + + # print thread exiting message + self.print_lock.acquire() + Avalon.debug_info(f'[upscaler] Thread {threading.current_thread().name} exiting') + self.print_lock.release() + + # return command execution return code + return return_value + except Exception as e: + upscaler_exceptions.append(e) diff --git a/bin/ffmpeg.py b/bin/ffmpeg.py index 14df385..ab3d139 100644 --- a/bin/ffmpeg.py +++ b/bin/ffmpeg.py @@ -4,7 +4,7 @@ Name: Video2X FFmpeg Controller Author: K4YT3X Date Created: Feb 24, 2018 -Last Modified: August 3, 2019 +Last Modified: August 15, 2019 Description: This class handles all FFmpeg related operations. """ diff --git a/bin/upscaler.py b/bin/upscaler.py index 9614da2..88de710 100644 --- a/bin/upscaler.py +++ b/bin/upscaler.py @@ -4,7 +4,7 @@ Name: Video2X Upscaler Author: K4YT3X Date Created: December 10, 2018 -Last Modified: August 3, 2019 +Last Modified: August 15, 2019 Dev: SAT3LL @@ -15,6 +15,7 @@ Licensed under the GNU General Public License Version 3 (GNU GPL v3), """ # local imports +from anime4k import Anime4k from exceptions import * from ffmpeg import Ffmpeg from image_cleaner import ImageCleaner @@ -156,7 +157,7 @@ class Upscaler: self.upscaler_exceptions = [] # initialize waifu2x driver - drivers = ['waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan'] + drivers = ['waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan', 'anime4k'] if self.waifu2x_driver not in drivers: raise UnrecognizedDriverError(f'Unrecognized waifu2x driver: {self.waifu2x_driver}') @@ -176,6 +177,8 @@ class Upscaler: self.progress_bar_exit_signal = True progress_bar.join() return + + # drivers that are to be multi-threaded by video2x else: # create a container for all upscaler threads upscaler_threads = [] @@ -247,6 +250,15 @@ class Upscaler: self.scale_ratio, self.upscaler_exceptions)) + # if the driver being used is anime4k + elif self.waifu2x_driver == 'anime4k': + w2 = Anime4k(copy.deepcopy(self.waifu2x_settings)) + thread = threading.Thread(target=w2.upscale, + args=(thread_info[0], + self.upscaled_frames, + self.scale_ratio, + self.upscaler_exceptions)) + # create thread thread.name = thread_info[1] diff --git a/bin/video2x.json b/bin/video2x.json index fab9097..5854b3e 100644 --- a/bin/video2x.json +++ b/bin/video2x.json @@ -51,6 +51,10 @@ "g": 0, "j": "1:2:2" }, + "anime4k": { + "anime4k_path": "C:\\Users\\K4YT3X\\AppData\\Local\\video2x\\Anime4K_Java\\Anime4K.jar", + "java_path": "C:\\Program Files\\Java\\jdk-12.0.2\\bin\\java.exe" + }, "ffmpeg": { "ffmpeg_path": "C:\\Users\\K4YT3X\\AppData\\Local\\video2x\\ffmpeg-latest-win64-static\\bin", "video_to_frames": { diff --git a/bin/video2x.py b/bin/video2x.py index 3746c4c..8764027 100644 --- a/bin/video2x.py +++ b/bin/video2x.py @@ -13,7 +13,7 @@ __ __ _ _ ___ __ __ Name: Video2X Controller Author: K4YT3X Date Created: Feb 24, 2018 -Last Modified: August 7, 2019 +Last Modified: August 15, 2019 Dev: BrianPetkovsek Dev: SAT3LL @@ -64,7 +64,7 @@ from avalon_framework import Avalon import GPUtil import psutil -VERSION = '2.9.0' +VERSION = '2.10.0' LEGAL_INFO = f'''Video2X Version: {VERSION} Author: K4YT3X @@ -104,7 +104,7 @@ def process_arguments(): # upscaler options 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']) - upscaler_options.add_argument('-d', '--driver', help='waifu2x driver', action='store', default='waifu2x_caffe', choices=['waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan']) + upscaler_options.add_argument('-d', '--driver', help='upscaling driver', action='store', default='waifu2x_caffe', choices=['waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan', 'anime4k']) upscaler_options.add_argument('-y', '--model_dir', type=pathlib.Path, 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=1) upscaler_options.add_argument('-c', '--config', type=pathlib.Path, help='video2x config file location', action='store', default=pathlib.Path(sys.argv[0]).parent.absolute() / 'video2x.json') @@ -224,6 +224,10 @@ def absolutify_paths(config): # check waifu2x_ncnn_vulkan path if not re.match('^[a-z]:', config['waifu2x_ncnn_vulkan']['waifu2x_ncnn_vulkan_path'], re.IGNORECASE): config['waifu2x_ncnn_vulkan']['waifu2x_ncnn_vulkan_path'] = current_directory / config['waifu2x_ncnn_vulkan']['waifu2x_ncnn_vulkan_path'] + + # check anime4k path + if not re.match('^[a-z]:', config['anime4k']['anime4k_path'], re.IGNORECASE): + config['anime4k']['anime4k_path'] = current_directory / config['anime4k']['anime4k_path'] # check ffmpeg path if not re.match('^[a-z]:', config['ffmpeg']['ffmpeg_path'], re.IGNORECASE): @@ -301,6 +305,12 @@ elif args.driver == 'waifu2x_ncnn_vulkan': Avalon.error('Specified waifu2x_ncnn_vulkan directory doesn\'t exist') Avalon.error('Please check the configuration file settings') raise FileNotFoundError(waifu2x_settings['waifu2x_ncnn_vulkan_path']) +elif args.driver == 'anime4k': + waifu2x_settings = config['anime4k'] + if not pathlib.Path(waifu2x_settings['anime4k_path']).is_file(): + Avalon.error('Specified anime4k directory doesn\'t exist') + Avalon.error('Please check the configuration file settings') + raise FileNotFoundError(waifu2x_settings['anime4k_path']) # read FFmpeg configuration ffmpeg_settings = config['ffmpeg']