feat: add enhance image input

use this so you don't have to modify an image before enhancement
This commit is contained in:
Manuel Schmid
2024-06-21 23:51:00 +02:00
parent c7a411a8c7
commit 40e1c82b74
2 changed files with 122 additions and 105 deletions
+27 -17
View File
@@ -113,6 +113,7 @@ class AsyncTask:
self.dino_erode_or_dilate = args.pop() self.dino_erode_or_dilate = args.pop()
self.debugging_enhance_masks_checkbox = args.pop() self.debugging_enhance_masks_checkbox = args.pop()
self.enhance_input_image = args.pop()
self.enhance_checkbox = args.pop() self.enhance_checkbox = args.pop()
self.enhance_uov_method = args.pop() self.enhance_uov_method = args.pop()
self.enhance_ctrls = [] self.enhance_ctrls = []
@@ -569,7 +570,7 @@ def worker():
H, W, C = uov_input_image.shape H, W, C = uov_input_image.shape
if advance_progress: if advance_progress:
current_progress += 1 current_progress += 1
progressbar(async_task, current_progress, f'Upscaling image from {str((H, W))} ...') progressbar(async_task, current_progress, f'Upscaling image from {str((W, H))} ...')
uov_input_image = perform_upscale(uov_input_image) uov_input_image = perform_upscale(uov_input_image)
print(f'Image upscaled.') print(f'Image upscaled.')
if '1.5x' in uov_method: if '1.5x' in uov_method:
@@ -843,7 +844,7 @@ def worker():
skip_prompt_processing, use_synthetic_refiner): skip_prompt_processing, use_synthetic_refiner):
if (async_task.current_tab == 'uov' or ( if (async_task.current_tab == 'uov' or (
async_task.current_tab == 'ip' and async_task.mixing_image_prompt_and_vary_upscale)) \ async_task.current_tab == 'ip' and async_task.mixing_image_prompt_and_vary_upscale)) \
and async_task.uov_method != flags.disabled and async_task.uov_input_image is not None: and async_task.uov_method != flags.disabled.lower() and async_task.uov_input_image is not None:
async_task.uov_input_image, skip_prompt_processing, async_task.steps = prepare_upscale( async_task.uov_input_image, skip_prompt_processing, async_task.steps = prepare_upscale(
async_task, goals, async_task.uov_input_image, async_task.uov_method, async_task.performance_selection, async_task, goals, async_task.uov_input_image, async_task.uov_method, async_task.performance_selection,
async_task.steps, 1, skip_prompt_processing=skip_prompt_processing) async_task.steps, 1, skip_prompt_processing=skip_prompt_processing)
@@ -914,6 +915,10 @@ def worker():
if len(async_task.cn_tasks[flags.cn_ip_face]) > 0: if len(async_task.cn_tasks[flags.cn_ip_face]) > 0:
clip_vision_path, ip_negative_path, ip_adapter_face_path = modules.config.downloading_ip_adapters( clip_vision_path, ip_negative_path, ip_adapter_face_path = modules.config.downloading_ip_adapters(
'face') 'face')
if async_task.current_tab == 'enhance' and async_task.enhance_input_image is not None:
goals.append('enhance')
skip_prompt_processing = True
async_task.enhance_input_image = HWC3(async_task.enhance_input_image)
return base_model_additional_loras, clip_vision_path, controlnet_canny_path, controlnet_cpds_path, inpaint_head_model_path, inpaint_image, inpaint_mask, ip_adapter_face_path, ip_adapter_path, ip_negative_path, skip_prompt_processing, use_synthetic_refiner return base_model_additional_loras, clip_vision_path, controlnet_canny_path, controlnet_cpds_path, inpaint_head_model_path, inpaint_image, inpaint_mask, ip_adapter_face_path, ip_adapter_path, ip_negative_path, skip_prompt_processing, use_synthetic_refiner
def prepare_upscale(async_task, goals, uov_input_image, uov_method, performance, steps, current_progress, def prepare_upscale(async_task, goals, uov_input_image, uov_method, performance, steps, current_progress,
@@ -1161,18 +1166,26 @@ def worker():
if async_task.freeu_enabled: if async_task.freeu_enabled:
apply_freeu(async_task) apply_freeu(async_task)
# async_task.steps can have value of uov steps here when upscale has been applied
steps, _, _, _ = apply_overrides(async_task, async_task.steps, height, width) steps, _, _, _ = apply_overrides(async_task, async_task.steps, height, width)
images_to_enhance = []
if 'enhance' in goals:
images_to_enhance += [async_task.enhance_input_image]
height, width, _ = async_task.enhance_input_image.shape
# input image already provided, processing is skipped
steps = 0
all_steps = steps * async_task.image_number all_steps = steps * async_task.image_number
# enhance_upscale_steps = 0 if async_task.enhance_checkbox and async_task.enhance_uov_method != flags.disabled.lower():
# enhance_upscale_steps_total = 0
if async_task.enhance_checkbox and async_task.enhance_uov_method != flags.disabled:
enhance_upscale_steps, _, _, _ = apply_overrides(async_task, async_task.performance_selection.steps_uov(), height, width) enhance_upscale_steps, _, _, _ = apply_overrides(async_task, async_task.performance_selection.steps_uov(), height, width)
enhance_upscale_steps_total = async_task.image_number * enhance_upscale_steps enhance_upscale_steps_total = async_task.image_number * enhance_upscale_steps
all_steps += enhance_upscale_steps_total all_steps += enhance_upscale_steps_total
if async_task.enhance_checkbox and len(async_task.enhance_ctrls) != 0: if async_task.enhance_checkbox and len(async_task.enhance_ctrls) != 0:
all_steps += async_task.image_number * len(async_task.enhance_ctrls) * steps enhance_steps, _, _, _ = apply_overrides(async_task, async_task.original_steps, height, width)
all_steps += async_task.image_number * len(async_task.enhance_ctrls) * enhance_steps
print(f'[Parameters] Denoising Strength = {denoising_strength}') print(f'[Parameters] Denoising Strength = {denoising_strength}')
@@ -1205,8 +1218,6 @@ def worker():
int(current_progress + async_task.callback_steps), int(current_progress + async_task.callback_steps),
f'Sampling step {step + 1}/{total_steps}, image {current_task_id + 1}/{total_count} ...', y)]) f'Sampling step {step + 1}/{total_steps}, image {current_task_id + 1}/{total_count} ...', y)])
generated_imgs = {}
for current_task_id, task in enumerate(tasks): for current_task_id, task in enumerate(tasks):
progressbar(async_task, current_progress, progressbar(async_task, current_progress,
f'Preparing task {current_task_id + 1}/{async_task.image_number} ...') f'Preparing task {current_task_id + 1}/{async_task.image_number} ...')
@@ -1222,7 +1233,7 @@ def worker():
preparation_steps, preparation_steps, preparation_steps, preparation_steps,
async_task.image_number) async_task.image_number)
generated_imgs[current_task_id] = imgs images_to_enhance += imgs
except ldm_patched.modules.model_management.InterruptProcessingException: except ldm_patched.modules.model_management.InterruptProcessingException:
if async_task.last_stop == 'skip': if async_task.last_stop == 'skip':
@@ -1237,7 +1248,7 @@ def worker():
execution_time = time.perf_counter() - execution_start_time execution_time = time.perf_counter() - execution_start_time
print(f'Generating and saving time: {execution_time:.2f} seconds') print(f'Generating and saving time: {execution_time:.2f} seconds')
if not async_task.enhance_checkbox or (async_task.enhance_uov_method == flags.disabled and len(async_task.enhance_ctrls) == 0): if not async_task.enhance_checkbox or (async_task.enhance_uov_method == flags.disabled.lower() and len(async_task.enhance_ctrls) == 0):
print(f'[Enhance] Skipping, preconditions aren\'t met') print(f'[Enhance] Skipping, preconditions aren\'t met')
stop_processing(async_task, processing_start_time) stop_processing(async_task, processing_start_time)
return return
@@ -1245,22 +1256,21 @@ def worker():
progressbar(async_task, current_progress, 'Processing enhance ...') progressbar(async_task, current_progress, 'Processing enhance ...')
active_enhance_tabs = len(async_task.enhance_ctrls) active_enhance_tabs = len(async_task.enhance_ctrls)
should_process_uov = async_task.enhance_uov_method != flags.disabled should_process_enhance_uov = async_task.enhance_uov_method != flags.disabled.lower()
if should_process_uov: if should_process_enhance_uov:
active_enhance_tabs += 1 active_enhance_tabs += 1
total_count = sum([len(imgs) for _, imgs in generated_imgs.items()]) * active_enhance_tabs total_count = len(images_to_enhance) * active_enhance_tabs
base_progress = current_progress base_progress = current_progress
current_task_id = -1 current_task_id = -1
done_steps_upscaling = 0 done_steps_upscaling = 0
done_steps_inpainting = 0 done_steps_inpainting = 0
enhance_steps, _, _, _ = apply_overrides(async_task, async_task.original_steps, height, width) enhance_steps, _, _, _ = apply_overrides(async_task, async_task.original_steps, height, width)
for imgs in generated_imgs.values(): for img in images_to_enhance:
for img in imgs:
enhancement_image_start_time = time.perf_counter() enhancement_image_start_time = time.perf_counter()
# upscale if not disabled or already in goals # upscale if not disabled or already in goals
if should_process_uov: if should_process_enhance_uov:
current_task_id += 1 current_task_id += 1
current_progress = int(base_progress + (100 - preparation_steps) / float(all_steps) * (done_steps_upscaling + done_steps_inpainting)) current_progress = int(base_progress + (100 - preparation_steps) / float(all_steps) * (done_steps_upscaling + done_steps_inpainting))
goals_enhance = [] goals_enhance = []
@@ -1269,7 +1279,7 @@ def worker():
async_task.performance_selection, async_task.performance_selection,
enhance_steps, current_progress) enhance_steps, current_progress)
steps, _, _, _ = apply_overrides(async_task, async_task.original_steps, height, width) steps, _, _, _ = apply_overrides(async_task, steps, height, width)
if len(goals_enhance) > 0: if len(goals_enhance) > 0:
try: try:
+8 -1
View File
@@ -325,6 +325,11 @@ with shared.gradio_root:
desc_input_image.upload(trigger_show_image_properties, inputs=desc_input_image, desc_input_image.upload(trigger_show_image_properties, inputs=desc_input_image,
outputs=desc_image_size, show_progress=False, queue=False) outputs=desc_image_size, show_progress=False, queue=False)
with gr.TabItem(label='Enhance') as enhance_tab:
with gr.Row():
with gr.Column():
enhance_input_image = grh.Image(label='Image to enhance', source='upload', type='numpy')
with gr.TabItem(label='Metadata') as metadata_tab: with gr.TabItem(label='Metadata') as metadata_tab:
with gr.Column(): with gr.Column():
metadata_input_image = grh.Image(label='For images created by Fooocus', source='upload', type='filepath') metadata_input_image = grh.Image(label='For images created by Fooocus', source='upload', type='filepath')
@@ -488,6 +493,7 @@ with shared.gradio_root:
inpaint_tab.select(lambda: 'inpaint', outputs=current_tab, queue=False, _js=down_js, show_progress=False) inpaint_tab.select(lambda: 'inpaint', outputs=current_tab, queue=False, _js=down_js, show_progress=False)
ip_tab.select(lambda: 'ip', outputs=current_tab, queue=False, _js=down_js, show_progress=False) ip_tab.select(lambda: 'ip', outputs=current_tab, queue=False, _js=down_js, show_progress=False)
desc_tab.select(lambda: 'desc', outputs=current_tab, queue=False, _js=down_js, show_progress=False) desc_tab.select(lambda: 'desc', outputs=current_tab, queue=False, _js=down_js, show_progress=False)
enhance_tab.select(lambda: 'enhance', outputs=current_tab, queue=False, _js=down_js, show_progress=False)
metadata_tab.select(lambda: 'metadata', outputs=current_tab, queue=False, _js=down_js, show_progress=False) metadata_tab.select(lambda: 'metadata', outputs=current_tab, queue=False, _js=down_js, show_progress=False)
enhance_checkbox.change(lambda x: gr.update(visible=x), inputs=enhance_checkbox, enhance_checkbox.change(lambda x: gr.update(visible=x), inputs=enhance_checkbox,
@@ -930,7 +936,8 @@ with shared.gradio_root:
ctrls += [save_metadata_to_images, metadata_scheme] ctrls += [save_metadata_to_images, metadata_scheme]
ctrls += ip_ctrls ctrls += ip_ctrls
ctrls += [debugging_dino, dino_erode_or_dilate, debugging_enhance_masks_checkbox, enhance_checkbox, enhance_uov_method] ctrls += [debugging_dino, dino_erode_or_dilate, debugging_enhance_masks_checkbox,
enhance_input_image, enhance_checkbox, enhance_uov_method]
ctrls += enhance_ctrls ctrls += enhance_ctrls
def parse_meta(raw_prompt_txt, is_generating): def parse_meta(raw_prompt_txt, is_generating):