2022-08-26 07:15:42 +00:00
|
|
|
"""
|
2022-08-26 02:49:15 +00:00
|
|
|
Readline helper functions for dream.py (linux and mac only).
|
2022-08-26 07:15:42 +00:00
|
|
|
"""
|
2022-08-26 02:49:15 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import atexit
|
2022-08-26 07:15:42 +00:00
|
|
|
|
2022-08-26 02:49:15 +00:00
|
|
|
# ---------------readline utilities---------------------
|
|
|
|
try:
|
|
|
|
import readline
|
2022-08-26 07:15:42 +00:00
|
|
|
|
2022-08-26 02:49:15 +00:00
|
|
|
readline_available = True
|
|
|
|
except:
|
|
|
|
readline_available = False
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
|
|
|
|
class Completer:
|
|
|
|
def __init__(self, options):
|
2022-08-26 02:49:15 +00:00
|
|
|
self.options = sorted(options)
|
|
|
|
return
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
def complete(self, text, state):
|
2022-08-26 02:49:15 +00:00
|
|
|
buffer = readline.get_line_buffer()
|
|
|
|
|
2022-09-18 13:47:57 +00:00
|
|
|
if text.startswith(('-I', '--init_img','-M','--init_mask',
|
|
|
|
'--init_color')):
|
2022-08-30 19:26:02 +00:00
|
|
|
return self._path_completions(text, state, ('.png','.jpg','.jpeg'))
|
2022-08-26 02:49:15 +00:00
|
|
|
|
add ability to post-process images from the CLI
- supports gfpgan, esrgan, codeformer and embiggen
- To use:
dream> !fix ./outputs/img-samples/000056.292144555.png -ft gfpgan -U2 -G0.8
dream> !fix ./outputs/img-samples/000056.292144555.png -ft codeformer -G 0.8
dream> !fix ./outputs/img-samples/000056.29214455.png -U4
dream> !fix ./outputs/img-samples/000056.292144555.png -embiggen 1.5
The first example invokes gfpgan to fix faces and esrgan to upscale.
The second example invokes codeformer to fix faces, no upscaling
The third example uses esrgan to upscale 4X
The four example runs embiggen to enlarge 1.5X
- This is very preliminary work. There are some anomalies to note:
1. The syntax is non-obvious. I would prefer something like:
!fix esrgan,gfpgan
!fix esrgan
!fix embiggen,codeformer
However, this will require refactoring the gfpgan and embiggen
code.
2. Images generated using gfpgan, esrgan or codeformer all are named
"xxxxxx.xxxxxx.postprocessed.png" and the original is saved.
However, the prefix is a new one that is not related to the
original.
3. Images generated using embiggen are named "xxxxx.xxxxxxx.png",
and once again the prefix is new. I'm not sure whether the
prefix should be aligned with the original file's prefix or not.
Probably not, but opinions welcome.
2022-09-18 21:26:09 +00:00
|
|
|
if buffer.strip().endswith('pp') or text.startswith(('.', '/')):
|
|
|
|
return self._path_completions(text, state, ('.png','.jpg','.jpeg'))
|
2022-08-26 02:49:15 +00:00
|
|
|
|
|
|
|
response = None
|
|
|
|
if state == 0:
|
|
|
|
# This is the first time for this text, so build a match list.
|
|
|
|
if text:
|
2022-08-26 07:15:42 +00:00
|
|
|
self.matches = [
|
|
|
|
s for s in self.options if s and s.startswith(text)
|
|
|
|
]
|
2022-08-26 02:49:15 +00:00
|
|
|
else:
|
|
|
|
self.matches = self.options[:]
|
|
|
|
|
|
|
|
# Return the state'th item from the match list,
|
|
|
|
# if we have that many.
|
|
|
|
try:
|
|
|
|
response = self.matches[state]
|
|
|
|
except IndexError:
|
|
|
|
response = None
|
|
|
|
return response
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
def _path_completions(self, text, state, extensions):
|
2022-08-26 02:49:15 +00:00
|
|
|
# get the path so far
|
2022-09-06 00:40:10 +00:00
|
|
|
# TODO: replace this mess with a regular expression match
|
2022-08-26 02:49:15 +00:00
|
|
|
if text.startswith('-I'):
|
2022-08-26 07:15:42 +00:00
|
|
|
path = text.replace('-I', '', 1).lstrip()
|
2022-08-26 02:49:15 +00:00
|
|
|
elif text.startswith('--init_img='):
|
2022-08-26 07:15:42 +00:00
|
|
|
path = text.replace('--init_img=', '', 1).lstrip()
|
2022-09-06 00:40:10 +00:00
|
|
|
elif text.startswith('--init_mask='):
|
|
|
|
path = text.replace('--init_mask=', '', 1).lstrip()
|
|
|
|
elif text.startswith('-M'):
|
|
|
|
path = text.replace('-M', '', 1).lstrip()
|
2022-09-18 13:47:57 +00:00
|
|
|
elif text.startswith('--init_color='):
|
|
|
|
path = text.replace('--init_color=', '', 1).lstrip()
|
2022-08-26 02:49:15 +00:00
|
|
|
else:
|
|
|
|
path = text
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
matches = list()
|
2022-08-26 02:49:15 +00:00
|
|
|
|
|
|
|
path = os.path.expanduser(path)
|
2022-08-26 07:15:42 +00:00
|
|
|
if len(path) == 0:
|
|
|
|
matches.append(text + './')
|
2022-08-26 02:49:15 +00:00
|
|
|
else:
|
2022-08-26 07:15:42 +00:00
|
|
|
dir = os.path.dirname(path)
|
2022-08-26 02:49:15 +00:00
|
|
|
dir_list = os.listdir(dir)
|
|
|
|
for n in dir_list:
|
2022-08-26 07:15:42 +00:00
|
|
|
if n.startswith('.') and len(n) > 1:
|
2022-08-26 02:49:15 +00:00
|
|
|
continue
|
2022-08-26 07:15:42 +00:00
|
|
|
full_path = os.path.join(dir, n)
|
2022-08-26 02:49:15 +00:00
|
|
|
if full_path.startswith(path):
|
|
|
|
if os.path.isdir(full_path):
|
2022-08-26 07:15:42 +00:00
|
|
|
matches.append(
|
|
|
|
os.path.join(os.path.dirname(text), n) + '/'
|
|
|
|
)
|
2022-08-26 02:49:15 +00:00
|
|
|
elif n.endswith(extensions):
|
2022-08-26 07:15:42 +00:00
|
|
|
matches.append(os.path.join(os.path.dirname(text), n))
|
2022-08-26 02:49:15 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
response = matches[state]
|
|
|
|
except IndexError:
|
|
|
|
response = None
|
|
|
|
return response
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
|
2022-08-26 02:49:15 +00:00
|
|
|
if readline_available:
|
2022-08-26 07:15:42 +00:00
|
|
|
readline.set_completer(
|
|
|
|
Completer(
|
|
|
|
[
|
2022-08-30 02:34:09 +00:00
|
|
|
'--steps','-s',
|
|
|
|
'--seed','-S',
|
|
|
|
'--iterations','-n',
|
|
|
|
'--width','-W','--height','-H',
|
|
|
|
'--cfg_scale','-C',
|
|
|
|
'--grid','-g',
|
|
|
|
'--individual','-i',
|
|
|
|
'--init_img','-I',
|
2022-09-06 00:40:10 +00:00
|
|
|
'--init_mask','-M',
|
2022-09-18 13:47:57 +00:00
|
|
|
'--init_color',
|
2022-08-30 02:34:09 +00:00
|
|
|
'--strength','-f',
|
|
|
|
'--variants','-v',
|
|
|
|
'--outdir','-o',
|
|
|
|
'--sampler','-A','-m',
|
|
|
|
'--embedding_path',
|
|
|
|
'--device',
|
|
|
|
'--grid','-g',
|
|
|
|
'--gfpgan_strength','-G',
|
|
|
|
'--upscale','-U',
|
|
|
|
'-save_orig','--save_original',
|
|
|
|
'--skip_normalize','-x',
|
|
|
|
'--log_tokenization','t',
|
2022-08-26 07:15:42 +00:00
|
|
|
]
|
|
|
|
).complete
|
|
|
|
)
|
|
|
|
readline.set_completer_delims(' ')
|
2022-08-26 02:49:15 +00:00
|
|
|
readline.parse_and_bind('tab: complete')
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
histfile = os.path.join(os.path.expanduser('~'), '.dream_history')
|
2022-08-26 02:49:15 +00:00
|
|
|
try:
|
|
|
|
readline.read_history_file(histfile)
|
|
|
|
readline.set_history_length(1000)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2022-08-26 07:15:42 +00:00
|
|
|
atexit.register(readline.write_history_file, histfile)
|