File size: 15,140 Bytes
c490c3c 714a075 5bbdced 714a075 5bbdced 714a075 c490c3c 5bbdced c490c3c 714a075 c490c3c 714a075 c490c3c 5f9e862 c490c3c 714a075 c490c3c 714a075 c490c3c 714a075 c490c3c 714a075 c490c3c 5bbdced c490c3c 5bbdced c490c3c 714a075 5bbdced c490c3c 5f9e862 c490c3c 5f9e862 c490c3c 5f9e862 c490c3c 714a075 c490c3c 5bbdced c490c3c 5bbdced c490c3c 5bbdced c490c3c 5f9e862 c490c3c 714a075 c490c3c 5f9e862 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
import os
from io import BytesIO
import gradio as gr
import grpc
from PIL import Image
import pandas as pd
from inference_pb2 import SFERequest, SFEResponse, SFERequestMask, SFEResponseMask
from inference_pb2_grpc import SFEServiceStub
PREDEFINED_EDITINGS_DATA = {
"glasses": ([-20.0, 30.0], False),
"smile": ([-10.0, 10.0], False),
"makeup": ([-10.0, 15.0], False),
"eye_openness": ([-45.0, 30.0], True),
"trimmed_beard": ([-30.0, 30.0], True),
"face_roundness": ([-20.0, 15.0], False),
"nose_length": ([-30.0, 30.0], True),
"eyebrow_thickness": ([-20.0, 20.0], True),
"displeased": ([-10.0, 10.0], False),
"age": ([-10.0, 10.0], False),
"rotation": ([-7.0, 7.0], False),
"afro": ([0, 0.14], False),
"angry": ([0, 0.14], False),
"bobcut": ([0, 0.18], False),
"bowlcut": ([0, 0.14], False),
"mohawk": ([0, 0.1], False),
"curly_hair": ([0, 0.12], False),
"purple_hair": ([0, 0.12], False),
"surprised": ([0, 0.1], False),
"beyonce": ([0, 0.12], False),
"hilary_clinton": ([0, 0.1], False),
"depp": ([0, 0.12], False),
"taylor_swift": ([0, 0.1], False),
"trump": ([0, 0.1], False),
"zuckerberg": ([0, 0.1], False),
"black hair": ([-7.0, 10.0], False),
"blond hair": ([-7.0, 10.0], True),
"grey hair": ([-7.0, 7.0], True),
"wavy hair": ([-7.0, 7.0], False),
"receding hairline": ([-10.0, 10.0], True),
"sideburns": ([-7.0, 7.0], True),
"goatee": ([-7.0, 7.0], True),
"gender swap": ([-10.0, 7.0], False)
}
DIRECTIONS_NAME_SWAP = {
"smile" : "fs_smiling",
"glasses": "fs_glasses",
"makeup": "fs_makeup",
"gender swap": "gender"
}
def denormalize_power(direction_name, directon_power):
if direction_name not in PREDEFINED_EDITINGS_DATA:
return directon_power
original_range, is_reversed = PREDEFINED_EDITINGS_DATA[direction_name]
if directon_power > 0:
normalized = directon_power / 15 * abs(original_range[1])
else:
normalized = directon_power / 15 * abs(original_range[0])
if is_reversed:
normalized = -normalized
return normalized
def get_bytes(img):
if img is None:
return img
buffered = BytesIO()
img.save(buffered, format="JPEG")
return buffered.getvalue()
def bytes_to_image(image: bytes) -> Image.Image:
image = Image.open(BytesIO(image))
return image
def edit_image(orig_image, edit_direction, edit_power, align, mask, progress=gr.Progress(track_tqdm=True)):
if edit_direction in DIRECTIONS_NAME_SWAP:
edit_direction = DIRECTIONS_NAME_SWAP[edit_direction]
if not orig_image:
return gr.update(visible=False), gr.update(visible=False), gr.update(value="Need to upload an input image ❗", visible=True)
orig_image_bytes = get_bytes(orig_image)
mask_bytes = get_bytes(mask)
if mask_bytes is None:
mask_bytes = b"mask"
edit_power = denormalize_power(edit_direction, edit_power)
with grpc.insecure_channel(os.environ["SERVER"]) as channel:
stub = SFEServiceStub(channel)
output: SFEResponse = stub.edit(
SFERequest(orig_image=orig_image_bytes, direction=edit_direction, power=edit_power, align=align, mask=mask_bytes, use_cache=True)
)
if output.image == b"aligner error":
return gr.update(visible=False), gr.update(visible=False), gr.update(value="Face aligner can not find face in your image 😢 Try to upload another one", visible=True)
output_edited = bytes_to_image(output.image)
output_inv = bytes_to_image(output.inv_image)
return gr.update(value=output_edited, visible=True), gr.update(value=output_inv, visible=True), gr.update(visible=False)
def edit_image_clip(orig_image, neutral_prompt, target_prompt, disentanglement, edit_power, align, mask, edit_method, progress=gr.Progress(track_tqdm=True)):
if edit_method == "StyleClip":
edit_direction = "_".join(["styleclip_global", neutral_prompt, target_prompt, str(disentanglement)])
else:
edit_power = edit_power / 10
disentanglement = disentanglement / 3
edit_direction = "_".join(["deltaedit", neutral_prompt, target_prompt, str(disentanglement)])
return edit_image(orig_image, edit_direction, edit_power, align, mask, progress=None)
def get_mask(input_image, align, mask_trashhold, progress=gr.Progress(track_tqdm=True)):
if not input_image:
return gr.update(visible=False), gr.update(value="Need to upload an input image ❗", visible=True)
input_image_bytes = get_bytes(input_image)
with grpc.insecure_channel(os.environ["SERVER"]) as channel:
stub = SFEServiceStub(channel)
output: SFEResponseMask = stub.generate_mask(
SFERequestMask(orig_image=input_image_bytes, trashold=mask_trashhold, align=align, use_cache=True)
)
if output.mask == b"aligner error":
return gr.update(visible=False), gr.update(value="Face aligner can not find face in your image 😢 Try to upload another one", visible=True)
if output.mask == b"masker face parser error":
return gr.update(visible=False), gr.update(value="Masker's face detector can't find face in your image 😢 Try to upload another one", visible=True)
output_mask = bytes_to_image(output.mask)
return gr.update(value=output_mask, visible=True), gr.update(visible=False)
def get_demo():
editings_table = pd.read_csv("editings_table.csv")
editings_table = editings_table.style.set_properties(**{"text-align": "center"})
editings_table = editings_table.set_table_styles([dict(selector="th", props=[("text-align", "center")])])
with gr.Blocks() as demo:
gr.Markdown("## StyleFeatureEditor")
gr.Markdown(
'<div style="display: flex; align-items: center; gap: 10px;">'
'<span>Official Gradio demo for StyleFeatureEditor:</span>'
'<a href="https://arxiv.org/abs/2406.10601"><img src="https://img.shields.io/badge/arXiv-2404.01094-b31b1b.svg" height=22.5></a>'
'<a href="https://github.com/AIRI-Institute/StyleFeatureEditor"><img src="https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white" height=22.5></a>'
'<a href="https://huggingface.co/AIRI-Institute/StyleFeatureEditor"><img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/model-on-hf-md.svg" height=22.5></a>'
'<a href="https://colab.research.google.com/#fileId=https://github.com/AIRI-Institute/StyleFeatureEditor/blob/main/notebook/StyleFeatureEditor_inference.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" height=22.5></a>'
'</div>'
)
with gr.Row():
with gr.Column():
with gr.Accordion("Input Image", open=True):
input_image = gr.Image(label="Input image you want to edit", type="pil", height=300)
align = gr.Checkbox(label="Align (crop and resize) the input image. For SFE to work well, it is necessary to align the input if it is not.", value=True)
with gr.Accordion("Predefined Editings", open=True):
with gr.Accordion("Description", open=False):
gr.Markdown('''A branch of predefined editings gained from InterfaceGAN, Stylespace, GANSpace and StyleClip mappers. Look at the table below to see which direction is responsible for which editings.
**Editing power** -- the greater the absolute value of this parameter, the more the selected edit will appear. Better use values in the range 7 - 13, lower values may not give the desired edit, higher values -- on the contrary -- may apply edit too much and create artefacts.
**Positive effect** -- the effect applied to the image when positive editing power is used.
**Negative effect** -- the effect applied to the image when negative editing power is used. It is usually the opposite of the positive effect.
'''
)
gr.Dataframe(value=editings_table, datatype=["markdown","markdown","markdown","markdown"], interactive=False, wrap=True,
column_widths=["25px", "25px", "25px", "25px"], height=300) # 100
with gr.Row():
predef_editing_direction = gr.Dropdown(list(PREDEFINED_EDITINGS_DATA.keys()), label="Editing direction", value="smile")
predef_editing_power = gr.Slider(-20, 20, value=7, step=0.1, label="Editing power")
btn_predef = gr.Button("Edit image")
with gr.Accordion("Text Prompt Editings", open=False):
with gr.Accordion("Description", open=False):
gr.Markdown('''You can alse use editings from text prompts via **StyleClip Global Mapper** (https://arxiv.org/abs/2103.17249) or **DeltaEdit** (https://arxiv.org/abs/2303.06285). You just need to choose:
**Method** -- method to use, StyleClip or DeltaEdit
**Editing power** -- the greater the absolute value of this parameter, the more the selected edit will appear.
**Neutral prompt** -- some neutral description of the original image (e.g. "a face").
**Target prompt** -- text that contains the desired edit (e.g. "a smilling face").
**Disentanglement** -- positive number, the less this attribute -- the more related attributes will also be changed (e.g. for grey hair editing, wrinkle, skin colour and glasses may also be edited)
''')
edit_method = gr.Dropdown(["StyleClip", "DeltaEdit"], label="Editing method", value="StyleClip")
neutral_prompt = gr.Textbox(value="face with hair", label="Neutreal prompt (e.g. 'a face')")
target_prompt = gr.Textbox(value="face with fire hair", label="Target prompt (e.g. 'a smilling face')")
styleclip_editing_power = gr.Slider(-50, 50, value=10, step=1, label="Editing power")
disentanglement = gr.Slider(0, 1, value=0.1, step=0.01, label="Disentanglement")
btn_clip = gr.Button("Edit image")
with gr.Accordion("Mask settings (optional)", open=False):
gr.Markdown('''If some artefacts appear during editing (or some details disappear), you can specify an image mask to select which regions of the image should not be edited. The mask must have a size of 1024 x 1024 and represent an inversion of the original image.
'''
)
mask = gr.Image(label="Upload mask for editing", type="pil", height=350)
with gr.Accordion("Mask generating", open=False):
gr.Markdown("Here you can generate mask that separates face (with hair) from the background.")
with gr.Row():
input_mask = gr.Image(label="Input image for mask generating", type="pil", height=240)
output_mask = gr.Image(label="Generated mask", height=240)
error_message_mask = gr.Textbox(label="⚠️ Error ⚠️", visible=False, elem_classes="error-message")
align_mask = gr.Checkbox(label="To align (crop and resize image) or not. Only uncheck this box if the original image has already been aligned.", value=True)
mask_trashhold = gr.Slider(0, 1, value=0.9, step=0.001, label="Mask trashold",
info="The more this parameter, the more is face part, and the less is background part.")
btn_mask = gr.Button("Generate mask")
with gr.Column():
with gr.Row():
output_inv = gr.Image(label="Inversion result", visible=True)
output_edit = gr.Image(label="Editing result", visible=True)
error_message = gr.Textbox(label="⚠️ Error ⚠️", visible=False, elem_classes="error-message")
gr.Markdown("If artefacts appear during editing -- try lowering the editing power or using a mask.")
gr.Examples(
label="Input Examples for editing",
examples=[
["images/scarlet.jpg"],
["images/gosling.jpg"],
["images/robert.png"],
["images/smith.jpg"],
["images/watson.jpeg"],
],
inputs=[input_image],
examples_per_page=5
)
gr.Examples(
label="Mask Examples for editing",
examples=[
["images/scarlet_mask.webp"],
["images/gosling_mask.webp"],
["images/robert_mask.webp"],
["images/smith_mask.webp"],
["images/watson_mask.webp"],
],
inputs=[mask]
)
gr.Examples(
label="Input Examples for Mask generation",
examples=[
["images/scarlet.jpg"],
["images/gosling.jpg"],
["images/robert.png"],
["images/smith.jpg"],
["images/watson.jpeg"],
],
inputs=[input_mask]
)
btn_predef.click(
fn=edit_image,
inputs=[input_image, predef_editing_direction, predef_editing_power, align, mask],
outputs=[output_edit, output_inv, error_message]
)
btn_clip.click(
fn=edit_image_clip,
inputs=[input_image, neutral_prompt, target_prompt, disentanglement, styleclip_editing_power, align, mask, edit_method],
outputs=[output_edit, output_inv, error_message]
)
btn_mask.click(
fn=get_mask,
inputs=[input_mask, align_mask, mask_trashhold],
outputs=[output_mask, error_message_mask]
)
gr.Markdown('''To cite the paper by the authors
```
@InProceedings{Bobkov_2024_CVPR,
author = {Bobkov, Denis and Titov, Vadim and Alanov, Aibek and Vetrov, Dmitry},
title = {The Devil is in the Details: StyleFeatureEditor for Detail-Rich StyleGAN Inversion and High Quality Image Editing},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {June},
year = {2024},
pages = {9337-9346}
}
```
''')
return demo
if __name__ == "__main__":
demo = get_demo()
demo.launch(server_name="127.0.0.1", server_port=7860)
|