sync - broken currently due to refactoring!!
This commit is contained in:
339
bin/engines/fsr.py
Normal file
339
bin/engines/fsr.py
Normal file
@@ -0,0 +1,339 @@
|
||||
import os
|
||||
import multiprocessing
|
||||
import time
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
class FSRScaler:
|
||||
def __init__(self):
|
||||
self.os_type = sys.platform
|
||||
self.command = ""
|
||||
self.tmppath = ""
|
||||
self.videometa = {}
|
||||
|
||||
def fsrScaler ( self, tmppath, filepath, threads, fsrpath, quality_setting, sharpening, scaling, filetype, mode ):
|
||||
# Locate Images and assemble FSR-Command
|
||||
self.file_list = []
|
||||
self.filelist = os.listdir(tmppath)
|
||||
self.filelist.pop(0)
|
||||
self.filelist.sort()
|
||||
self.number = 0
|
||||
if sharpening != '' and sharpening != None:
|
||||
for self.file in self.filelist:
|
||||
self.number += 1
|
||||
if ( self.os_type == 'win32' ):
|
||||
self.file_list.append( f"{tmppath}{self.file} {tmppath}up\\up{str(self.number).zfill(8)}.{ filetype } " );
|
||||
else:
|
||||
self.file_list.append( f"{tmppath}{self.file} {tmppath}up/up{str(self.number).zfill(8)}.{ filetype } " );
|
||||
try:
|
||||
os.mkdir( f'{tmppath}up' )
|
||||
except FileExistsError:
|
||||
pass
|
||||
else:
|
||||
for self.file in self.filelist:
|
||||
self.number += 1
|
||||
if ( self.os_type == 'win32' ):
|
||||
self.file_list.append( f"{tmppath}{self.file} {tmppath}sc\\ig{str(self.number).zfill(8)}.{ filetype } " );
|
||||
else:
|
||||
self.file_list.append( f"{tmppath}{self.file} {tmppath}sc/ig{str(self.number).zfill(8)}.{ filetype } " );
|
||||
|
||||
try:
|
||||
os.mkdir( f'{tmppath}sc' )
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
if ( self.os_type == 'win32' ):
|
||||
self.maxlength = 8000
|
||||
else:
|
||||
self.maxlength = 31900
|
||||
self.pos = 1
|
||||
|
||||
############################################
|
||||
#
|
||||
# Thread optimisation: Divide workload up into different threads & upscale using helper function
|
||||
#
|
||||
############################################
|
||||
self.threads = threads
|
||||
if ( threads > multiprocessing.cpu_count() ):
|
||||
self.threads = multiprocessing.cpu_count();
|
||||
|
||||
if ( not scaling ):
|
||||
engines = { 'NN': 'NearestNeighbor', 'fsr':'FidelityFX Super Resolution' }
|
||||
print( f'\n\n==> Upscaling using { self.threads } threads <==\n\n' );
|
||||
print( f'\n\n==> Upscaling Engine is { engines[ mode ] } <==\n\n' );
|
||||
|
||||
time.sleep( 2 );
|
||||
|
||||
self.command_list = [];
|
||||
self.file_list_length = len( self.file_list );
|
||||
for i in range( self.threads ):
|
||||
self.files = '';
|
||||
for _ in range( int( self.file_list_length // self.threads ) ):
|
||||
self.files += self.file_list.pop( 0 );
|
||||
|
||||
if ( i == self.threads - 1 ):
|
||||
for element in self.file_list:
|
||||
self.files += element;
|
||||
self.command_list.append( ( self.files, fsrpath, quality_setting, i, self.maxlength, self.os_type ) )
|
||||
|
||||
self.pool = multiprocessing.Pool( self.threads )
|
||||
if ( mode == 'B' ):
|
||||
self.pool.starmap( bilinearEngine, self.command_list );
|
||||
elif ( mode == 'fsr' ):
|
||||
self.pool.starmap( upscalerEngine, self.command_list );
|
||||
self.pool.close();
|
||||
self.pool.join();
|
||||
|
||||
if sharpening != '' and sharpening != None:
|
||||
print( f'\n\n\n==> Sharpening using { self.threads } threads <==\n\n' );
|
||||
time.sleep( 2 );
|
||||
|
||||
self.pathSharpening = tmppath
|
||||
|
||||
if ( not scaling ):
|
||||
if ( self.os_type == 'win32' ):
|
||||
self.pathSharpening += 'up\\'
|
||||
elif ( self.os_type == 'linux' ):
|
||||
self.pathSharpening += 'up/'
|
||||
|
||||
time.sleep( 2 );
|
||||
try:
|
||||
os.mkdir( f'{tmppath}sc' )
|
||||
except FileExistsError:
|
||||
pass
|
||||
# Locate Images and assemble FSR-Command
|
||||
self.file_list = []
|
||||
self.filelist = os.listdir( self.pathSharpening )
|
||||
self.filelist.pop(0)
|
||||
self.filelist.sort()
|
||||
self.number = 0
|
||||
for self.file in self.filelist:
|
||||
self.number += 1
|
||||
if ( self.os_type == 'win32' ):
|
||||
self.file_list.append( f"{self.pathSharpening}{self.file} {tmppath}sc\\ig{str(self.number).zfill(8)}.{ filetype } " );
|
||||
else:
|
||||
self.file_list.append( f"{self.pathSharpening}{self.file} {tmppath}sc/ig{str(self.number).zfill(8)}.{ filetype } " );
|
||||
|
||||
if ( self.os_type == 'win32' ):
|
||||
self.maxlength = 8000
|
||||
else:
|
||||
self.maxlength = 31900
|
||||
self.pos = 1
|
||||
|
||||
# assemble command list
|
||||
self.command_list = [];
|
||||
self.file_list_length = len( self.file_list );
|
||||
for i in range( self.threads ):
|
||||
self.files = '';
|
||||
for _ in range( int( self.file_list_length // self.threads ) ):
|
||||
self.files += self.file_list.pop( 0 );
|
||||
|
||||
if ( i == self.threads - 1 ):
|
||||
for element in self.file_list:
|
||||
self.files += element;
|
||||
self.command_list.append( ( self.files, fsrpath, i, self.maxlength, self.os_type, sharpening, not sharpening ) )
|
||||
|
||||
self.pool = multiprocessing.Pool( self.threads )
|
||||
self.pool.starmap( sharpeningEngine, self.command_list );
|
||||
self.pool.close();
|
||||
self.pool.join();
|
||||
|
||||
# Add return values
|
||||
|
||||
|
||||
def upscalerEngine ( files, fsrpath, quality_setting, number, maxlength, os_type ):
|
||||
files = files;
|
||||
# Refactoring of commands that are longer than 32K characters
|
||||
fileout = [];
|
||||
pos = 0;
|
||||
if len( files ) > maxlength:
|
||||
while files[maxlength - pos:maxlength - pos + 1] != ' ':
|
||||
pos += 1
|
||||
file_processing = files[:maxlength - pos]
|
||||
if file_processing[len(file_processing) - 14:len(file_processing) - 12] == 'ig':
|
||||
pos += 5
|
||||
else:
|
||||
pass
|
||||
while files[maxlength - pos:maxlength - pos + 1] != ' ':
|
||||
pos += 1
|
||||
fileout.append(files[:maxlength - pos])
|
||||
filesopt = files[maxlength - pos:]
|
||||
posx = 0
|
||||
posy = maxlength
|
||||
|
||||
# Command refactoring for commands that are longer than 64K characters
|
||||
if len(filesopt) > maxlength:
|
||||
while len(filesopt) > maxlength:
|
||||
posx += maxlength - pos
|
||||
posy += maxlength - pos
|
||||
pos = 1
|
||||
while files[posy - pos:posy - pos + 1] != ' ':
|
||||
pos += 1
|
||||
file_processing = files[posx:posy - pos]
|
||||
if file_processing[len(file_processing) - 14:len(file_processing) - 12] == 'ig':
|
||||
pos += 5
|
||||
while files[posy - pos:posy - pos + 1] != ' ':
|
||||
pos += 1
|
||||
|
||||
file_processing = files[posx:posy - pos]
|
||||
fileout.append(file_processing)
|
||||
filesopt = files[posy - pos:]
|
||||
fileout.append(filesopt)
|
||||
else:
|
||||
fileout.append(files[maxlength - pos:])
|
||||
else:
|
||||
fileout.append(files)
|
||||
|
||||
# Upscaling images
|
||||
print( '\n\n\nUpscaling images... \n\n\n\n\n\n PROCESS: ', number, '\n\n\n' )
|
||||
|
||||
while len( fileout ) > 0:
|
||||
files_handle = fileout.pop(0)
|
||||
if os_type == 'linux':
|
||||
command_us = f'wine {fsrpath} -Scale {quality_setting} {quality_setting} {files_handle}'
|
||||
elif os_type == 'win32':
|
||||
command_us = f'FidelityFX_CLI -Scale {quality_setting} {quality_setting} {files_handle}'
|
||||
else:
|
||||
print( 'OS CURRENTLY UNSUPPORTED!' )
|
||||
return False
|
||||
sub = subprocess.Popen( command_us, shell=True );
|
||||
sub.wait();
|
||||
time.sleep(3)
|
||||
print( '\n\nCompleted executing Job\n\n\n PROCESS: ', number, '\n\n\n' );
|
||||
|
||||
|
||||
def bilinearEngine ( files, fsrpath, quality_setting, number, maxlength, os_type, version ):
|
||||
if ( version == 'HQC' ):
|
||||
scaler = 'HighQualityCubic'
|
||||
files = files;
|
||||
# Refactoring of commands that are longer than 32K characters
|
||||
fileout = [];
|
||||
pos = 0;
|
||||
if len( files ) > maxlength:
|
||||
while files[maxlength - pos:maxlength - pos + 1] != ' ':
|
||||
pos += 1
|
||||
file_processing = files[:maxlength - pos]
|
||||
if file_processing[len(file_processing) - 14:len(file_processing) - 12] == 'ig':
|
||||
pos += 5
|
||||
while files[maxlength - pos:maxlength - pos + 1] != ' ':
|
||||
pos += 1
|
||||
fileout.append(files[:maxlength - pos])
|
||||
filesopt = files[maxlength - pos:]
|
||||
posx = 0
|
||||
posy = maxlength
|
||||
|
||||
# Command refactoring for commands that are longer than 64K characters
|
||||
if len(filesopt) > maxlength:
|
||||
while len(filesopt) > maxlength:
|
||||
posx += maxlength - pos
|
||||
posy += maxlength - pos
|
||||
pos = 1
|
||||
while files[posy - pos:posy - pos + 1] != ' ':
|
||||
pos += 1
|
||||
file_processing = files[posx:posy - pos]
|
||||
if file_processing[len(file_processing) - 14:len(file_processing) - 12] == 'ig':
|
||||
pos += 5
|
||||
else:
|
||||
pass
|
||||
while files[posy - pos:posy - pos + 1] != ' ':
|
||||
pos += 1
|
||||
|
||||
file_processing = files[posx:posy - pos]
|
||||
fileout.append(file_processing)
|
||||
filesopt = files[posy - pos:]
|
||||
fileout.append(filesopt)
|
||||
else:
|
||||
fileout.append(files[maxlength - pos:])
|
||||
else:
|
||||
fileout.append(files)
|
||||
|
||||
# Upscaling images
|
||||
print( '\n\n\nUpscaling images... \n\n\n\n\n\n PROCESS: ', number, '\n\n\n' )
|
||||
|
||||
while len( fileout ) > 0:
|
||||
files_handle = fileout.pop(0)
|
||||
if os_type == 'linux':
|
||||
command_us = f'wine {fsrpath} -Mode NearestNeighbor -Scale {quality_setting} {quality_setting} {files_handle}'
|
||||
elif os_type == 'win32':
|
||||
command_us = f'FidelityFX_CLI -Mode NearestNeighbor -Scale {quality_setting} {quality_setting} {files_handle}'
|
||||
else:
|
||||
print( 'OS CURRENTLY UNSUPPORTED!' )
|
||||
return False
|
||||
sub = subprocess.Popen( command_us, shell=True );
|
||||
sub.wait();
|
||||
time.sleep(3)
|
||||
print( '\n\nCompleted executing Job\n\n\n PROCESS: ', number, '\n\n\n' );
|
||||
|
||||
########################
|
||||
#
|
||||
# Sharpening
|
||||
#
|
||||
#######################
|
||||
|
||||
def sharpeningEngine ( files, fsrpath, number, maxlength, os_type, sharpening, didUpscale ):
|
||||
files = files;
|
||||
# Refactoring of commands that are longer than 32K characters
|
||||
fileout = [];
|
||||
pos = 0;
|
||||
if len( files ) > maxlength:
|
||||
while files[maxlength - pos:maxlength - pos + 1] != ' ':
|
||||
pos += 1
|
||||
file_processing = files[:maxlength - pos]
|
||||
if ( didUpscale ):
|
||||
if file_processing[len(file_processing) - 14:len(file_processing) - 12] == 'up':
|
||||
pos += 5
|
||||
else:
|
||||
if file_processing[len(file_processing) - 17:len(file_processing) - 15] == 'ru':
|
||||
pos += 8
|
||||
while files[maxlength - pos:maxlength - pos + 1] != ' ':
|
||||
pos += 1
|
||||
fileout.append(files[:maxlength - pos])
|
||||
filesopt = files[maxlength - pos:]
|
||||
posx = 0
|
||||
posy = maxlength
|
||||
|
||||
# Command refactoring for commands that are longer than 64K characters
|
||||
if len(filesopt) > maxlength:
|
||||
while len(filesopt) > maxlength:
|
||||
posx += maxlength - pos
|
||||
posy += maxlength - pos
|
||||
pos = 1
|
||||
while files[posy - pos:posy - pos + 1] != ' ':
|
||||
pos += 1
|
||||
file_processing = files[posx:posy - pos]
|
||||
if ( didUpscale ):
|
||||
if file_processing[len(file_processing) - 14:len(file_processing) - 12] == 'up':
|
||||
pos += 5
|
||||
else:
|
||||
if file_processing[len(file_processing) - 17:len(file_processing) - 15] == 'ru':
|
||||
pos += 8
|
||||
while files[posy - pos:posy - pos + 1] != ' ':
|
||||
pos += 1
|
||||
|
||||
file_processing = files[posx:posy - pos]
|
||||
fileout.append(file_processing)
|
||||
filesopt = files[posy - pos:]
|
||||
fileout.append(filesopt)
|
||||
else:
|
||||
fileout.append(files[maxlength - pos:])
|
||||
else:
|
||||
fileout.append(files)
|
||||
|
||||
# Upscaling images
|
||||
print( '\n\n\nSharpening images... \n\n\n\n\n\n PROCESS: ', number, '\n\n\n' )
|
||||
|
||||
while len( fileout ) > 0:
|
||||
files_handle = fileout.pop(0)
|
||||
print( '\n\n\n PROCESS: ', number, '\nRunning sharpening filter\n\n\n' );
|
||||
if os_type == 'linux':
|
||||
command_sharpening = f'wine {fsrpath} -Mode CAS -Sharpness {sharpening} {files_handle}'
|
||||
elif os_type == 'win32':
|
||||
command_sharpening = f'FidelityFX_CLI -Mode CAS -Sharpness {sharpening} {files_handle}'
|
||||
else:
|
||||
print( 'OS CURRENTLY UNSUPPORTED!' )
|
||||
return False
|
||||
sub2 = subprocess.Popen( command_sharpening, shell=True );
|
||||
sub2.wait()
|
||||
time.sleep(3)
|
||||
print( '\n\nCompleted executing Job\n\n\n PROCESS: ', number, '\n\n\n' );
|
||||
|
||||
90
bin/engines/ss.py
Normal file
90
bin/engines/ss.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import os
|
||||
import subprocess
|
||||
import multiprocessing
|
||||
import time
|
||||
import sys
|
||||
|
||||
class SpecialScaler:
|
||||
def __init__(self):
|
||||
self.os_type = sys.platform
|
||||
self.command = ""
|
||||
self.tmppath = ""
|
||||
self.videometa = {}
|
||||
|
||||
def superScaler ( self, tmppath, threads, quality_setting, os_platform, model ):
|
||||
print( '\n\n==> Preparing to upscale videos <==\n\n==> You will see a lot of numbers flying by showing the progress of the upscaling of each individual image.\n==> This process might take a long time, depending on the length of the video.\n\n')
|
||||
time.sleep( 2 );
|
||||
|
||||
try:
|
||||
os.mkdir( f'{tmppath}sc' )
|
||||
except FileExistsError:
|
||||
pass
|
||||
if ( os_platform == 'win32' ):
|
||||
self.command = f'realesrgan-ncnn-vulkan -i {tmppath} -o {tmppath}sc -s {quality_setting} -j {threads}:{threads}:{threads} -n {model}'
|
||||
elif ( os_platform == 'linux' ):
|
||||
self.command = f'wine ./bin/lib/realesrgan-ncnn-vulkan.exe -i {tmppath} -o {tmppath}sc -s {quality_setting} -j {threads}:{threads}:{threads} -n {model}'
|
||||
os.system( self.command );
|
||||
|
||||
|
||||
def specialSuperScaler ( self, tmppath, threads, quality_setting, model ):
|
||||
self.fileList = os.listdir( tmppath )
|
||||
self.fileList.pop( 0 )
|
||||
self.fileList.sort()
|
||||
if ( threads > multiprocessing.cpu_count() * 2 ):
|
||||
self.threads = multiprocessing.cpu_count() * 2;
|
||||
else:
|
||||
self.threads = threads
|
||||
|
||||
self.fileCount = len( self.fileList ) // self.threads
|
||||
self.spareFiles = len( self.fileList ) % self.threads
|
||||
|
||||
self.cmdList = [];
|
||||
|
||||
for t in range( threads ):
|
||||
try:
|
||||
os.mkdir( f'{tmppath}{t}' )
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
self.base = t * self.fileCount;
|
||||
if ( self.os_type == 'win32' ):
|
||||
for j in range( self.fileCount ):
|
||||
os.rename( f'{tmppath}{self.fileList[ self.base + j ] }', f'{tmppath}{ t }\\{self.fileList[ self.base + j ] }' )
|
||||
elif ( self.os_type == 'linux' ):
|
||||
for j in range( self.fileCount ):
|
||||
os.rename( f'{tmppath}{self.fileList[ self.base + j ] }', f'{tmppath}{ t }/{self.fileList[ self.base + j ] }' )
|
||||
|
||||
self.cmdList.append( ( tmppath, t, quality_setting, model, self.os_type ) )
|
||||
|
||||
try:
|
||||
os.mkdir( f'{tmppath}{self.threads + 1}' )
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
if ( self.os_type == 'win32' ):
|
||||
for k in range( self.spareFiles ):
|
||||
os.rename( f'{tmppath}{self.fileList[ self.threads * self.fileCount + k ] }', f'{tmppath}{ t }\\{self.fileList[ self.threads * self.fileCount + k ] }' )
|
||||
elif ( self.os_type == 'linux' ):
|
||||
for k in range( self.spareFiles ):
|
||||
os.rename( f'{tmppath}{self.fileList[ self.threads * self.fileCount + k ] }', f'{tmppath}{ self.threads + 1 }/{self.fileList[ self.threads * self.fileCount + k ] }' )
|
||||
|
||||
try:
|
||||
os.mkdir( f'{tmppath}sc' )
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
self.pool_ss = multiprocessing.Pool( self.threads )
|
||||
self.pool_ss.starmap( specialScalerEngine, self.cmdList );
|
||||
self.pool_ss.close();
|
||||
self.pool_ss.join();
|
||||
|
||||
specialScalerEngine( tmppath, t, quality_setting, model, self.os_type )
|
||||
|
||||
|
||||
def specialScalerEngine ( tmppath, tNumber, quality_setting, model, os_type ):
|
||||
if ( os_type == 'win32' ):
|
||||
command = f'realesrgan-ncnn-vulkan -i {tmppath}{tNumber} -o {tmppath}sc -s {quality_setting} -n {model}'
|
||||
elif ( os_type == 'linux' ):
|
||||
command = f'wine ./bin/lib/realesrgan-ncnn-vulkan.exe -i {tmppath}{tNumber} -o {tmppath}sc -s {quality_setting} -n {model}'
|
||||
sub = subprocess.Popen( command, shell=True );
|
||||
sub.wait();
|
||||
Reference in New Issue
Block a user