Skip to content

Commit

Permalink
UPDATE: Force Upscale even if no face
Browse files Browse the repository at this point in the history
FR #116
  • Loading branch information
Gourieff committed Feb 2, 2024
1 parent c5ecf9a commit c745fa2
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img src="https://github.com/Gourieff/Assets/raw/main/sd-webui-reactor/ReActor_logo_NEW_EN.png?raw=true" alt="logo" width="180px"/>

![Version](https://img.shields.io/badge/version-0.6.1_beta2-green?style=for-the-badge&labelColor=darkgreen)
![Version](https://img.shields.io/badge/version-0.6.1_beta3-green?style=for-the-badge&labelColor=darkgreen)

<a href="https://boosty.to/artgourieff" target="_blank">
<img src="https://lovemet.ru/www/boosty.jpg" width="108" alt="Support Me on Boosty"/>
Expand Down Expand Up @@ -40,6 +40,10 @@

## What's new in the latest updates

### 0.6.1 <sub><sup>BETA3

- 'Force Upscale' option inside the 'Upscale' tab: ReActor will run the Upscaler even if there's no face is detected (FR https://github.com/Gourieff/sd-webui-reactor/issues/116)

### 0.6.1 <sub><sup>BETA2

- 'Save original' option works fine now when you select 'Multiple Images' or 'Source Folder'
Expand Down
6 changes: 5 additions & 1 deletion README_RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img src="https://github.com/Gourieff/Assets/raw/main/sd-webui-reactor/ReActor_logo_NEW_RU.png?raw=true" alt="logo" width="180px"/>

![Version](https://img.shields.io/badge/версия-0.6.1_beta2-green?style=for-the-badge&labelColor=darkgreen)
![Version](https://img.shields.io/badge/версия-0.6.1_beta3-green?style=for-the-badge&labelColor=darkgreen)

<a href="https://boosty.to/artgourieff" target="_blank">
<img src="https://lovemet.ru/www/boosty.jpg" width="108" alt="Поддержать проект на Boosty"/>
Expand Down Expand Up @@ -39,6 +39,10 @@

## Что нового в последних обновлениях

### 0.6.1 <sub><sup>BETA3

- Опция 'Force Upscale' внутри вкладки 'Upscale': апскейл выполнится, даже если не было обнаружено ни одного лица (FR https://github.com/Gourieff/sd-webui-reactor/issues/116)

### 0.6.1 <sub><sup>BETA2

- Опция 'Save original' теперь работает правильно, когда вы выбираете 'Multiple Images' или 'Source Folder'
Expand Down
1 change: 1 addition & 0 deletions example/api_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"C:\PATH_TO_FACES_IMAGES", #24 The path to the folder containing source faces images, don't forger to set #22 to 2
None, #25 skip it for API
True, #26 Randomly select an image from the path
True, #27 Force Upscale even if no face found
]

# The args for ReActor can be found by
Expand Down
3 changes: 2 additions & 1 deletion example/api_external.curl
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ curl -X POST \
"select_source": 1,
"face_model": "elena.safetensors",
"source_folder": "C:/faces",
"random_image": 1
"random_image": 1,
"upscale_force": 1
}'
3 changes: 2 additions & 1 deletion example/api_external.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"select_source": 1,
"face_model": "elena.safetensors",
"source_folder": "C:/faces",
"random_image": 1
"random_image": 1,
"upscale_force": 1
}
20 changes: 14 additions & 6 deletions reactor_ui/reactor_upscale_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ def update_upscalers_list(selected: str):
# TAB UPSCALE
def show(show_br: bool = True):
with gr.Tab("Upscale"):
restore_first = gr.Checkbox(
True,
label="1. Restore Face -> 2. Upscale (-Uncheck- if you want vice versa)",
info="Postprocessing Order"
)
with gr.Row():
restore_first = gr.Checkbox(
True,
label="1. Restore Face -> 2. Upscale (-Uncheck- if you want vice versa)",
info="Postprocessing Order",
scale=2
)
upscale_force = gr.Checkbox(
False,
label="Force Upscale",
info="Upscale anyway - even if no face found",
scale=1
)
with gr.Row():
upscaler_name = gr.Dropdown(
choices=[upscaler.name for upscaler in shared.sd_upscalers],
Expand All @@ -36,4 +44,4 @@ def show(show_br: bool = True):
upscaler_visibility = gr.Slider(
0, 1, 1, step=0.1, label="Upscaler Visibility (if scale = 1)"
)
return restore_first, upscaler_name, upscaler_scale, upscaler_visibility
return restore_first, upscaler_name, upscaler_scale, upscaler_visibility, upscale_force
8 changes: 5 additions & 3 deletions scripts/reactor_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''
Thanks SpenserCai for the original version of the roop api script
-----------------------------------
--- ReActor External API v1.0.3 ---
--- ReActor External API v1.0.4 ---
-----------------------------------
'''
import os, glob
Expand Down Expand Up @@ -76,7 +76,8 @@ async def reactor_image(
select_source: int = Body(0,title="Select Source, 0 - Image, 1 - Face Model, 2 - Source Folder"),
face_model: str = Body("None",title="Filename of the face model (from 'models/reactor/faces'), e.g. elena.safetensors"),
source_folder: str = Body("",title="The path to the folder containing source faces images"),
random_image: int = Body(0,title="Randomly select an image from the path")
random_image: int = Body(0,title="Randomly select an image from the path"),
upscale_force: int = Body(0,title="Force Upscale even if no face found")
):
s_image = api.decode_base64_to_image(source_image) if select_source == 0 else None
t_image = api.decode_base64_to_image(target_image)
Expand All @@ -87,7 +88,8 @@ async def reactor_image(
restore_first_bool = True if restore_first == 1 else False
mask_face = True if mask_face == 1 else False
random_image = False if random_image == 0 else True
up_options = EnhancementOptions(do_restore_first=restore_first_bool, scale=scale, upscaler=get_upscaler(upscaler), upscale_visibility=upscale_visibility,face_restorer=get_face_restorer(face_restorer),restorer_visibility=restorer_visibility,codeformer_weight=codeformer_weight)
upscale_force = False if upscale_force == 0 else True
up_options = EnhancementOptions(do_restore_first=restore_first_bool, scale=scale, upscaler=get_upscaler(upscaler), upscale_visibility=upscale_visibility,face_restorer=get_face_restorer(face_restorer),restorer_visibility=restorer_visibility,codeformer_weight=codeformer_weight,upscale_force=upscale_force)
use_model = get_full_model(model)
if use_model is None:
Exception("Model not found")
Expand Down
17 changes: 14 additions & 3 deletions scripts/reactor_faceswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def ui(self, is_img2img):
img, imgs, select_source, face_model, source_folder, save_original, mask_face, source_faces_index, gender_source, faces_index, gender_target, face_restorer_name, face_restorer_visibility, codeformer_weight, swap_in_source, swap_in_generated, random_image = ui_main.show(is_img2img=is_img2img, **msgs)

# TAB UPSCALE
restore_first, upscaler_name, upscaler_scale, upscaler_visibility = ui_upscale.show()
restore_first, upscaler_name, upscaler_scale, upscaler_visibility, upscale_force = ui_upscale.show()

# TAB TOOLS
ui_tools.show()
Expand Down Expand Up @@ -104,6 +104,7 @@ def ui(self, is_img2img):
source_folder,
imgs,
random_image,
upscale_force
]


Expand All @@ -124,13 +125,14 @@ def face_restorer(self) -> FaceRestoration:
@property
def enhancement_options(self) -> EnhancementOptions:
return EnhancementOptions(
do_restore_first = self.restore_first,
do_restore_first=self.restore_first,
scale=self.upscaler_scale,
upscaler=self.upscaler,
face_restorer=self.face_restorer,
upscale_visibility=self.upscaler_visibility,
restorer_visibility=self.face_restorer_visibility,
codeformer_weight=self.codeformer_weight,
upscale_force=self.upscale_force
)

def process(
Expand Down Expand Up @@ -163,6 +165,7 @@ def process(
source_folder,
imgs,
random_image,
upscale_force,
):
self.enable = enable
if self.enable:
Expand Down Expand Up @@ -198,6 +201,7 @@ def process(
self.source_folder = source_folder
self.source_imgs = imgs
self.random_image = random_image
self.upscale_force = upscale_force
if self.gender_source is None or self.gender_source == "No":
self.gender_source = 0
if self.gender_target is None or self.gender_target == "No":
Expand All @@ -222,6 +226,8 @@ def process(
self.mask_face = False
if self.random_image is None:
self.random_image = False
if self.upscale_force is None:
self.upscale_force = False

logger.debug("*** Set Device")
set_Device(self.device)
Expand Down Expand Up @@ -444,7 +450,7 @@ def ui(self):
img, imgs, select_source, face_model, source_folder, save_original, mask_face, source_faces_index, gender_source, faces_index, gender_target, face_restorer_name, face_restorer_visibility, codeformer_weight, swap_in_source, swap_in_generated, random_image = ui_main.show(is_img2img=False, show_br=False, **msgs)

# TAB UPSCALE
restore_first, upscaler_name, upscaler_scale, upscaler_visibility = ui_upscale.show(show_br=False)
restore_first, upscaler_name, upscaler_scale, upscaler_visibility, upscale_force = ui_upscale.show(show_br=False)

# TAB TOOLS
ui_tools.show()
Expand Down Expand Up @@ -477,6 +483,7 @@ def ui(self):
'source_folder': source_folder,
'imgs': imgs,
'random_image': random_image,
'upscale_force': upscale_force,
}
return args

Expand Down Expand Up @@ -504,6 +511,7 @@ def enhancement_options(self) -> EnhancementOptions:
upscale_visibility=self.upscaler_visibility,
restorer_visibility=self.face_restorer_visibility,
codeformer_weight=self.codeformer_weight,
upscale_force=self.upscale_force,
)

def process(self, pp: scripts_postprocessing.PostprocessedImage, **args):
Expand Down Expand Up @@ -532,6 +540,7 @@ def process(self, pp: scripts_postprocessing.PostprocessedImage, **args):
self.source_folder = args['source_folder']
self.source_imgs = args['imgs']
self.random_image = args['random_image']
self.upscale_force = args['upscale_force']
if self.gender_source is None or self.gender_source == "No":
self.gender_source = 0
if self.gender_target is None or self.gender_target == "No":
Expand All @@ -550,6 +559,8 @@ def process(self, pp: scripts_postprocessing.PostprocessedImage, **args):
self.mask_face = False
if self.random_image is None:
self.random_image = False
if self.upscale_force is None:
self.upscale_force = False

current_job_number = shared.state.job_no + 1
job_count = shared.state.job_count
Expand Down
3 changes: 2 additions & 1 deletion scripts/reactor_swapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class EnhancementOptions:
face_restorer: FaceRestoration = None
restorer_visibility: float = 0.5
codeformer_weight: float = 0.5
upscale_force: bool = False


MESSAGED_STOPPED = False
Expand Down Expand Up @@ -713,7 +714,7 @@ def operate(

result_image = Image.fromarray(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))

if enhancement_options is not None and swapped > 0:
if (enhancement_options is not None and swapped > 0) or enhancement_options.upscale_force:
if mask_face and entire_mask_image is not None:
result_image = enhance_image_and_mask(result_image, enhancement_options,Image.fromarray(target_img_orig),Image.fromarray(entire_mask_image).convert("L"))
else:
Expand Down
2 changes: 1 addition & 1 deletion scripts/reactor_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
app_title = "ReActor"
version_flag = "v0.6.1-b2"
version_flag = "v0.6.1-b3"

from scripts.reactor_logger import logger, get_Run, set_Run
from scripts.reactor_globals import DEVICE
Expand Down

0 comments on commit c745fa2

Please sign in to comment.