Spaces:
Sleeping
Sleeping
File size: 7,044 Bytes
4de728a cd61183 4de728a 2c2a920 ae84eb8 cd61183 ae84eb8 cd61183 ae84eb8 4de728a cd61183 ae84eb8 cd61183 ae84eb8 4de728a ae84eb8 4de728a ae84eb8 5debf41 4de728a 5debf41 4de728a 5debf41 4de728a 5debf41 4de728a 5debf41 |
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 |
import huggingface_hub
import gradio as gr
from stable_diffusion_reference_only.pipelines.stable_diffusion_reference_only_pipeline import (
StableDiffusionReferenceOnlyPipeline,
)
from anime_segmentation import get_model as get_anime_segmentation_model
from anime_segmentation import character_segment as anime_character_segment
from diffusers.schedulers import UniPCMultistepScheduler
from PIL import Image
import cv2
import numpy as np
import os
import torch
if __name__ == "__main__":
print(f"Is CUDA available: {torch.cuda.is_available()}")
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
automatic_coloring_pipeline = StableDiffusionReferenceOnlyPipeline.from_pretrained(
"AisingioroHao0/stable-diffusion-reference-only-automatic-coloring-0.1.2"
).to(device)
automatic_coloring_pipeline.scheduler = UniPCMultistepScheduler.from_config(
automatic_coloring_pipeline.scheduler.config
)
segment_model = get_anime_segmentation_model(
model_path=huggingface_hub.hf_hub_download("skytnt/anime-seg", "isnetis.ckpt")
).to(device)
def character_segment(img):
if img is None:
return None
img = anime_character_segment(segment_model, img)
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
return img
def color_inversion(img):
if img is None:
return None
return 255 - img
def get_line_art(img):
if img is None:
return None
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = cv2.adaptiveThreshold(
img,
255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
blockSize=5,
C=7,
)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
return img
def inference(prompt, blueprint, num_inference_steps):
if prompt is None or blueprint is None:
return None
return np.array(
automatic_coloring_pipeline(
prompt=Image.fromarray(prompt),
blueprint=Image.fromarray(blueprint),
num_inference_steps=num_inference_steps,
).images[0]
)
def automatic_coloring(prompt, blueprint, num_inference_steps):
if prompt is None or blueprint is None:
return None
blueprint = color_inversion(blueprint)
return inference(prompt, blueprint, num_inference_steps)
def style_transfer(prompt, blueprint, num_inference_steps):
if prompt is None or blueprint is None:
return None
prompt = character_segment(prompt)
blueprint = character_segment(blueprint)
blueprint = get_line_art(blueprint)
blueprint = color_inversion(blueprint)
return inference(prompt, blueprint, num_inference_steps)
with gr.Blocks() as demo:
gr.Markdown(
"""
# Stable Diffusion Reference Only Automatic Coloring 0.1.2\n\n
demo for [https://github.com/aihao2000/stable-diffusion-reference-only](https://github.com/aihao2000/stable-diffusion-reference-only)
"""
)
with gr.Row():
with gr.Column():
prompt_input_compoent = gr.Image(shape=(512, 512), label="prompt")
prompt_character_segment_button = gr.Button(
"character segment",
)
prompt_character_segment_button.click(
character_segment,
inputs=prompt_input_compoent,
outputs=prompt_input_compoent,
)
with gr.Column():
blueprint_input_compoent = gr.Image(shape=(512, 512), label="blueprint")
blueprint_character_segment_button = gr.Button("character segment")
blueprint_character_segment_button.click(
character_segment,
inputs=blueprint_input_compoent,
outputs=blueprint_input_compoent,
)
get_line_art_button = gr.Button(
"get line art",
)
get_line_art_button.click(
get_line_art,
inputs=blueprint_input_compoent,
outputs=blueprint_input_compoent,
)
color_inversion_button = gr.Button(
"color inversion",
)
color_inversion_button.click(
color_inversion,
inputs=blueprint_input_compoent,
outputs=blueprint_input_compoent,
)
with gr.Column():
result_output_component = gr.Image(shape=(512, 512), label="result")
num_inference_steps_input_component = gr.Number(
20, label="num inference steps", minimum=1, maximum=1000, step=1
)
inference_button = gr.Button("inference")
inference_button.click(
inference,
inputs=[
prompt_input_compoent,
blueprint_input_compoent,
num_inference_steps_input_component,
],
outputs=result_output_component,
)
automatic_coloring_button = gr.Button("automatic coloring")
automatic_coloring_button.click(
automatic_coloring,
inputs=[
prompt_input_compoent,
blueprint_input_compoent,
num_inference_steps_input_component,
],
outputs=result_output_component,
)
style_transfer_button = gr.Button("style transfer")
style_transfer_button.click(
style_transfer,
inputs=[
prompt_input_compoent,
blueprint_input_compoent,
num_inference_steps_input_component,
],
outputs=result_output_component,
)
with gr.Row():
gr.Examples(
examples=[
[
os.path.join(
os.path.dirname(__file__), "README.assets", "3x9_prompt.png"
),
os.path.join(
os.path.dirname(__file__), "README.assets", "3x9_blueprint.png"
),
],
],
inputs=[prompt_input_compoent, blueprint_input_compoent],
outputs=result_output_component,
fn=lambda x, y: None,
cache_examples=True,
)
demo.queue(max_size=10).launch()
|