lsb commited on
Commit
e674f0f
1 Parent(s): e34fa5d

inpainting

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -11,13 +11,19 @@ import numpy as np
11
  from PIL import Image
12
  from datetime import datetime
13
 
14
- preferred_dtype = torch.float32
15
  preferred_device = "cuda" if torch.cuda.is_available() else "cpu"
 
16
 
17
  def label_func(fn): return path/"labels"/f"{fn.stem}_P{fn.suffix}"
18
 
19
  segmodel = load_learner("camvid-512.pkl")
20
 
 
 
 
 
 
 
21
  working_size = (512, 512)
22
 
23
  default_inpainting_prompt = "watercolor of a leafy pedestrian mall at golden hour with multiracial genderqueer joggers and bicyclists and wheelchair users talking and laughing"
@@ -49,10 +55,20 @@ def app(img, prompt):
49
  img = np.array(Image.fromarray(img).resize(working_size))
50
  mask = ban_cars_mask[get_seg_mask(img)]
51
  mask = mask * 255
52
- overlay_img = Image.fromarray(np.stack([img[:, :, 0], mask, img[:,:,2]], axis=-1))
 
 
 
 
 
 
 
53
  end_time = datetime.now().timestamp()
54
  draw = ImageDraw.Draw(overlay_img)
55
- draw.text((50, 10), f"Old size: {old_size}\nDuration: {int(1000 * (end_time - start_time))}ms\n<{prompt}>", fill=(255, 255, 255))
 
 
 
56
  return overlay_img
57
 
58
  #ideally:
 
11
  from PIL import Image
12
  from datetime import datetime
13
 
 
14
  preferred_device = "cuda" if torch.cuda.is_available() else "cpu"
15
+ preferred_dtype = torch.float32 if preferred_device == 'cpu' else torch.float16
16
 
17
  def label_func(fn): return path/"labels"/f"{fn.stem}_P{fn.suffix}"
18
 
19
  segmodel = load_learner("camvid-512.pkl")
20
 
21
+ inpainting_pipeline = AutoPipelineForInpainting(
22
+ model="runwayml/stable-diffusion-inpainting",
23
+ revision="fp16",
24
+ torch_dtype=preferred_dtype,
25
+ ).to(preferred_device)
26
+
27
  working_size = (512, 512)
28
 
29
  default_inpainting_prompt = "watercolor of a leafy pedestrian mall at golden hour with multiracial genderqueer joggers and bicyclists and wheelchair users talking and laughing"
 
55
  img = np.array(Image.fromarray(img).resize(working_size))
56
  mask = ban_cars_mask[get_seg_mask(img)]
57
  mask = mask * 255
58
+ mask_time = datetime.now().timestamp()
59
+ overlay_img = inpainting_pipeline(
60
+ prompt=prompt,
61
+ image=Image.fromarray(img),
62
+ mask=Image.fromarray(mask),
63
+ strength=0.95,
64
+ num_inference_steps=13,
65
+ ).images[0]
66
  end_time = datetime.now().timestamp()
67
  draw = ImageDraw.Draw(overlay_img)
68
+ # replace spaces with newlines after many words to line break prompt
69
+ prompt = " ".join([prompt.split(" ")[i] if (i+1) % 5 else prompt.split(" ")[i] + "\n" for i in range(len(prompt.split(" ")))])
70
+
71
+ draw.text((50, 10), f"Old size: {old_size}\nTotal duration: {int(1000 * (end_time - start_time))}ms\nSegmentation {int(1000 * (mask_time - start_time))}ms / inpainting {int(1000 * (end_time - mask_time))} \n<{prompt}>", fill=(255, 255, 255))
72
  return overlay_img
73
 
74
  #ideally: