Spaces:
Sleeping
Sleeping
michaelapplydesign
commited on
Commit
•
1cd414e
1
Parent(s):
7bff278
up 8
Browse files- app.py +18 -13
- config.py +6 -6
- models.py +20 -15
- pipelines.py +1 -1
- preprocessing.py +5 -5
app.py
CHANGED
@@ -3,7 +3,7 @@ import io
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
|
6 |
-
import
|
7 |
from models import make_image_controlnet, make_inpainting
|
8 |
from preprocessing import get_mask
|
9 |
|
@@ -24,30 +24,35 @@ def predict(input_img1,
|
|
24 |
resolution
|
25 |
):
|
26 |
|
|
|
|
|
27 |
print("predict")
|
28 |
# bla bla
|
29 |
# input_img1 = Image.fromarray(input_img1)
|
30 |
# input_img2 = Image.fromarray(input_img2)
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
input_img1 = input_img1.resize((
|
35 |
-
input_img2 = input_img2.resize((
|
36 |
|
37 |
canvas_mask = np.array(input_img2)
|
38 |
mask = get_mask(canvas_mask)
|
39 |
|
40 |
print(input_img1, mask, positive_prompt, negative_prompt)
|
41 |
|
42 |
-
retList=
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
while (len(retList)<10):
|
52 |
retList.append(None)
|
53 |
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
|
6 |
+
# from config import setResoluton
|
7 |
from models import make_image_controlnet, make_inpainting
|
8 |
from preprocessing import get_mask
|
9 |
|
|
|
24 |
resolution
|
25 |
):
|
26 |
|
27 |
+
|
28 |
+
|
29 |
print("predict")
|
30 |
# bla bla
|
31 |
# input_img1 = Image.fromarray(input_img1)
|
32 |
# input_img2 = Image.fromarray(input_img2)
|
33 |
|
34 |
+
# setResoluton(resolution)
|
35 |
+
HEIGHT = resolution
|
36 |
+
WIDTH = resolution
|
37 |
+
# WIDTH = resolution
|
38 |
+
# HEIGHT = resolution
|
39 |
|
40 |
+
input_img1 = input_img1.resize((resolution, resolution))
|
41 |
+
input_img2 = input_img2.resize((resolution, resolution))
|
42 |
|
43 |
canvas_mask = np.array(input_img2)
|
44 |
mask = get_mask(canvas_mask)
|
45 |
|
46 |
print(input_img1, mask, positive_prompt, negative_prompt)
|
47 |
|
48 |
+
retList= make_inpainting(positive_prompt=positive_prompt,
|
49 |
+
image=input_img1,
|
50 |
+
mask_image=mask,
|
51 |
+
negative_prompt=negative_prompt,
|
52 |
+
num_of_images=num_of_images,
|
53 |
+
resolution=resolution
|
54 |
+
)
|
55 |
+
# add the rest up to 10
|
|
|
56 |
while (len(retList)<10):
|
57 |
retList.append(None)
|
58 |
|
config.py
CHANGED
@@ -3,14 +3,14 @@ from palette import COLOR_MAPPING_, COLOR_MAPPING
|
|
3 |
|
4 |
# HEIGHT = 1024
|
5 |
# WIDTH = 1024
|
6 |
-
HEIGHT = 512
|
7 |
-
WIDTH = 512
|
8 |
#
|
9 |
|
10 |
-
def setResoluton(res):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
def to_rgb(color: str) -> tuple:
|
16 |
"""Convert hex color to rgb.
|
|
|
3 |
|
4 |
# HEIGHT = 1024
|
5 |
# WIDTH = 1024
|
6 |
+
# HEIGHT = 512
|
7 |
+
# WIDTH = 512
|
8 |
#
|
9 |
|
10 |
+
# def setResoluton(res):
|
11 |
+
# global HEIGHT, WIDTH
|
12 |
+
# HEIGHT = res
|
13 |
+
# WIDTH = res
|
14 |
|
15 |
def to_rgb(color: str) -> tuple:
|
16 |
"""Convert hex color to rgb.
|
models.py
CHANGED
@@ -12,7 +12,7 @@ from PIL import ImageFilter
|
|
12 |
|
13 |
from diffusers import ControlNetModel, UniPCMultistepScheduler
|
14 |
|
15 |
-
from config import WIDTH, HEIGHT
|
16 |
from palette import ade_palette
|
17 |
from stable_diffusion_controlnet_inpaint_img2img import StableDiffusionControlNetInpaintImg2ImgPipeline
|
18 |
from helpers import flush, postprocess_image_masking, convolution
|
@@ -70,7 +70,9 @@ def make_inpainting(positive_prompt: str,
|
|
70 |
image: Image,
|
71 |
mask_image: np.ndarray,
|
72 |
negative_prompt: str = "",
|
73 |
-
|
|
|
|
|
74 |
"""Method to make inpainting
|
75 |
Args:
|
76 |
positive_prompt (str): positive prompt string
|
@@ -85,17 +87,20 @@ def make_inpainting(positive_prompt: str,
|
|
85 |
mask_image_postproc = convolution(mask_image)
|
86 |
|
87 |
flush()
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
100 |
|
101 |
-
return
|
|
|
12 |
|
13 |
from diffusers import ControlNetModel, UniPCMultistepScheduler
|
14 |
|
15 |
+
# from config import WIDTH, HEIGHT
|
16 |
from palette import ade_palette
|
17 |
from stable_diffusion_controlnet_inpaint_img2img import StableDiffusionControlNetInpaintImg2ImgPipeline
|
18 |
from helpers import flush, postprocess_image_masking, convolution
|
|
|
70 |
image: Image,
|
71 |
mask_image: np.ndarray,
|
72 |
negative_prompt: str = "",
|
73 |
+
num_of_images: int = 1,
|
74 |
+
resolution:int = 512) -> List[Image.Image]:
|
75 |
+
|
76 |
"""Method to make inpainting
|
77 |
Args:
|
78 |
positive_prompt (str): positive prompt string
|
|
|
87 |
mask_image_postproc = convolution(mask_image)
|
88 |
|
89 |
flush()
|
90 |
+
retList=[]
|
91 |
+
for x in range(num_of_images):
|
92 |
+
st.success(f"{pipe.queue_size} images in the queue, can take up to {(pipe.queue_size+1) * 10} seconds")
|
93 |
+
resp = pipe(image=image,
|
94 |
+
mask_image=mask_image,
|
95 |
+
prompt=positive_prompt,
|
96 |
+
negative_prompt=negative_prompt,
|
97 |
+
num_inference_steps=50,
|
98 |
+
height=resolution,
|
99 |
+
width=resolution,
|
100 |
+
)
|
101 |
+
print("RESP !!!!",resp)
|
102 |
+
generated_image = resp.images[0]
|
103 |
+
generated_image = postprocess_image_masking(generated_image, image, mask_image_postproc)
|
104 |
+
retList.append(generated_image)
|
105 |
|
106 |
+
return retList
|
pipelines.py
CHANGED
@@ -15,7 +15,7 @@ from PIL import ImageFilter
|
|
15 |
from diffusers import ControlNetModel, UniPCMultistepScheduler
|
16 |
from diffusers import StableDiffusionInpaintPipeline
|
17 |
|
18 |
-
from config import WIDTH, HEIGHT
|
19 |
from stable_diffusion_controlnet_inpaint_img2img import StableDiffusionControlNetInpaintImg2ImgPipeline
|
20 |
from helpers import flush
|
21 |
|
|
|
15 |
from diffusers import ControlNetModel, UniPCMultistepScheduler
|
16 |
from diffusers import StableDiffusionInpaintPipeline
|
17 |
|
18 |
+
# from config import WIDTH, HEIGHT
|
19 |
from stable_diffusion_controlnet_inpaint_img2img import StableDiffusionControlNetInpaintImg2ImgPipeline
|
20 |
from helpers import flush
|
21 |
|
preprocessing.py
CHANGED
@@ -6,8 +6,7 @@ import numpy as np
|
|
6 |
from PIL import Image, ImageFilter
|
7 |
import streamlit as st
|
8 |
|
9 |
-
import
|
10 |
-
|
11 |
# from enhance_config import ENHANCE_SETTINGS
|
12 |
|
13 |
LOGGING = logging.getLogger(__name__)
|
@@ -36,7 +35,7 @@ def preprocess_seg_mask(canvas_seg, real_seg: Image.Image = None) -> Tuple[np.nd
|
|
36 |
unique_colors = [color for color in unique_colors if np.sum(
|
37 |
np.all(image_seg == color, axis=-1)) > 100]
|
38 |
|
39 |
-
unique_colors_exact = [color for color in unique_colors if color in
|
40 |
|
41 |
if real_seg is not None:
|
42 |
overlay_seg = np.array(real_seg)
|
@@ -68,6 +67,7 @@ def get_mask(image_mask: np.ndarray) -> np.ndarray:
|
|
68 |
|
69 |
|
70 |
def get_image() -> np.ndarray:
|
|
|
71 |
"""Get the image from the session state.
|
72 |
Returns:
|
73 |
np.ndarray: image
|
@@ -75,9 +75,9 @@ def get_image() -> np.ndarray:
|
|
75 |
if 'initial_image' in st.session_state and st.session_state['initial_image'] is not None:
|
76 |
initial_image = st.session_state['initial_image']
|
77 |
if isinstance(initial_image, Image.Image):
|
78 |
-
return np.array(initial_image.resize((
|
79 |
else:
|
80 |
-
return np.array(Image.fromarray(initial_image).resize((
|
81 |
else:
|
82 |
return None
|
83 |
|
|
|
6 |
from PIL import Image, ImageFilter
|
7 |
import streamlit as st
|
8 |
|
9 |
+
from config import COLOR_RGB
|
|
|
10 |
# from enhance_config import ENHANCE_SETTINGS
|
11 |
|
12 |
LOGGING = logging.getLogger(__name__)
|
|
|
35 |
unique_colors = [color for color in unique_colors if np.sum(
|
36 |
np.all(image_seg == color, axis=-1)) > 100]
|
37 |
|
38 |
+
unique_colors_exact = [color for color in unique_colors if color in COLOR_RGB]
|
39 |
|
40 |
if real_seg is not None:
|
41 |
overlay_seg = np.array(real_seg)
|
|
|
67 |
|
68 |
|
69 |
def get_image() -> np.ndarray:
|
70 |
+
|
71 |
"""Get the image from the session state.
|
72 |
Returns:
|
73 |
np.ndarray: image
|
|
|
75 |
if 'initial_image' in st.session_state and st.session_state['initial_image'] is not None:
|
76 |
initial_image = st.session_state['initial_image']
|
77 |
if isinstance(initial_image, Image.Image):
|
78 |
+
return np.array(initial_image.resize((WIDTH, HEIGHT)))
|
79 |
else:
|
80 |
+
return np.array(Image.fromarray(initial_image).resize((WIDTH, HEIGHT)))
|
81 |
else:
|
82 |
return None
|
83 |
|