Spaces:
Build error
Build error
Sophie98
commited on
Commit
β’
2578b1e
1
Parent(s):
71893b8
fix resize
Browse files- app.py +2 -2
- segmentation.py +0 -4
- sofaApp.py +0 -50
- styleTransfer.py +11 -5
- test.py +0 -1
app.py
CHANGED
@@ -17,13 +17,13 @@ def style_sofa(input_img: np.ndarray, style_img: np.ndarray):
|
|
17 |
"""
|
18 |
|
19 |
# preprocess input images to be (640,640) squares to fit requirements of the segmentation model
|
20 |
-
resized_img = resize_sofa(input_img)
|
21 |
resized_style = resize_style(style_img)
|
22 |
# generate mask for image
|
23 |
mask = get_mask(resized_img)
|
24 |
styled_sofa = create_styledSofa(resized_img,resized_style)
|
25 |
-
print('Created styled image')
|
26 |
new_sofa = replace_sofa(resized_img,mask,styled_sofa)
|
|
|
27 |
return new_sofa
|
28 |
|
29 |
image = gr.inputs.Image()
|
|
|
17 |
"""
|
18 |
|
19 |
# preprocess input images to be (640,640) squares to fit requirements of the segmentation model
|
20 |
+
resized_img,box = resize_sofa(input_img)
|
21 |
resized_style = resize_style(style_img)
|
22 |
# generate mask for image
|
23 |
mask = get_mask(resized_img)
|
24 |
styled_sofa = create_styledSofa(resized_img,resized_style)
|
|
|
25 |
new_sofa = replace_sofa(resized_img,mask,styled_sofa)
|
26 |
+
new_sofa = Image.fromarray(new_sofa).crop(box)
|
27 |
return new_sofa
|
28 |
|
29 |
image = gr.inputs.Image()
|
segmentation.py
CHANGED
@@ -51,18 +51,14 @@ def replace_sofa(image,mask,styled_sofa):
|
|
51 |
# print(mask.shape)
|
52 |
# mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
|
53 |
# print(mask.shape)
|
54 |
-
print('started function')
|
55 |
image = np.array(image)
|
56 |
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
57 |
styled_sofa = cv2.cvtColor(styled_sofa, cv2.COLOR_BGR2RGB)
|
58 |
|
59 |
-
print('reading masks')
|
60 |
_, mask = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY)
|
61 |
mask_inv = cv2.bitwise_not(mask)
|
62 |
-
print('creating parts')
|
63 |
image_bg = cv2.bitwise_and(image,image,mask = mask_inv)
|
64 |
sofa_fg = cv2.bitwise_and(styled_sofa,styled_sofa,mask = mask)
|
65 |
-
print('combining parts')
|
66 |
new_image = cv2.add(image_bg,sofa_fg)
|
67 |
return new_image
|
68 |
|
|
|
51 |
# print(mask.shape)
|
52 |
# mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
|
53 |
# print(mask.shape)
|
|
|
54 |
image = np.array(image)
|
55 |
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
56 |
styled_sofa = cv2.cvtColor(styled_sofa, cv2.COLOR_BGR2RGB)
|
57 |
|
|
|
58 |
_, mask = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY)
|
59 |
mask_inv = cv2.bitwise_not(mask)
|
|
|
60 |
image_bg = cv2.bitwise_and(image,image,mask = mask_inv)
|
61 |
sofa_fg = cv2.bitwise_and(styled_sofa,styled_sofa,mask = mask)
|
|
|
62 |
new_image = cv2.add(image_bg,sofa_fg)
|
63 |
return new_image
|
64 |
|
sofaApp.py
DELETED
@@ -1,50 +0,0 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import gradio as gr
|
3 |
-
from segmentation import get_mask,replace_sofa
|
4 |
-
from styleTransfer import resize_sofa,resize_style,create_styledSofa
|
5 |
-
from PIL import Image
|
6 |
-
|
7 |
-
def style_sofa(input_img: np.ndarray, style_img: np.ndarray):
|
8 |
-
"""
|
9 |
-
Styles (all) the sofas in the image to the given style.
|
10 |
-
This function uses a transformer to combine the image with the desired style according
|
11 |
-
to a generated mask of the sofas in the image.
|
12 |
-
Input:
|
13 |
-
input_img = image containing a sofa
|
14 |
-
style_img = image containing a style
|
15 |
-
Return:
|
16 |
-
new_sofa = image containing the styled sofa
|
17 |
-
"""
|
18 |
-
|
19 |
-
# preprocess input images to be (640,640) squares to fit requirements of the segmentation model
|
20 |
-
resized_img = resize_sofa(input_img)
|
21 |
-
resized_style = resize_style(style_img)
|
22 |
-
# generate mask for image
|
23 |
-
mask = get_mask(resized_img)
|
24 |
-
styled_sofa = create_styledSofa(resized_img,resized_style)
|
25 |
-
new_sofa = replace_sofa(resized_img,mask,styled_sofa)
|
26 |
-
return new_sofa
|
27 |
-
|
28 |
-
image = gr.inputs.Image()
|
29 |
-
style = gr.inputs.Image()
|
30 |
-
|
31 |
-
demo = gr.Interface(
|
32 |
-
style_sofa,
|
33 |
-
[image,style],
|
34 |
-
'image',
|
35 |
-
examples=[
|
36 |
-
['sofa_example1.jpg','style_example1.jpg'],
|
37 |
-
['sofa_example1.jpg','style_example2.jpg'],
|
38 |
-
['sofa_example1.jpg','style_example3.jpg'],
|
39 |
-
['sofa_example1.jpg','style_example4.jpg'],
|
40 |
-
['sofa_example1.jpg','style_example5.jpg'],
|
41 |
-
],
|
42 |
-
title="Style your sofa",
|
43 |
-
description="π Customize your sofa to your wildest dreams! π",
|
44 |
-
)
|
45 |
-
|
46 |
-
if __name__ == "__main__":
|
47 |
-
demo.launch(share=True)
|
48 |
-
|
49 |
-
|
50 |
-
#https://github.com/dhawan98/Post-Processing-of-Image-Segmentation-using-CRF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styleTransfer.py
CHANGED
@@ -8,17 +8,19 @@ def resize_sofa(img):
|
|
8 |
img = Image.fromarray(img)
|
9 |
width, height = img.size
|
10 |
idx = np.argmin([width,height])
|
|
|
11 |
|
12 |
if idx==0:
|
13 |
img1 = Image.new(img.mode, (height, height), (255, 255, 255))
|
14 |
img1.paste(img, ((height-width)//2, 0))
|
|
|
15 |
else:
|
16 |
img1 = Image.new(img.mode, (width, width), (255, 255, 255))
|
17 |
img1.paste(img, (0, (width-height)//2))
|
|
|
18 |
|
19 |
-
newsize = (640, 640) # parameters from test script
|
20 |
im1 = img1.resize(newsize)
|
21 |
-
return im1
|
22 |
|
23 |
def resize_style(img):
|
24 |
#img = Image.open(path)#"../style5.jpg")
|
@@ -68,6 +70,10 @@ def create_styledSofa(sofa,style):
|
|
68 |
|
69 |
return styled_sofa
|
70 |
|
71 |
-
# image = Image.open('
|
72 |
-
# image
|
73 |
-
# image =
|
|
|
|
|
|
|
|
|
|
8 |
img = Image.fromarray(img)
|
9 |
width, height = img.size
|
10 |
idx = np.argmin([width,height])
|
11 |
+
newsize = (640, 640) # parameters from test script
|
12 |
|
13 |
if idx==0:
|
14 |
img1 = Image.new(img.mode, (height, height), (255, 255, 255))
|
15 |
img1.paste(img, ((height-width)//2, 0))
|
16 |
+
box = ((height-width)//2*newsize[0]/width, 0, newsize[0] - (height-width)//2*newsize[0]/width, newsize[1])
|
17 |
else:
|
18 |
img1 = Image.new(img.mode, (width, width), (255, 255, 255))
|
19 |
img1.paste(img, (0, (width-height)//2))
|
20 |
+
box = (0, (height-width)//2*newsize[1]/height, newsize[0], newsize[1]-(height-width)//2*newsize[1]/height)
|
21 |
|
|
|
22 |
im1 = img1.resize(newsize)
|
23 |
+
return im1,box
|
24 |
|
25 |
def resize_style(img):
|
26 |
#img = Image.open(path)#"../style5.jpg")
|
|
|
70 |
|
71 |
return styled_sofa
|
72 |
|
73 |
+
# image = Image.open('sofa_office.jpg')
|
74 |
+
# image.show()
|
75 |
+
# image = np.array(image)
|
76 |
+
# image,box = resize_sofa(image)
|
77 |
+
# image = image.crop(box)
|
78 |
+
# print(box)
|
79 |
+
# image.show()
|
test.py
CHANGED
@@ -148,7 +148,6 @@ style_tf = test_transform(style_size, crop)
|
|
148 |
|
149 |
for content_path in content_paths:
|
150 |
for style_path in style_paths:
|
151 |
-
print(content_path)
|
152 |
|
153 |
|
154 |
content_tf1 = content_transform()
|
|
|
148 |
|
149 |
for content_path in content_paths:
|
150 |
for style_path in style_paths:
|
|
|
151 |
|
152 |
|
153 |
content_tf1 = content_transform()
|