Spaces:
Runtime error
Runtime error
TakahashiShotaro
commited on
Commit
•
d1ddda1
1
Parent(s):
311538b
Update app.py
Browse files
app.py
CHANGED
@@ -9,25 +9,43 @@ import time
|
|
9 |
|
10 |
from models import make_image_controlnet, make_inpainting
|
11 |
from segmentation import segment_image
|
12 |
-
from config import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
from palette import COLOR_MAPPING_CATEGORY
|
14 |
from preprocessing import preprocess_seg_mask, get_image, get_mask
|
15 |
-
from explanation import
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# wide layout
|
17 |
st.set_page_config(layout="wide")
|
18 |
|
19 |
|
20 |
def on_upload() -> None:
|
21 |
"""Upload image to the canvas."""
|
22 |
-
if
|
23 |
-
|
24 |
-
st.session_state[
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def make_image_row(image_0, image_1):
|
33 |
col_0, col_1 = st.columns(2)
|
@@ -42,17 +60,19 @@ def check_reset_state() -> bool:
|
|
42 |
Returns:
|
43 |
bool: True if the UI elements need to be reset, False otherwise
|
44 |
"""
|
45 |
-
if
|
46 |
-
st.session_state[
|
47 |
return True
|
48 |
-
st.session_state[
|
49 |
return False
|
50 |
|
51 |
|
52 |
-
def move_image(
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
"""Move image from source to destination.
|
57 |
Args:
|
58 |
source (Union[str, Image.Image]): source image
|
@@ -60,24 +80,26 @@ def move_image(source: Union[str, Image.Image],
|
|
60 |
rerun (bool, optional): rerun streamlit. Defaults to True.
|
61 |
remove_state (bool, optional): remove the canvas state. Defaults to True.
|
62 |
"""
|
63 |
-
source_image =
|
|
|
|
|
64 |
|
65 |
if remove_state:
|
66 |
-
st.session_state[
|
67 |
-
if
|
68 |
-
del st.session_state[
|
69 |
-
if
|
70 |
-
del st.session_state[
|
71 |
|
72 |
st.session_state[dest] = source_image
|
73 |
-
st.session_state[
|
74 |
if rerun:
|
75 |
st.experimental_rerun()
|
76 |
|
77 |
|
78 |
def on_change_radio() -> None:
|
79 |
"""Reset the UI elements when the radio button is changed."""
|
80 |
-
st.session_state[
|
81 |
|
82 |
|
83 |
def make_canvas_dict(canvas_color, brush, paint_mode, _reset_state):
|
@@ -85,311 +107,187 @@ def make_canvas_dict(canvas_color, brush, paint_mode, _reset_state):
|
|
85 |
fill_color=canvas_color,
|
86 |
stroke_color=canvas_color,
|
87 |
background_color="#FFFFFF",
|
88 |
-
background_image=st.session_state[
|
|
|
|
|
89 |
stroke_width=brush,
|
90 |
-
initial_drawing={
|
91 |
update_streamlit=True,
|
92 |
height=512,
|
93 |
width=512,
|
94 |
drawing_mode=paint_mode,
|
95 |
key="canvas",
|
96 |
)
|
97 |
-
return canvas_dict
|
98 |
-
|
99 |
-
def make_prompt_row():
|
100 |
-
col_0_0, col_0_1 = st.columns(2)
|
101 |
-
with col_0_0:
|
102 |
-
st.text_input(label="Positive prompt", value="a photograph of a room, interior design, 4k, high resolution", key='positive_prompt')
|
103 |
-
with col_0_1:
|
104 |
-
st.text_input(label="Negative prompt", value="lowres, watermark, banner, logo, watermark, contactinfo, text, deformed, blurry, blur, out of focus, out of frame, surreal, ugly", key='negative_prompt')
|
105 |
-
|
106 |
-
def make_sidebar():
|
107 |
-
with st.sidebar:
|
108 |
-
input_image = st.file_uploader("", type=["png", "jpg"], key='input_image', on_change=on_upload)
|
109 |
-
generation_mode = st.selectbox("Generation mode", ["Regenerate",
|
110 |
-
"Segmentation",
|
111 |
-
"Inpainting"], on_change=on_change_radio)
|
112 |
-
|
113 |
-
|
114 |
-
if generation_mode == "Segmentation":
|
115 |
-
paint_mode = st.sidebar.selectbox("Painting mode", ("freedraw", "polygon"))
|
116 |
-
if paint_mode == "freedraw":
|
117 |
-
brush = st.slider("Stroke width", 5, 140, 100, key='slider_seg')
|
118 |
-
else:
|
119 |
-
brush = 5
|
120 |
-
|
121 |
-
category_chooser = st.sidebar.selectbox("Filter on category", list(
|
122 |
-
COLOR_MAPPING_CATEGORY.keys()), index=0, key='category_chooser')
|
123 |
-
|
124 |
-
chosen_colors = list(COLOR_MAPPING_CATEGORY[category_chooser].keys())
|
125 |
-
|
126 |
-
color_chooser = st.sidebar.selectbox(
|
127 |
-
"Choose a color", chosen_colors, index=0, format_func=map_colors, key='color_chooser'
|
128 |
-
)
|
129 |
|
130 |
-
elif generation_mode == "Regenerate":
|
131 |
-
color_chooser = "rgba(0, 0, 0, 0.0)"
|
132 |
-
paint_mode = 'freedraw'
|
133 |
-
brush = 0
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
if paint_mode == "freedraw":
|
138 |
-
brush = st.slider("Stroke width", 5, 140, 100, key='slider_seg')
|
139 |
-
else:
|
140 |
-
brush = 5
|
141 |
|
142 |
-
|
143 |
-
|
|
|
|
|
144 |
|
|
|
|
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
output_image = Image.fromarray(output_image)
|
151 |
|
152 |
-
|
153 |
-
|
154 |
else:
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
-
st.write("#### Output image")
|
158 |
-
st.image(output_image, width=512)
|
159 |
-
if st.button("Move to input image"):
|
160 |
-
move_image('output_image', 'initial_image', remove_state=True, rerun=True)
|
161 |
|
162 |
def make_editing_canvas(canvas_color, brush, _reset_state, generation_mode, paint_mode):
|
163 |
-
st.write("####
|
164 |
canvas_dict = make_canvas_dict(
|
165 |
canvas_color=canvas_color,
|
166 |
paint_mode=paint_mode,
|
167 |
brush=brush,
|
168 |
-
_reset_state=_reset_state
|
169 |
)
|
170 |
-
if generation_mode == "Segmentation":
|
171 |
-
canvas = st_canvas(
|
172 |
-
**canvas_dict,
|
173 |
-
)
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
with st.spinner(text="Generating image"):
|
182 |
-
print("Making image")
|
183 |
-
result_image = make_image_controlnet(image=image,
|
184 |
-
mask_image=mask,
|
185 |
-
controlnet_conditioning_image=seg,
|
186 |
-
positive_prompt=st.session_state['positive_prompt'],
|
187 |
-
negative_prompt=st.session_state['negative_prompt'],
|
188 |
-
seed=random.randint(0, 100000) # nosec
|
189 |
-
)
|
190 |
-
if isinstance(result_image, np.ndarray):
|
191 |
-
result_image = Image.fromarray(result_image)
|
192 |
-
st.session_state['output_image'] = result_image
|
193 |
-
|
194 |
-
|
195 |
-
elif generation_mode == "Regenerate":
|
196 |
-
canvas = st_canvas(
|
197 |
-
**canvas_dict,
|
198 |
-
)
|
199 |
-
if 'seg' not in st.session_state:
|
200 |
-
with st.spinner(text="Preparing image segmentation"):
|
201 |
-
image = get_image()
|
202 |
-
real_seg = np.array(segment_image(Image.fromarray(image)))
|
203 |
-
st.session_state['seg'] = real_seg
|
204 |
-
|
205 |
-
if 'unique_colors' not in st.session_state:
|
206 |
-
real_seg = st.session_state['seg']
|
207 |
-
unique_colors = np.unique(real_seg.reshape(-1, real_seg.shape[2]), axis=0)
|
208 |
-
unique_colors = [tuple(color) for color in unique_colors]
|
209 |
-
st.session_state['unique_colors'] = unique_colors
|
210 |
-
|
211 |
-
with st.expander("Explanation", expanded=True):
|
212 |
-
st.write("This mode allows you to choose which objects you want to re-generate in the image. "
|
213 |
-
"Use the selection dropdown to add or remove objects. If you are ready, press the generate button"
|
214 |
-
" to generate the image, which can take up to 30 seconds. If you want to improve the generated image, click"
|
215 |
-
" the 'move image to input' button."
|
216 |
-
)
|
217 |
-
|
218 |
-
chosen_colors = st.multiselect(
|
219 |
-
label="Choose which concepts you want to regenerate in the image",
|
220 |
-
options=st.session_state['unique_colors'],
|
221 |
-
key='chosen_colors',
|
222 |
-
default=st.session_state['unique_colors'],
|
223 |
-
format_func=map_colors_rgb,
|
224 |
-
)
|
225 |
-
|
226 |
-
if st.button("generate image", key='generate_button'):
|
227 |
image = get_image()
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
canvas = st_canvas(
|
252 |
-
**canvas_dict,
|
253 |
-
)
|
254 |
|
255 |
-
if st.button("generate images", key='generate_button'):
|
256 |
-
canvas_mask = canvas.image_data
|
257 |
-
if not isinstance(canvas_mask, np.ndarray):
|
258 |
-
canvas_mask = np.array(canvas_mask)
|
259 |
-
mask = get_mask(canvas_mask)
|
260 |
-
|
261 |
-
with st.spinner(text="Generating new images"):
|
262 |
-
print("Making image")
|
263 |
-
result_image = make_inpainting(positive_prompt=st.session_state['positive_prompt'],
|
264 |
-
image=Image.fromarray(image),
|
265 |
-
mask_image=mask,
|
266 |
-
negative_prompt=st.session_state['negative_prompt'],
|
267 |
-
)
|
268 |
-
if isinstance(result_image, np.ndarray):
|
269 |
-
result_image = Image.fromarray(result_image)
|
270 |
-
st.session_state['output_image'] = result_image
|
271 |
|
272 |
def main():
|
273 |
# center text
|
274 |
-
st.write("##
|
275 |
-
|
276 |
-
input_image, generation_mode, brush, color_chooser, paint_mode = make_sidebar()
|
277 |
-
|
278 |
-
# check if there is an input_image
|
279 |
-
if not ('initial_image' in st.session_state and st.session_state['initial_image'] is not None):
|
280 |
-
st.success("Upload an image to start")
|
281 |
-
st.write("Welcome to the interior design controlnet demo! "
|
282 |
-
"You can start by uploading a picture of your room, after which you will see "
|
283 |
-
"a good variety of options to edit your current room to generate the room of your dreams! "
|
284 |
-
"You can choose between inpainting, Segmentation and re-generating objects, which "
|
285 |
-
"use our custom trained controlnet model. The main idea is that you can iterate over the "
|
286 |
-
"generated images, because you will rarely get something perfect in one step (although it's possible). "
|
287 |
-
"We added functionality to load in the generated image into the input, so you can keep "
|
288 |
-
"changing the image until you are satisfied."
|
289 |
-
)
|
290 |
-
with st.expander("Useful information", expanded=True):
|
291 |
-
st.write("### About the dataset")
|
292 |
-
st.write("To make this demo as good as possible, our team spend a lot of time training a custom model. "
|
293 |
-
"We used the LAION5B dataset to build our custom dataset, which contains 130k images of 15 types of rooms "
|
294 |
-
"in almost 30 design styles. After fetching all these images, we started adding metadata such as "
|
295 |
-
"captions (from the BLIP captioning model) and segmentation maps (from the HuggingFace UperNetForSemanticSegmentation model). "
|
296 |
-
)
|
297 |
-
st.write("For the gathering and inference of the metadata we used the Fondant framework (https://github.com/ml6team/fondant) provided by ML6 (https://www.ml6.eu/), which is an open source "
|
298 |
-
"data centric framework for data preparation. The pipeline used for training this controlnet will soon be available as an "
|
299 |
-
"example pipeline within Fondant and can be easily adapted for building your own dataset."
|
300 |
-
)
|
301 |
-
st.write("### About the model")
|
302 |
-
st.write(
|
303 |
-
"These were then used to train the controlnet model to generate quality interior design images by using "
|
304 |
-
"the segmentation maps and prompts as conditioning information for the model. "
|
305 |
-
"By training on segmentation maps, the enduser has a very finegrained control over which objects they "
|
306 |
-
"want to place in their room. "
|
307 |
-
"The resulting model is then used in a community pipeline that supports image2image and inpainting, "
|
308 |
-
"so the user can keep elements of their room and change specific parts of the image."
|
309 |
-
""
|
310 |
-
)
|
311 |
-
|
312 |
-
st.write("### Trivia")
|
313 |
-
st.write("The first time someone uses the demo after startup, the models still need to be loaded into memory. "
|
314 |
-
"After this initial load, the model is cached as a resource and can be used for all the users. "
|
315 |
-
"To avoid simultaneous requests, we have implemented a queueing mechanism that ensures that only one "
|
316 |
-
"user accesses the model at a time (similar to the Gradio framework).\n"
|
317 |
-
)
|
318 |
-
st.write("To enable the features in the demo, we calculate the underlying segmentation maps and categories that "
|
319 |
-
"are present in the image. This allows us to hide some of the manual work for the user, and "
|
320 |
-
"by doing this, the users don't need to make a segmentation map in an external tool. Everything needed can be done within this demo."
|
321 |
-
)
|
322 |
-
|
323 |
-
# st.write("### News: Fondant - an open source data-centric framework for Foundation model finetuning")
|
324 |
-
# st.write("The ML6 team is proud to announce that we are open sourcing our Fondant framework, which is a "
|
325 |
-
# "data-centric framework that allows you to prepare large scale multimodal datasets with ease. We have implemented the components "
|
326 |
-
# "that we used to train this controlnet model in Fondant as an example pipeline, and we are excited to see what you can do with it! In the future we will add a whole library of plug-and-play data preparation components, such as different ML models and filtering steps, in addition to dataset scraping components that connect to LAION5B."
|
327 |
-
# )
|
328 |
-
# st.write("The framework is built on top of kubeflow pipelines and abstracts all the complexity of efficient storing and moving of large datasets, so you can focus on implemented just that piece of code that you need without worrying about the rest. We also build it to run on each Cloud provider or VM. You can find the code on our github page: https://github.com/ml6team/fondant.")
|
329 |
-
|
330 |
-
st.write("### Testing images")
|
331 |
-
st.write("If you don't have any pictures close, you can use one of these images to test the model by clicking on the 'use example X' buttons")
|
332 |
-
|
333 |
-
st.session_state['example_image_0'] = Image.open("content/example_0.png")
|
334 |
-
st.session_state['example_image_1'] = Image.open("content/example_1.jpg")
|
335 |
-
st.session_state['example_image_2'] = Image.open("content/example_2.jpg")
|
336 |
-
st.session_state['example_image_3'] = Image.open("content/example_3.jpg")
|
337 |
-
|
338 |
-
col_im_0, col_im_1 = st.columns(2)
|
339 |
-
|
340 |
-
with col_im_0:
|
341 |
-
st.image(st.session_state['example_image_0'], caption="Example image 1", use_column_width=True)
|
342 |
-
if st.button("Use example 1"):
|
343 |
-
move_image('example_image_0', 'initial_image', remove_state=True, rerun=True)
|
344 |
-
|
345 |
-
st.image(st.session_state['example_image_2'], caption="Example image 3", use_column_width=True)
|
346 |
-
if st.button("Use example 3"):
|
347 |
-
move_image('example_image_2', 'initial_image', remove_state=True, rerun=True)
|
348 |
-
with col_im_1:
|
349 |
-
st.image(st.session_state['example_image_1'], caption="Example image 2", use_column_width=True)
|
350 |
-
if st.button("Use example 2"):
|
351 |
-
move_image('example_image_1', 'initial_image', remove_state=True, rerun=True)
|
352 |
-
|
353 |
-
st.image(st.session_state['example_image_3'], caption="Example image 4", use_column_width=True)
|
354 |
-
if st.button("Use example 4"):
|
355 |
-
move_image('example_image_3', 'initial_image', remove_state=True, rerun=True)
|
356 |
-
|
357 |
-
st.write("## Generated examples")
|
358 |
-
make_image_row(Image.open("content/output_1.png"), Image.open("content/regen_example.png"))
|
359 |
-
make_image_row(Image.open("content/keep background 2.png"), Image.open("content/output_0.png"))
|
360 |
-
make_image_row(Image.open("content/segmentation window.png"), Image.open("content/output_3.png"))
|
361 |
-
|
362 |
-
st.write("## Example video")
|
363 |
-
st.write("### Video 1")
|
364 |
-
st.video(open('content/controlnet_sprint_demo.mp4', 'rb').read())
|
365 |
-
st.write("### Video 2")
|
366 |
-
st.video(open('content/controlnet_demo_video_2.mp4', 'rb').read())
|
367 |
-
|
368 |
-
else:
|
369 |
-
make_prompt_row()
|
370 |
|
371 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
372 |
|
373 |
-
|
374 |
-
make_inpainting_explanation()
|
375 |
-
elif generation_mode == "Segmentation":
|
376 |
-
make_segmentation_explanation()
|
377 |
-
elif generation_mode == "Regenerate":
|
378 |
-
make_regeneration_explanation()
|
379 |
|
|
|
380 |
col1, col2 = st.columns(2)
|
381 |
with col1:
|
382 |
-
make_editing_canvas(
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
|
|
388 |
|
389 |
with col2:
|
390 |
make_output_image()
|
391 |
|
|
|
392 |
if __name__ == "__main__":
|
393 |
main()
|
394 |
-
|
395 |
-
|
|
|
9 |
|
10 |
from models import make_image_controlnet, make_inpainting
|
11 |
from segmentation import segment_image
|
12 |
+
from config import (
|
13 |
+
HEIGHT,
|
14 |
+
WIDTH,
|
15 |
+
POS_PROMPT,
|
16 |
+
NEG_PROMPT,
|
17 |
+
COLOR_MAPPING,
|
18 |
+
map_colors,
|
19 |
+
map_colors_rgb,
|
20 |
+
)
|
21 |
from palette import COLOR_MAPPING_CATEGORY
|
22 |
from preprocessing import preprocess_seg_mask, get_image, get_mask
|
23 |
+
from explanation import (
|
24 |
+
make_inpainting_explanation,
|
25 |
+
make_regeneration_explanation,
|
26 |
+
make_segmentation_explanation,
|
27 |
+
)
|
28 |
+
from colors import INTERIOR
|
29 |
+
|
30 |
# wide layout
|
31 |
st.set_page_config(layout="wide")
|
32 |
|
33 |
|
34 |
def on_upload() -> None:
|
35 |
"""Upload image to the canvas."""
|
36 |
+
if (
|
37 |
+
"input_image" in st.session_state
|
38 |
+
and st.session_state["input_image"] is not None
|
39 |
+
):
|
40 |
+
image = Image.open(st.session_state["input_image"]).convert("RGB")
|
41 |
+
st.session_state["initial_image"] = image
|
42 |
+
if "seg" in st.session_state:
|
43 |
+
del st.session_state["seg"]
|
44 |
+
if "unique_colors" in st.session_state:
|
45 |
+
del st.session_state["unique_colors"]
|
46 |
+
if "output_image" in st.session_state:
|
47 |
+
del st.session_state["output_image"]
|
48 |
+
|
49 |
|
50 |
def make_image_row(image_0, image_1):
|
51 |
col_0, col_1 = st.columns(2)
|
|
|
60 |
Returns:
|
61 |
bool: True if the UI elements need to be reset, False otherwise
|
62 |
"""
|
63 |
+
if "reset_canvas" in st.session_state and st.session_state["reset_canvas"]:
|
64 |
+
st.session_state["reset_canvas"] = False
|
65 |
return True
|
66 |
+
st.session_state["reset_canvas"] = False
|
67 |
return False
|
68 |
|
69 |
|
70 |
+
def move_image(
|
71 |
+
source: Union[str, Image.Image],
|
72 |
+
dest: str,
|
73 |
+
rerun: bool = True,
|
74 |
+
remove_state: bool = True,
|
75 |
+
) -> None:
|
76 |
"""Move image from source to destination.
|
77 |
Args:
|
78 |
source (Union[str, Image.Image]): source image
|
|
|
80 |
rerun (bool, optional): rerun streamlit. Defaults to True.
|
81 |
remove_state (bool, optional): remove the canvas state. Defaults to True.
|
82 |
"""
|
83 |
+
source_image = (
|
84 |
+
source if isinstance(source, Image.Image) else st.session_state[source]
|
85 |
+
)
|
86 |
|
87 |
if remove_state:
|
88 |
+
st.session_state["reset_canvas"] = True
|
89 |
+
if "seg" in st.session_state:
|
90 |
+
del st.session_state["seg"]
|
91 |
+
if "unique_colors" in st.session_state:
|
92 |
+
del st.session_state["unique_colors"]
|
93 |
|
94 |
st.session_state[dest] = source_image
|
95 |
+
st.session_state["dest"] = source_image
|
96 |
if rerun:
|
97 |
st.experimental_rerun()
|
98 |
|
99 |
|
100 |
def on_change_radio() -> None:
|
101 |
"""Reset the UI elements when the radio button is changed."""
|
102 |
+
st.session_state["reset_canvas"] = True
|
103 |
|
104 |
|
105 |
def make_canvas_dict(canvas_color, brush, paint_mode, _reset_state):
|
|
|
107 |
fill_color=canvas_color,
|
108 |
stroke_color=canvas_color,
|
109 |
background_color="#FFFFFF",
|
110 |
+
background_image=st.session_state["initial_image"]
|
111 |
+
if "initial_image" in st.session_state
|
112 |
+
else None,
|
113 |
stroke_width=brush,
|
114 |
+
initial_drawing={"version": "4.4.0", "objects": []} if _reset_state else None,
|
115 |
update_streamlit=True,
|
116 |
height=512,
|
117 |
width=512,
|
118 |
drawing_mode=paint_mode,
|
119 |
key="canvas",
|
120 |
)
|
121 |
+
return canvas_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
def make_output_image():
|
125 |
+
st.write("#### After")
|
|
|
|
|
|
|
|
|
126 |
|
127 |
+
output_images = st.session_state["output_images"] if st.session_state["output_images"] else []
|
128 |
+
for output_image in output_images:
|
129 |
+
if isinstance(output_image, np.ndarray):
|
130 |
+
output_image = Image.fromarray(output_image)
|
131 |
|
132 |
+
if isinstance(output_image, Image.Image):
|
133 |
+
output_image = output_image.resize((512, 512))
|
134 |
|
135 |
+
if len(output_images) >= 1:
|
136 |
+
st.image(output_images[0], width=512)
|
137 |
+
else:
|
138 |
+
st.spinner()
|
|
|
139 |
|
140 |
+
if len(output_images) >= 2:
|
141 |
+
st.image(output_images[1], width=512)
|
142 |
else:
|
143 |
+
st.spinner()
|
144 |
+
|
145 |
+
if len(output_images) >= 3:
|
146 |
+
st.image(output_images[2], width=512)
|
147 |
+
else:
|
148 |
+
st.spinner()
|
149 |
+
|
150 |
+
if len(output_images) >= 4:
|
151 |
+
st.image(output_images[3], width=512)
|
152 |
+
else:
|
153 |
+
st.spinner()
|
154 |
+
|
155 |
+
def generate():
|
156 |
+
image = get_image()
|
157 |
+
|
158 |
+
segmentation = st.session_state["seg"]
|
159 |
+
mask = np.zeros_like(segmentation)
|
160 |
+
interior = list(INTERIOR.values())
|
161 |
+
print(interior)
|
162 |
+
for color in st.session_state["unique_colors"]:
|
163 |
+
print(map_colors_rgb(color))
|
164 |
+
# 壁や床を変えると違う家になってしまう
|
165 |
+
if map_colors_rgb(color) in interior:
|
166 |
+
continue
|
167 |
+
# if the color is in the segmentation, set mask to 1
|
168 |
+
mask[np.where((segmentation == color).all(axis=2))] = 1
|
169 |
+
|
170 |
+
positive_prompt = "a photograph of a room, interior design, 4k, high resolution"
|
171 |
+
negative_prompt = "lowres, watermark, banner, logo, watermark, contactinfo, text, deformed, blurry, blur, out of focus, out of frame, surreal, ugly"
|
172 |
+
|
173 |
+
with st.spinner(text="生成中…"):
|
174 |
+
st.session_state["output_images"] = []
|
175 |
+
result_image = make_image_controlnet(
|
176 |
+
image=image,
|
177 |
+
mask_image=mask,
|
178 |
+
controlnet_conditioning_image=segmentation,
|
179 |
+
positive_prompt=positive_prompt,
|
180 |
+
negative_prompt=negative_prompt,
|
181 |
+
seed=random.randint(0, 100000), # nosec
|
182 |
+
)
|
183 |
+
if isinstance(result_image, np.ndarray):
|
184 |
+
result_image = Image.fromarray(result_image)
|
185 |
+
st.session_state["output_images"].append(result_image)
|
186 |
+
|
187 |
+
result_image = make_image_controlnet(
|
188 |
+
image=image,
|
189 |
+
mask_image=mask,
|
190 |
+
controlnet_conditioning_image=segmentation,
|
191 |
+
positive_prompt=positive_prompt,
|
192 |
+
negative_prompt=negative_prompt,
|
193 |
+
seed=random.randint(0, 100000), # nosec
|
194 |
+
)
|
195 |
+
if isinstance(result_image, np.ndarray):
|
196 |
+
result_image = Image.fromarray(result_image)
|
197 |
+
st.session_state["output_images"].append(result_image)
|
198 |
+
|
199 |
+
result_image = make_image_controlnet(
|
200 |
+
image=image,
|
201 |
+
mask_image=mask,
|
202 |
+
controlnet_conditioning_image=segmentation,
|
203 |
+
positive_prompt=positive_prompt,
|
204 |
+
negative_prompt=negative_prompt,
|
205 |
+
seed=random.randint(0, 100000), # nosec
|
206 |
+
)
|
207 |
+
if isinstance(result_image, np.ndarray):
|
208 |
+
result_image = Image.fromarray(result_image)
|
209 |
+
st.session_state["output_images"].append(result_image)
|
210 |
+
|
211 |
+
result_image = make_image_controlnet(
|
212 |
+
image=image,
|
213 |
+
mask_image=mask,
|
214 |
+
controlnet_conditioning_image=segmentation,
|
215 |
+
positive_prompt=positive_prompt,
|
216 |
+
negative_prompt=negative_prompt,
|
217 |
+
seed=random.randint(0, 100000), # nosec
|
218 |
+
)
|
219 |
+
if isinstance(result_image, np.ndarray):
|
220 |
+
result_image = Image.fromarray(result_image)
|
221 |
+
st.session_state["output_images"].append(result_image)
|
222 |
|
|
|
|
|
|
|
|
|
223 |
|
224 |
def make_editing_canvas(canvas_color, brush, _reset_state, generation_mode, paint_mode):
|
225 |
+
st.write("#### Before")
|
226 |
canvas_dict = make_canvas_dict(
|
227 |
canvas_color=canvas_color,
|
228 |
paint_mode=paint_mode,
|
229 |
brush=brush,
|
230 |
+
_reset_state=_reset_state,
|
231 |
)
|
|
|
|
|
|
|
|
|
232 |
|
233 |
+
canvas = st_canvas(
|
234 |
+
**canvas_dict,
|
235 |
+
)
|
236 |
+
if "seg" not in st.session_state:
|
237 |
+
with st.spinner(text="生成中…"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
image = get_image()
|
239 |
+
real_seg = np.array(segment_image(Image.fromarray(image)))
|
240 |
+
st.session_state["seg"] = real_seg
|
241 |
+
|
242 |
+
if "unique_colors" not in st.session_state:
|
243 |
+
real_seg = st.session_state["seg"]
|
244 |
+
unique_colors = np.unique(real_seg.reshape(-1, real_seg.shape[2]), axis=0)
|
245 |
+
unique_colors = [tuple(color) for color in unique_colors]
|
246 |
+
st.session_state["unique_colors"] = unique_colors
|
247 |
+
|
248 |
+
# chosen_colors = st.multiselect(
|
249 |
+
# label="Choose which concepts you want to regenerate in the image",
|
250 |
+
# options=st.session_state['unique_colors'],
|
251 |
+
# key='chosen_colors',
|
252 |
+
# default=st.session_state['unique_colors'],
|
253 |
+
# format_func=map_colors_rgb,
|
254 |
+
# )
|
255 |
+
|
256 |
+
if "output_images" not in st.session_state:
|
257 |
+
generate()
|
258 |
+
else:
|
259 |
+
if st.button("再生成"):
|
260 |
+
generate()
|
|
|
|
|
|
|
|
|
261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
def main():
|
264 |
# center text
|
265 |
+
st.write("## Interior AI", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
|
267 |
+
input_image = st.file_uploader(
|
268 |
+
"部屋の画像を選択してください", type=["png", "jpg"], key="input_image", on_change=on_upload
|
269 |
+
)
|
270 |
+
generation_mode = "Regenerate"
|
271 |
+
color_chooser = "rgba(0, 0, 0, 0.0)"
|
272 |
+
paint_mode = "freedraw"
|
273 |
+
brush = 0
|
274 |
|
275 |
+
_reset_state = check_reset_state()
|
|
|
|
|
|
|
|
|
|
|
276 |
|
277 |
+
if input_image:
|
278 |
col1, col2 = st.columns(2)
|
279 |
with col1:
|
280 |
+
make_editing_canvas(
|
281 |
+
canvas_color=color_chooser,
|
282 |
+
brush=brush,
|
283 |
+
_reset_state=_reset_state,
|
284 |
+
generation_mode=generation_mode,
|
285 |
+
paint_mode=paint_mode,
|
286 |
+
)
|
287 |
|
288 |
with col2:
|
289 |
make_output_image()
|
290 |
|
291 |
+
|
292 |
if __name__ == "__main__":
|
293 |
main()
|
|
|
|