RamAnanth1 commited on
Commit
8b2a350
1 Parent(s): f50cc97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -9,6 +9,8 @@ from pytorch_lightning import seed_everything
9
  from util import resize_image, HWC3, apply_canny
10
  from ldm.models.diffusion.ddim import DDIMSampler
11
 
 
 
12
  from cldm.model import create_model, load_state_dict
13
 
14
  from huggingface_hub import hf_hub_url, cached_download
@@ -54,7 +56,36 @@ def process_canny(input_image, prompt, a_prompt, n_prompt, num_samples, image_re
54
  results = [x_samples[i] for i in range(num_samples)]
55
  return [255 - detected_map] + results
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
 
 
 
58
  block = gr.Blocks().queue()
59
  control_task_list = [
60
  "Canny Edge Map",
@@ -65,7 +96,7 @@ with block:
65
  with gr.Row():
66
  with gr.Column():
67
  input_image = gr.Image(source='upload', type="numpy")
68
- input_control = gr.Dropdown(control_list, value="Canny Edge Map", label="Control Task"),
69
  prompt = gr.Textbox(label="Prompt")
70
  run_button = gr.Button(label="Run")
71
  with gr.Accordion("Advanced options", open=False):
 
9
  from util import resize_image, HWC3, apply_canny
10
  from ldm.models.diffusion.ddim import DDIMSampler
11
 
12
+ from annotator.openpose import apply_openpose
13
+
14
  from cldm.model import create_model, load_state_dict
15
 
16
  from huggingface_hub import hf_hub_url, cached_download
 
56
  results = [x_samples[i] for i in range(num_samples)]
57
  return [255 - detected_map] + results
58
 
59
+ def process_pose(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, detect_resolution, ddim_steps, scale, seed, eta):
60
+ with torch.no_grad():
61
+ input_image = HWC3(input_image)
62
+ detected_map, _ = apply_openpose(resize_image(input_image, detect_resolution))
63
+ detected_map = HWC3(detected_map)
64
+ img = resize_image(input_image, image_resolution)
65
+ H, W, C = img.shape
66
+
67
+ detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_NEAREST)
68
+
69
+ control = torch.from_numpy(detected_map.copy()).float() / 255.0
70
+ control = torch.stack([control for _ in range(num_samples)], dim=0)
71
+ control = einops.rearrange(control, 'b h w c -> b c h w').clone()
72
+
73
+ seed_everything(seed)
74
+
75
+ cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
76
+ un_cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
77
+ shape = (4, H // 8, W // 8)
78
+
79
+ samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
80
+ shape, cond, verbose=False, eta=eta,
81
+ unconditional_guidance_scale=scale,
82
+ unconditional_conditioning=un_cond)
83
+ x_samples = model.decode_first_stage(samples)
84
+ 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)
85
 
86
+ results = [x_samples[i] for i in range(num_samples)]
87
+ return [detected_map] + results
88
+
89
  block = gr.Blocks().queue()
90
  control_task_list = [
91
  "Canny Edge Map",
 
96
  with gr.Row():
97
  with gr.Column():
98
  input_image = gr.Image(source='upload', type="numpy")
99
+ input_control = gr.Dropdown(control_task_list, value="Canny Edge Map", label="Control Task"),
100
  prompt = gr.Textbox(label="Prompt")
101
  run_button = gr.Button(label="Run")
102
  with gr.Accordion("Advanced options", open=False):