Spaces:
Build error
Build error
Sophie98
commited on
Commit
Β·
09012f9
1
Parent(s):
1eef631
finishing touches
Browse files- .flake8 +12 -0
- README.md +2 -2
- app.py +118 -70
- figures/sofa_example2.jpg +0 -0
- figures/sofa_example3.jpg +0 -0
- figures/style_example10.jpg +0 -0
- figures/style_example11.jpg +0 -0
- figures/style_example6.jpg +0 -0
- figures/style_example7.jpg +0 -0
- figures/style_example8.jpg +0 -0
- figures/style_example9.jpg +0 -0
- gradio_cached_examples/log.csv +4 -0
- gradio_cached_examples/output/0.png +0 -0
- gradio_cached_examples/output/1.png +0 -0
- gradio_cached_examples/output/2.png +0 -0
.flake8
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[flake8]
|
2 |
+
exclude =
|
3 |
+
.git,
|
4 |
+
*.egg-info,
|
5 |
+
__pycache__,
|
6 |
+
.tox,
|
7 |
+
.pytest_cache,
|
8 |
+
build,
|
9 |
+
dist,
|
10 |
+
tests
|
11 |
+
max-line-length = 88
|
12 |
+
ignore = D202,W503,E203 # conflicts with black
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: SofaStyler
|
3 |
emoji: π
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 2.9.4
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: SofaStyler
|
3 |
emoji: π
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 2.9.4
|
8 |
app_file: app.py
|
app.py
CHANGED
@@ -1,138 +1,186 @@
|
|
1 |
-
from
|
2 |
-
|
|
|
3 |
import gradio as gr
|
4 |
-
|
5 |
-
from StyleTransfer.styleTransfer import create_styledSofa
|
6 |
from PIL import Image
|
7 |
-
from random import randint
|
8 |
|
|
|
|
|
9 |
|
10 |
-
#https://colab.research.google.com/drive/11CtQpSeRBGAuw4TtE_rL470tRo-1X-p2#scrollTo=edGukUHXyymr
|
11 |
-
#https://colab.research.google.com/drive/1xq33YKf0LVKCkbbUZIoNPzgpR_4Kd0qL#scrollTo=sPuM8Xypjs-c
|
12 |
-
#https://github.com/dhawan98/Post-Processing-of-Image-Segmentation-using-CRF
|
13 |
|
14 |
-
def resize_sofa(img):
|
15 |
"""
|
16 |
-
This function adds padding to make the
|
17 |
-
It also returns the
|
|
|
18 |
Parameters:
|
19 |
img = original image
|
20 |
Return:
|
21 |
im1 = squared image
|
22 |
box = parameters to later crop the image to it original ratio
|
23 |
"""
|
24 |
-
width, height = img.size
|
25 |
-
idx = np.argmin([width,height])
|
26 |
-
newsize = (640, 640)
|
27 |
|
28 |
-
if idx==0:
|
29 |
img1 = Image.new(img.mode, (height, height), (255, 255, 255))
|
30 |
-
img1.paste(img, ((height-width)//2, 0))
|
31 |
-
box = (
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
else:
|
36 |
img1 = Image.new(img.mode, (width, width), (255, 255, 255))
|
37 |
-
img1.paste(img, (0, (width-height)//2))
|
38 |
-
box = (
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
im1 = img1.resize(newsize)
|
43 |
-
return im1,box
|
|
|
44 |
|
45 |
-
def resize_style(img):
|
46 |
"""
|
47 |
-
This function generates a zoomed out version of the style
|
|
|
48 |
Parameters:
|
49 |
img = image containing the style/pattern
|
50 |
Return:
|
51 |
dst = a zoomed-out and resized version of the pattern
|
52 |
"""
|
53 |
width, height = img.size
|
54 |
-
idx = np.argmin([width,height])
|
55 |
|
56 |
# Makes the image square by cropping
|
57 |
-
if idx==0:
|
58 |
-
top= (height-width)//2
|
59 |
-
bottom= height-(height-width)//2
|
60 |
left = 0
|
61 |
-
right= width
|
62 |
else:
|
63 |
-
left = (width-height)//2
|
64 |
-
right = width - (width-height)//2
|
65 |
top = 0
|
66 |
bottom = height
|
67 |
-
newsize = (640, 640)
|
68 |
im1 = img.crop((left, top, right, bottom))
|
69 |
|
70 |
# Constructs a zoomed-out version
|
71 |
copies = 8
|
72 |
-
resize = (newsize[0]//copies,newsize[1]//copies)
|
73 |
-
dst = Image.new(
|
74 |
im2 = im1.resize((resize))
|
75 |
for row in range(copies):
|
76 |
im2 = im2.transpose(Image.FLIP_LEFT_RIGHT)
|
77 |
for column in range(copies):
|
78 |
im2 = im2.transpose(Image.FLIP_TOP_BOTTOM)
|
79 |
-
dst.paste(im2, (resize[0]*row, resize[1]*column))
|
80 |
dst = dst.resize((newsize))
|
81 |
return dst
|
82 |
|
83 |
-
|
|
|
|
|
|
|
84 |
"""
|
85 |
Styles (all) the sofas in the image to the given style.
|
86 |
-
This function uses a transformer to combine the image with
|
87 |
-
to a generated mask of the sofas
|
|
|
88 |
Input:
|
89 |
input_img = image containing a sofa
|
90 |
style_img = image containing a style
|
|
|
91 |
Return:
|
92 |
new_sofa = image containing the styled sofa
|
93 |
"""
|
94 |
id = randint(0, 10)
|
95 |
-
print(
|
96 |
-
# preprocess input images to
|
97 |
-
input_img,style_img = Image.fromarray(
|
98 |
-
resized_img,box = resize_sofa(input_img)
|
99 |
resized_style = resize_style(style_img)
|
100 |
-
#resized_style.save('resized_style.jpg')
|
101 |
# generate mask for image
|
102 |
-
print(
|
103 |
mask = get_mask(resized_img)
|
104 |
-
#mask.save('mask.jpg')
|
105 |
# Created a styled sofa
|
106 |
-
print(
|
107 |
-
styled_sofa = create_styledSofa(resized_img,resized_style)
|
108 |
-
#styled_sofa.save('styled_sofa.jpg')
|
109 |
# postprocess the final image
|
110 |
-
print(
|
111 |
-
new_sofa = replace_sofa(resized_img,mask,styled_sofa)
|
112 |
new_sofa = new_sofa.crop(box)
|
113 |
-
print(
|
114 |
return new_sofa
|
115 |
|
|
|
116 |
demo = gr.Interface(
|
117 |
style_sofa,
|
118 |
-
inputs
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
[
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
title="π Style your sofa π ",
|
126 |
description="Customize your sofa to your wildest dreams π!\
|
127 |
-
\nProvide a picture of your sofa
|
128 |
-
|
|
|
129 |
theme="huggingface",
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
)
|
134 |
|
135 |
if __name__ == "__main__":
|
136 |
-
demo.launch()
|
137 |
-
|
138 |
-
|
|
|
1 |
+
from random import randint
|
2 |
+
from typing import Tuple
|
3 |
+
|
4 |
import gradio as gr
|
5 |
+
import numpy as np
|
|
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
+
from Segmentation.segmentation import get_mask, replace_sofa
|
9 |
+
from StyleTransfer.styleTransfer import create_styledSofa
|
10 |
|
|
|
|
|
|
|
11 |
|
12 |
+
def resize_sofa(img: Image.Image) -> Tuple[Image.Image, tuple]:
|
13 |
"""
|
14 |
+
This function adds padding to make the original image square
|
15 |
+
and 640by640. It also returns the original ratio of the image,
|
16 |
+
such that it can be reverted later.
|
17 |
Parameters:
|
18 |
img = original image
|
19 |
Return:
|
20 |
im1 = squared image
|
21 |
box = parameters to later crop the image to it original ratio
|
22 |
"""
|
23 |
+
width, height = img.size
|
24 |
+
idx = np.argmin([width, height])
|
25 |
+
newsize = (640, 640) # parameters from test script
|
26 |
|
27 |
+
if idx == 0:
|
28 |
img1 = Image.new(img.mode, (height, height), (255, 255, 255))
|
29 |
+
img1.paste(img, ((height - width) // 2, 0))
|
30 |
+
box = (
|
31 |
+
newsize[0] * (1 - width / height) // 2,
|
32 |
+
0,
|
33 |
+
newsize[0] - newsize[0] * (1 - width / height) // 2,
|
34 |
+
newsize[1],
|
35 |
+
)
|
36 |
else:
|
37 |
img1 = Image.new(img.mode, (width, width), (255, 255, 255))
|
38 |
+
img1.paste(img, (0, (width - height) // 2))
|
39 |
+
box = (
|
40 |
+
0,
|
41 |
+
newsize[1] * (1 - height / width) // 2,
|
42 |
+
newsize[0],
|
43 |
+
newsize[1] - newsize[1] * (1 - height / width) // 2,
|
44 |
+
)
|
45 |
im1 = img1.resize(newsize)
|
46 |
+
return im1, box
|
47 |
+
|
48 |
|
49 |
+
def resize_style(img: Image.Image) -> Image.Image:
|
50 |
"""
|
51 |
+
This function generates a zoomed out version of the style
|
52 |
+
image and resizes it to a 640by640 square.
|
53 |
Parameters:
|
54 |
img = image containing the style/pattern
|
55 |
Return:
|
56 |
dst = a zoomed-out and resized version of the pattern
|
57 |
"""
|
58 |
width, height = img.size
|
59 |
+
idx = np.argmin([width, height])
|
60 |
|
61 |
# Makes the image square by cropping
|
62 |
+
if idx == 0:
|
63 |
+
top = (height - width) // 2
|
64 |
+
bottom = height - (height - width) // 2
|
65 |
left = 0
|
66 |
+
right = width
|
67 |
else:
|
68 |
+
left = (width - height) // 2
|
69 |
+
right = width - (width - height) // 2
|
70 |
top = 0
|
71 |
bottom = height
|
72 |
+
newsize = (640, 640) # parameters from test script
|
73 |
im1 = img.crop((left, top, right, bottom))
|
74 |
|
75 |
# Constructs a zoomed-out version
|
76 |
copies = 8
|
77 |
+
resize = (newsize[0] // copies, newsize[1] // copies)
|
78 |
+
dst = Image.new("RGB", (resize[0] * copies, resize[1] * copies))
|
79 |
im2 = im1.resize((resize))
|
80 |
for row in range(copies):
|
81 |
im2 = im2.transpose(Image.FLIP_LEFT_RIGHT)
|
82 |
for column in range(copies):
|
83 |
im2 = im2.transpose(Image.FLIP_TOP_BOTTOM)
|
84 |
+
dst.paste(im2, (resize[0] * row, resize[1] * column))
|
85 |
dst = dst.resize((newsize))
|
86 |
return dst
|
87 |
|
88 |
+
|
89 |
+
def style_sofa(
|
90 |
+
Input_image: np.ndarray, Style_image: np.ndarray, Choice_of_algorithm: str
|
91 |
+
) -> Image.Image:
|
92 |
"""
|
93 |
Styles (all) the sofas in the image to the given style.
|
94 |
+
This function uses a transformer to combine the image with
|
95 |
+
the desired style according to a generated mask of the sofas
|
96 |
+
in the image.
|
97 |
Input:
|
98 |
input_img = image containing a sofa
|
99 |
style_img = image containing a style
|
100 |
+
choice = Style transfer algorithm to use
|
101 |
Return:
|
102 |
new_sofa = image containing the styled sofa
|
103 |
"""
|
104 |
id = randint(0, 10)
|
105 |
+
print("Starting job ", id)
|
106 |
+
# preprocess input images to fit requirements of the segmentation model
|
107 |
+
input_img, style_img = Image.fromarray(Input_image), Image.fromarray(Style_image)
|
108 |
+
resized_img, box = resize_sofa(input_img)
|
109 |
resized_style = resize_style(style_img)
|
110 |
+
# resized_style.save('resized_style.jpg')
|
111 |
# generate mask for image
|
112 |
+
print("generating mask...")
|
113 |
mask = get_mask(resized_img)
|
114 |
+
# mask.save('mask.jpg')
|
115 |
# Created a styled sofa
|
116 |
+
print("Styling sofa...")
|
117 |
+
styled_sofa = create_styledSofa(resized_img, resized_style, Choice_of_algorithm)
|
118 |
+
# styled_sofa.save('styled_sofa.jpg')
|
119 |
# postprocess the final image
|
120 |
+
print("Replacing sofa...")
|
121 |
+
new_sofa = replace_sofa(resized_img, mask, styled_sofa)
|
122 |
new_sofa = new_sofa.crop(box)
|
123 |
+
print("Finishing job", id)
|
124 |
return new_sofa
|
125 |
|
126 |
+
|
127 |
demo = gr.Interface(
|
128 |
style_sofa,
|
129 |
+
inputs=[
|
130 |
+
gr.inputs.Image(),
|
131 |
+
gr.inputs.Image(),
|
132 |
+
gr.inputs.Radio(
|
133 |
+
["Style Transformer", "Style FAST", "Style Projection"],
|
134 |
+
default="Style FAST",
|
135 |
+
),
|
136 |
+
],
|
137 |
+
outputs="image",
|
138 |
+
examples=[
|
139 |
+
[
|
140 |
+
"figures/sofa_example1.jpg",
|
141 |
+
"figures/style_example1.jpg",
|
142 |
+
"Style Transformer",
|
143 |
+
],
|
144 |
+
[
|
145 |
+
"figures/sofa_example3.jpg",
|
146 |
+
"figures/style_example10.jpg",
|
147 |
+
"Style FAST",
|
148 |
+
],
|
149 |
+
[
|
150 |
+
"figures/sofa_example2.jpg",
|
151 |
+
"figures/style_example6.jpg",
|
152 |
+
"Style Projection",
|
153 |
+
],
|
154 |
+
],
|
155 |
title="π Style your sofa π ",
|
156 |
description="Customize your sofa to your wildest dreams π!\
|
157 |
+
\nProvide a picture of your sofa, a desired pattern\
|
158 |
+
and (optionally) choose one of the algorithms.\
|
159 |
+
\nOr just pick one of the examples below. β¬",
|
160 |
theme="huggingface",
|
161 |
+
article="**References**\n\n"
|
162 |
+
"<a href='https://tianchi.aliyun.com/specials/promotion/alibaba-3d-future' \
|
163 |
+
target='_blank'>\
|
164 |
+
1. The data that was used to train the segmentation model. \
|
165 |
+
</a> \n"
|
166 |
+
"<a href='https://github.com/qubvel/segmentation_models' \
|
167 |
+
target='_blank'> \
|
168 |
+
2. Github repository used to train a segmentation model with transfer. \
|
169 |
+
learning.\
|
170 |
+
</a> \n"
|
171 |
+
"<a href='https://github.com/diyiiyiii/StyTR-2' \
|
172 |
+
target='_blank'> \
|
173 |
+
3. The github repository that is used for the style transformer. \
|
174 |
+
</a> \n"
|
175 |
+
"<a href='https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2' \
|
176 |
+
target='_blank'> \
|
177 |
+
4. A tensorflow model for fast arbitrary image style transfer. \
|
178 |
+
</a> \n"
|
179 |
+
"<a href='https://github.com/PaddlePaddle/PaddleHub/tree/release/v2.2/modules/image/Image_gan/style_transfer/stylepro_artistic' \
|
180 |
+
target='_blank'> \
|
181 |
+
5. A paddleHub model for parameter free style transfer. \
|
182 |
+
</a> \n",
|
183 |
)
|
184 |
|
185 |
if __name__ == "__main__":
|
186 |
+
demo.launch(cache_examples=True)
|
|
|
|
figures/sofa_example2.jpg
ADDED
![]() |
figures/sofa_example3.jpg
ADDED
![]() |
figures/style_example10.jpg
ADDED
![]() |
figures/style_example11.jpg
ADDED
![]() |
figures/style_example6.jpg
ADDED
![]() |
figures/style_example7.jpg
ADDED
![]() |
figures/style_example8.jpg
ADDED
![]() |
figures/style_example9.jpg
ADDED
![]() |
gradio_cached_examples/log.csv
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'output'
|
2 |
+
'output/0.png'
|
3 |
+
'output/1.png'
|
4 |
+
'output/2.png'
|
gradio_cached_examples/output/0.png
ADDED
![]() |
gradio_cached_examples/output/1.png
ADDED
![]() |
gradio_cached_examples/output/2.png
ADDED
![]() |