Spaces:
Running
Running
File size: 6,271 Bytes
009c38e dbbdaef 009c38e 33cef83 009c38e 811cb03 d2eda3f 811cb03 009c38e 873974f 811cb03 873974f 811cb03 873974f 811cb03 873974f 6abf639 009c38e 811cb03 009c38e 4e32bb9 009c38e 873974f 4e32bb9 009c38e f1383e1 4e32bb9 f1383e1 dbbdaef 4e32bb9 f1383e1 4e32bb9 dbbdaef ce50ac7 009c38e 811cb03 ce50ac7 009c38e 4e32bb9 811cb03 009c38e ce50ac7 160d1e3 009c38e ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 4e32bb9 ce50ac7 811cb03 ce50ac7 4e32bb9 811cb03 009c38e 811cb03 ce50ac7 4e32bb9 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 4e32bb9 811cb03 009c38e ce50ac7 009c38e 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 ce50ac7 811cb03 4e32bb9 |
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 |
#!/usr/bin/env python
from __future__ import annotations
import pathlib
import gradio as gr
from dualstylegan import Model
DESCRIPTION = """# Portrait Style Transfer with <a href="https://github.com/williamyang1991/DualStyleGAN">DualStyleGAN</a>
<img id="overview" alt="overview" src="https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/overview.jpg" />
"""
def get_style_image_url(style_name: str) -> str:
base_url = "https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images"
filenames = {
"cartoon": "cartoon_overview.jpg",
"caricature": "caricature_overview.jpg",
"anime": "anime_overview.jpg",
"arcane": "Reconstruction_arcane_overview.jpg",
"comic": "Reconstruction_comic_overview.jpg",
"pixar": "Reconstruction_pixar_overview.jpg",
"slamdunk": "Reconstruction_slamdunk_overview.jpg",
}
return f"{base_url}/{filenames[style_name]}"
def get_style_image_markdown_text(style_name: str) -> str:
url = get_style_image_url(style_name)
return f'<center><img id="style-image" src="{url}" alt="style image"></center>'
def update_slider(choice: str) -> dict:
max_vals = {
"cartoon": 316,
"caricature": 198,
"anime": 173,
"arcane": 99,
"comic": 100,
"pixar": 121,
"slamdunk": 119,
}
return gr.Slider(maximum=max_vals[choice])
def update_style_image(style_name: str) -> dict:
text = get_style_image_markdown_text(style_name)
return gr.Markdown(value=text)
def set_example_image(example: list) -> dict:
return gr.Image(value=example[0])
def set_example_styles(example: list) -> list[dict]:
return [
gr.Radio(value=example[0]),
gr.Slider(value=example[1]),
]
def set_example_weights(example: list) -> list[dict]:
return [
gr.Slider(value=example[0]),
gr.Slider(value=example[1]),
]
model = Model()
with gr.Blocks(css="style.css") as demo:
gr.Markdown(DESCRIPTION)
with gr.Group():
gr.Markdown(
"""## Step 1 (Preprocess Input Image)
- Drop an image containing a near-frontal face to the **Input Image**.
- If there are multiple faces in the image, hit the Edit button in the upper right corner and crop the input image beforehand.
- Hit the **Detect & Align Face** button.
- Hit the **Reconstruct Face** button.
- The final result will be based on this **Reconstructed Face**. So, if the reconstructed image is not satisfactory, you may want to change the input image.
"""
)
with gr.Row():
with gr.Column():
with gr.Row():
input_image = gr.Image(label="Input Image", type="filepath")
with gr.Row():
detect_button = gr.Button("Detect & Align Face")
with gr.Column():
with gr.Row():
aligned_face = gr.Image(label="Aligned Face", type="numpy", interactive=False)
with gr.Row():
reconstruct_button = gr.Button("Reconstruct Face")
with gr.Column():
reconstructed_face = gr.Image(label="Reconstructed Face", type="numpy")
instyle = gr.State()
with gr.Row():
paths = sorted(pathlib.Path("images").glob("*.jpg"))
gr.Examples(examples=[[path.as_posix()] for path in paths], inputs=input_image)
with gr.Group():
gr.Markdown(
"""## Step 2 (Select Style Image)
- Select **Style Type**.
- Select **Style Image Index** from the image table below.
"""
)
with gr.Row():
with gr.Column():
style_type = gr.Radio(label="Style Type", choices=model.style_types, value="cartoon")
text = get_style_image_markdown_text("cartoon")
style_image = gr.Markdown(value=text)
style_index = gr.Slider(label="Style Image Index", minimum=0, maximum=316, step=1, value=26)
with gr.Row():
gr.Examples(
examples=[
["cartoon", 26],
["caricature", 65],
["arcane", 63],
["pixar", 80],
],
inputs=[style_type, style_index],
)
with gr.Group():
gr.Markdown(
"""## Step 3 (Generate Style Transferred Image)
- Adjust **Structure Weight** and **Color Weight**.
- These are weights for the style image, so the larger the value, the closer the resulting image will be to the style image.
- Hit the **Generate** button.
"""
)
with gr.Row():
with gr.Column():
with gr.Row():
structure_weight = gr.Slider(label="Structure Weight", minimum=0, maximum=1, step=0.1, value=0.6)
with gr.Row():
color_weight = gr.Slider(label="Color Weight", minimum=0, maximum=1, step=0.1, value=1)
with gr.Row():
structure_only = gr.Checkbox(label="Structure Only")
with gr.Row():
generate_button = gr.Button("Generate")
with gr.Column():
result = gr.Image(label="Result")
with gr.Row():
gr.Examples(
examples=[
[0.6, 1.0],
[0.3, 1.0],
[0.0, 1.0],
[1.0, 0.0],
],
inputs=[structure_weight, color_weight],
)
detect_button.click(fn=model.detect_and_align_face, inputs=input_image, outputs=aligned_face)
reconstruct_button.click(fn=model.reconstruct_face, inputs=aligned_face, outputs=[reconstructed_face, instyle])
style_type.change(fn=update_slider, inputs=style_type, outputs=style_index)
style_type.change(fn=update_style_image, inputs=style_type, outputs=style_image)
generate_button.click(
fn=model.generate,
inputs=[
style_type,
style_index,
structure_weight,
color_weight,
structure_only,
instyle,
],
outputs=result,
)
if __name__ == "__main__":
demo.queue(max_size=10).launch()
|