georgefen's picture
Enable live conditioning via webcam (#3)
63c6ec7
from share import *
import config
import cv2
import einops
import gradio as gr
import numpy as np
import torch
import random
from pytorch_lightning import seed_everything
from annotator.util import resize_image, HWC3
from cldm.model import create_model, load_state_dict
from cldm.ddim_hacked import DDIMSampler
import dlib
from PIL import Image, ImageDraw
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
model = create_model('./models/cldm_v15.yaml').cpu()
model.load_state_dict(load_state_dict(
'./models/control_sd15_landmarks.pth', location='cpu'))
model = model.to(device)
ddim_sampler = DDIMSampler(model)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
canvas_html = "<face-canvas id='canvas-root' data-mode='points' style='display:flex;max-width: 500px;margin: 0 auto;'></face-canvas>"
load_js = """
async () => {
const url = "https://huggingface.co/datasets/radames/gradio-components/raw/main/face-canvas.js"
fetch(url)
.then(res => res.text())
.then(text => {
const script = document.createElement('script');
script.type = "module"
script.src = URL.createObjectURL(new Blob([text], { type: 'application/javascript' }));
document.head.appendChild(script);
});
}
"""
get_js_image = """
async (input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, landmark_direct_mode, strength, scale, seed, eta, image_file_live_opt) => {
const canvasEl = document.getElementById("canvas-root");
const imageData = canvasEl? canvasEl._data : null;
if(image_file_live_opt === 'webcam'){
input_image = imageData['image']
landmark_direct_mode = true
}
return [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, landmark_direct_mode, strength, scale, seed, eta, image_file_live_opt]
}
"""
def draw_landmarks(image, landmarks, color="white", radius=2.5):
draw = ImageDraw.Draw(image)
for dot in landmarks:
x, y = dot
draw.ellipse((x-radius, y-radius, x+radius, y+radius), fill=color)
def get_68landmarks_img(img):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
faces = detector(gray)
landmarks = []
for face in faces:
shape = predictor(gray, face)
for i in range(68):
x = shape.part(i).x
y = shape.part(i).y
landmarks.append((x, y))
con_img = Image.new('RGB', (img.shape[1], img.shape[0]), color=(0, 0, 0))
draw_landmarks(con_img, landmarks)
con_img = np.array(con_img)
return con_img
def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, landmark_direct_mode, strength, scale, seed, eta, image_file_live_opt="file"):
input_image = input_image.convert('RGB')
input_image = np.array(input_image)
input_image = np.flip(input_image, axis=2)
print('input_image.shape', input_image.shape)
# Limit the number of samples to 2 for Spaces only
num_samples = min(num_samples, 2)
with torch.no_grad():
img = resize_image(HWC3(input_image), image_resolution)
H, W, C = img.shape
if landmark_direct_mode:
detected_map = img
else:
detected_map = get_68landmarks_img(img)
detected_map = HWC3(detected_map)
control = torch.from_numpy(
detected_map.copy()).float().to(device) / 255.0
control = torch.stack([control for _ in range(num_samples)], dim=0)
control = einops.rearrange(control, 'b h w c -> b c h w').clone()
if seed == -1:
seed = random.randint(0, 2**32 - 1)
seed_everything(seed)
if config.save_memory:
model.low_vram_shift(is_diffusing=False)
cond = {"c_concat": [control], "c_crossattn": [
model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [
model.get_learned_conditioning([n_prompt] * num_samples)]}
shape = (4, H // 8, W // 8)
if config.save_memory:
model.low_vram_shift(is_diffusing=True)
model.control_scales = [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else (
[strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01
samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
shape, cond, verbose=False, eta=eta,
unconditional_guidance_scale=scale,
unconditional_conditioning=un_cond)
if config.save_memory:
model.low_vram_shift(is_diffusing=False)
x_samples = model.decode_first_stage(samples)
x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c')
* 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)
results = [x_samples[i] for i in range(num_samples)]
return [255 - detected_map] + results
def toggle(choice):
if choice == "file":
return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
elif choice == "webcam":
return gr.update(visible=False, value=None), gr.update(visible=True, value=canvas_html)
block = gr.Blocks().queue()
with block:
live_conditioning = gr.JSON(value={}, visible=False)
with gr.Row():
gr.Markdown("## Control Stable Diffusion with Face Landmarks")
with gr.Row():
with gr.Column():
image_file_live_opt = gr.Radio(["file", "webcam"], value="file",
label="How would you like to upload your image?")
input_image = gr.Image(source="upload", visible=True, type="pil")
canvas = gr.HTML(None, elem_id="canvas_html", visible=False)
image_file_live_opt.change(fn=toggle,
inputs=[image_file_live_opt],
outputs=[input_image, canvas],
queue=False)
prompt = gr.Textbox(label="Prompt")
run_button = gr.Button(label="Run")
with gr.Accordion("Advanced options", open=False):
num_samples = gr.Slider(
label="Images", minimum=1, maximum=2, value=1, step=1)
image_resolution = gr.Slider(
label="Image Resolution", minimum=256, maximum=768, value=512, step=64)
strength = gr.Slider(
label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
guess_mode = gr.Checkbox(label='Guess Mode', value=False)
landmark_direct_mode = gr.Checkbox(
label='Input Landmark Directly', value=False)
ddim_steps = gr.Slider(
label="Steps", minimum=1, maximum=100, value=20, step=1)
scale = gr.Slider(label="Guidance Scale",
minimum=0.1, maximum=30.0, value=9.0, step=0.1)
seed = gr.Slider(label="Seed", minimum=-1,
maximum=2147483647, step=1, randomize=True)
eta = gr.Number(label="eta (DDIM)", value=0.0)
a_prompt = gr.Textbox(
label="Added Prompt", value='best quality, extremely detailed')
n_prompt = gr.Textbox(label="Negative Prompt",
value='cartoon, disfigured, bad art, deformed, poorly drawn, extra limbs, weird colors, blurry, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality')
with gr.Column():
result_gallery = gr.Gallery(
label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution,
ddim_steps, guess_mode, landmark_direct_mode, strength, scale, seed, eta]
gr.Examples(fn=process, examples=[
["examples/image0.jpg", "a silly clown face", "best quality, extremely detailed",
"cartoon, disfigured, bad art, deformed, poorly drawn, extra limbs, weird colors, blurry, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", 1, 512, 20, False, False, 1.0, 9.0, -1, 0.0],
["examples/image1.png", "a photo of a woman wearing glasses", "best quality, extremely detailed",
"cartoon, disfigured, bad art, deformed, poorly drawn, extra limbs, weird colors, blurry, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", 1, 512, 20, False, False, 1.0, 9.0, -1, 0.0],
["examples/image2.png", "a silly portrait of man with head tilted and a beautiful hair on the side", "best quality, extremely detailed",
"cartoon, disfigured, bad art, deformed, poorly drawn, extra limbs, weird colors, blurry, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", 1, 512, 20, False, False, 1.0, 9.0, -1, 0.0],
["examples/image3.png", "portrait handsome men", "best quality, extremely detailed",
"cartoon, disfigured, bad art, deformed, poorly drawn, extra limbs, weird colors, blurry, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", 1, 512, 20, False, False, 1.0, 9.0, -1, 0.0],
["examples/image4.jpg", "a beautiful woman looking at the sky", "best quality, extremely detailed",
"cartoon, disfigured, bad art, deformed, poorly drawn, extra limbs, weird colors, blurry, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", 1, 512, 20, False, False, 1.0, 9.0, -1, 0.0],
], inputs=ips, outputs=[result_gallery], cache_examples=True)
run_button.click(fn=process, inputs=ips + [image_file_live_opt],
outputs=[result_gallery], _js=get_js_image)
block.load(None, None, None, _js=load_js)
block.launch()