Spaces:
Runtime error
Runtime error
mikonvergence
commited on
Commit
·
b932645
1
Parent(s):
159c049
Changed layout in app.py
Browse files
app.py
CHANGED
@@ -56,25 +56,7 @@ if torch.cuda.is_available():
|
|
56 |
# Functions
|
57 |
|
58 |
def get_guide(image):
|
59 |
-
|
60 |
-
|
61 |
-
def set_mask(image):
|
62 |
-
img=image['image'][...,:3]
|
63 |
-
mask=1*(image['mask'][...,:3]>0)
|
64 |
-
# save vars
|
65 |
-
CURRENT_IMAGE['image']=img
|
66 |
-
CURRENT_IMAGE['mask']=mask
|
67 |
-
|
68 |
-
guide=get_guide(img)
|
69 |
-
CURRENT_IMAGE['guide']=np.array(guide)
|
70 |
-
guide=255-np.asarray(guide)
|
71 |
-
|
72 |
-
seg_img = guide*(1-mask) + mask*192
|
73 |
-
preview = img * (seg_img==255)
|
74 |
-
|
75 |
-
vis_image=(preview/2).astype(seg_img.dtype) + seg_img * (seg_img!=255)
|
76 |
-
|
77 |
-
return vis_image
|
78 |
|
79 |
def generate(image,
|
80 |
prompt,
|
@@ -83,14 +65,14 @@ def generate(image,
|
|
83 |
sketch_scale,
|
84 |
seed):
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
|
91 |
-
|
92 |
|
93 |
-
|
94 |
prompt,
|
95 |
num_inference_steps=num_steps,
|
96 |
guidance_scale=text_scale,
|
@@ -99,72 +81,68 @@ def generate(image,
|
|
99 |
control_image=Image.fromarray(CURRENT_IMAGE['guide']),
|
100 |
controlnet_conditioning_scale=sketch_scale,
|
101 |
mask_image=Image.fromarray(mask_img)
|
102 |
-
|
103 |
|
104 |
-
|
105 |
|
106 |
def create_demo(max_images=12, default_num_images=3):
|
107 |
-
|
108 |
-
with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace","monospace"])
|
109 |
-
) as demo:
|
110 |
gr.Markdown('## Cut and Sketch ✂️▶️✏️')
|
111 |
-
|
112 |
-
gr.
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
with gr.Row():
|
137 |
-
mask_button = gr.Button(label='Set Mask', value='Set Mask')
|
138 |
-
run_button = gr.Button(label='Generate', value='Generate')
|
139 |
output_image = gr.Gallery(
|
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 |
inputs = [
|
169 |
sketch_image,
|
170 |
prompt,
|
@@ -173,10 +151,28 @@ def create_demo(max_images=12, default_num_images=3):
|
|
173 |
sketch_scale,
|
174 |
seed
|
175 |
]
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
run_button.click(fn=generate, inputs=inputs, outputs=output_image)
|
179 |
-
|
180 |
|
181 |
if __name__ == '__main__':
|
182 |
demo = create_demo()
|
|
|
56 |
# Functions
|
57 |
|
58 |
def get_guide(image):
|
59 |
+
return hed(image,scribble=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
def generate(image,
|
62 |
prompt,
|
|
|
65 |
sketch_scale,
|
66 |
seed):
|
67 |
|
68 |
+
sketch=(255*(image['mask'][...,:3]>0)).astype(CURRENT_IMAGE['image'].dtype)
|
69 |
+
mask=CURRENT_IMAGE['mask']
|
70 |
+
|
71 |
+
CURRENT_IMAGE['guide']=(CURRENT_IMAGE['guide']*(mask==0) + sketch*(mask!=0)).astype(CURRENT_IMAGE['image'].dtype)
|
72 |
|
73 |
+
mask_img=255*CURRENT_IMAGE['mask'].astype(CURRENT_IMAGE['image'].dtype)
|
74 |
|
75 |
+
new_image = pipe(
|
76 |
prompt,
|
77 |
num_inference_steps=num_steps,
|
78 |
guidance_scale=text_scale,
|
|
|
81 |
control_image=Image.fromarray(CURRENT_IMAGE['guide']),
|
82 |
controlnet_conditioning_scale=sketch_scale,
|
83 |
mask_image=Image.fromarray(mask_img)
|
84 |
+
).images#[0]
|
85 |
|
86 |
+
return new_image
|
87 |
|
88 |
def create_demo(max_images=12, default_num_images=3):
|
89 |
+
with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace","monospace"])) as demo:
|
|
|
|
|
90 |
gr.Markdown('## Cut and Sketch ✂️▶️✏️')
|
91 |
+
|
92 |
+
with gr.Column() as step_1:
|
93 |
+
gr.Markdown('**Start Here**')
|
94 |
+
gr.Markdown('1. Upload your image below')
|
95 |
+
gr.Markdown('2. **Draw the mask** for the region you want changed (Cut ✂️)')
|
96 |
+
input_image = gr.Image(source='upload',
|
97 |
+
shape=[HEIGHT,WIDTH],
|
98 |
+
type='numpy',
|
99 |
+
label='Mask Draw (Cut!)',
|
100 |
+
tool='sketch',
|
101 |
+
brush_radius=80)
|
102 |
+
gr.Markdown('3. Click `Set Mask` when it is ready!')
|
103 |
+
mask_button = gr.Button(label='Set Mask', value='Set Mask')
|
104 |
+
with gr.Column(visible=False) as step_2:
|
105 |
+
gr.Markdown('4. Now, you can **sketch a replacement** object! (Sketch ✏️)')
|
106 |
+
sketch_image = gr.Image(source='upload',
|
107 |
+
shape=[HEIGHT,WIDTH],
|
108 |
+
type='numpy',
|
109 |
+
label='Fill Draw (Sketch!)',
|
110 |
+
tool='sketch',
|
111 |
+
brush_radius=20)
|
112 |
+
gr.Markdown('5. (You can also provide a **text prompt** if you want)')
|
113 |
+
prompt = gr.Textbox(label='Prompt')
|
114 |
+
gr.Markdown('6. 🔮 Click `Generate` when ready! ')
|
115 |
+
run_button = gr.Button(label='Generate', value='Generate')
|
|
|
|
|
|
|
116 |
output_image = gr.Gallery(
|
117 |
+
label="Generated images",
|
118 |
+
visible=False,
|
119 |
+
show_label=False,
|
120 |
+
elem_id="gallery",
|
121 |
+
)#.style(grid=(1, 2))
|
122 |
+
|
123 |
+
with gr.Accordion('Advanced options', open=False):
|
124 |
+
num_steps = gr.Slider(label='Steps',
|
125 |
+
minimum=1,
|
126 |
+
maximum=100,
|
127 |
+
value=20,
|
128 |
+
step=1)
|
129 |
+
text_scale = gr.Slider(label='Text Guidance Scale',
|
130 |
+
minimum=0.1,
|
131 |
+
maximum=30.0,
|
132 |
+
value=7.5,
|
133 |
+
step=0.1)
|
134 |
+
seed = gr.Slider(label='Seed',
|
135 |
+
minimum=-1,
|
136 |
+
maximum=2147483647,
|
137 |
+
step=1,
|
138 |
+
randomize=True)
|
139 |
+
|
140 |
+
sketch_scale = gr.Slider(label='Sketch Guidance Scale',
|
141 |
+
minimum=0.0,
|
142 |
+
maximum=1.0,
|
143 |
+
value=1.0,
|
144 |
+
step=0.05)
|
145 |
+
|
146 |
inputs = [
|
147 |
sketch_image,
|
148 |
prompt,
|
|
|
151 |
sketch_scale,
|
152 |
seed
|
153 |
]
|
154 |
+
|
155 |
+
def set_mask(image):
|
156 |
+
img=image['image'][...,:3]
|
157 |
+
mask=1*(image['mask'][...,:3]>0)
|
158 |
+
# save vars
|
159 |
+
CURRENT_IMAGE['image']=img
|
160 |
+
CURRENT_IMAGE['mask']=mask
|
161 |
+
|
162 |
+
guide=get_guide(img)
|
163 |
+
CURRENT_IMAGE['guide']=np.array(guide)
|
164 |
+
guide=255-np.asarray(guide)
|
165 |
+
|
166 |
+
seg_img = guide*(1-mask) + mask*192
|
167 |
+
preview = img * (seg_img==255)
|
168 |
+
|
169 |
+
vis_image=(preview/2).astype(seg_img.dtype) + seg_img * (seg_img!=255)
|
170 |
+
|
171 |
+
return {input_image : image['image'], sketch_image : vis_image, step_2: gr.update(visible=True)}
|
172 |
+
|
173 |
+
mask_button.click(fn=set_mask, inputs=[input_image], outputs=[input_image, sketch_image,step_2])
|
174 |
run_button.click(fn=generate, inputs=inputs, outputs=output_image)
|
175 |
+
return demo
|
176 |
|
177 |
if __name__ == '__main__':
|
178 |
demo = create_demo()
|