Spaces:
Build error
Build error
Sophie98 commited on
Commit Β·
79c6687
1
Parent(s): 91c0967
documentation
Browse files- app.py +5 -6
- segmentation.py +20 -6
- styleTransfer.py +0 -1
app.py
CHANGED
|
@@ -3,7 +3,6 @@ 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 |
-
import cv2
|
| 7 |
|
| 8 |
def style_sofa(input_img: np.ndarray, style_img: np.ndarray):
|
| 9 |
"""
|
|
@@ -21,11 +20,9 @@ def style_sofa(input_img: np.ndarray, style_img: np.ndarray):
|
|
| 21 |
resized_img,box = resize_sofa(input_img)
|
| 22 |
resized_style = resize_style(style_img)
|
| 23 |
# generate mask for image
|
| 24 |
-
print('starting mask')
|
| 25 |
mask = get_mask(resized_img)
|
| 26 |
-
print('got mask')
|
| 27 |
styled_sofa = create_styledSofa(resized_img,resized_style)
|
| 28 |
-
|
| 29 |
new_sofa = replace_sofa(resized_img,mask,styled_sofa)
|
| 30 |
new_sofa = Image.fromarray(new_sofa).crop(box)
|
| 31 |
return new_sofa
|
|
@@ -44,8 +41,10 @@ demo = gr.Interface(
|
|
| 44 |
['sofa_example1.jpg','style_example4.jpg'],
|
| 45 |
['sofa_example1.jpg','style_example5.jpg'],
|
| 46 |
],
|
| 47 |
-
title="Style your sofa",
|
| 48 |
-
description="
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
|
|
|
| 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 |
"""
|
|
|
|
| 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 |
+
# postprocess the final image
|
| 26 |
new_sofa = replace_sofa(resized_img,mask,styled_sofa)
|
| 27 |
new_sofa = Image.fromarray(new_sofa).crop(box)
|
| 28 |
return new_sofa
|
|
|
|
| 41 |
['sofa_example1.jpg','style_example4.jpg'],
|
| 42 |
['sofa_example1.jpg','style_example5.jpg'],
|
| 43 |
],
|
| 44 |
+
title="π Style your sofa π ",
|
| 45 |
+
description="Customize your sofa to your wildest dreams!\
|
| 46 |
+
\nProvide a picture of your sofa and a desired pattern\
|
| 47 |
+
\n or choose one of the examples below",
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
segmentation.py
CHANGED
|
@@ -8,6 +8,15 @@ from PIL import Image
|
|
| 8 |
import segmentation_models as sm
|
| 9 |
|
| 10 |
def get_mask(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
model_path = "model_checkpoint.h5"
|
| 12 |
CLASSES = ['sofa']
|
| 13 |
BACKBONE = 'resnet50'
|
|
@@ -31,14 +40,11 @@ def get_mask(image):
|
|
| 31 |
# total_loss = sm.losses.binary_focal_dice_loss # or sm.losses.categorical_focal_dice_loss
|
| 32 |
metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]
|
| 33 |
# compile keras model with defined optimozer, loss and metrics
|
| 34 |
-
print('compiling')
|
| 35 |
model.compile(optim, total_loss, metrics)
|
| 36 |
|
| 37 |
-
print('loading model weights')
|
| 38 |
#load model
|
| 39 |
model.load_weights(model_path)
|
| 40 |
|
| 41 |
-
print('processing image')
|
| 42 |
test_img = np.array(image)#cv2.imread(path, cv2.IMREAD_COLOR)
|
| 43 |
test_img = cv2.resize(test_img, (640, 640))
|
| 44 |
test_img = cv2.cvtColor(test_img, cv2.COLOR_RGB2BGR)
|
|
@@ -51,9 +57,17 @@ def get_mask(image):
|
|
| 51 |
return np.array(mask)
|
| 52 |
|
| 53 |
def replace_sofa(image,mask,styled_sofa):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
image = np.array(image)
|
| 58 |
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 59 |
styled_sofa = cv2.cvtColor(styled_sofa, cv2.COLOR_BGR2RGB)
|
|
|
|
| 8 |
import segmentation_models as sm
|
| 9 |
|
| 10 |
def get_mask(image):
|
| 11 |
+
"""
|
| 12 |
+
This function generates a mask of the image that highlights all the sofas in the image.
|
| 13 |
+
This uses a pre-trained Unet model with a resnet50 backbone.
|
| 14 |
+
Remark: The model was trained on 640by640 images and it is therefore best that the image has the same size.
|
| 15 |
+
Parameters:
|
| 16 |
+
image = original image
|
| 17 |
+
Return:
|
| 18 |
+
mask = corresponding maks of the image
|
| 19 |
+
"""
|
| 20 |
model_path = "model_checkpoint.h5"
|
| 21 |
CLASSES = ['sofa']
|
| 22 |
BACKBONE = 'resnet50'
|
|
|
|
| 40 |
# total_loss = sm.losses.binary_focal_dice_loss # or sm.losses.categorical_focal_dice_loss
|
| 41 |
metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]
|
| 42 |
# compile keras model with defined optimozer, loss and metrics
|
|
|
|
| 43 |
model.compile(optim, total_loss, metrics)
|
| 44 |
|
|
|
|
| 45 |
#load model
|
| 46 |
model.load_weights(model_path)
|
| 47 |
|
|
|
|
| 48 |
test_img = np.array(image)#cv2.imread(path, cv2.IMREAD_COLOR)
|
| 49 |
test_img = cv2.resize(test_img, (640, 640))
|
| 50 |
test_img = cv2.cvtColor(test_img, cv2.COLOR_RGB2BGR)
|
|
|
|
| 57 |
return np.array(mask)
|
| 58 |
|
| 59 |
def replace_sofa(image,mask,styled_sofa):
|
| 60 |
+
"""
|
| 61 |
+
This function replaces the original sofa in the image by the new styled sofa according
|
| 62 |
+
to the mask.
|
| 63 |
+
Remark: All images should have the same size.
|
| 64 |
+
Input:
|
| 65 |
+
image = Original image
|
| 66 |
+
mask = Generated masks highlighting the sofas in the image
|
| 67 |
+
styled_sofa = Styled image
|
| 68 |
+
Return:
|
| 69 |
+
new_image = Image containing the styled sofa
|
| 70 |
+
"""
|
| 71 |
image = np.array(image)
|
| 72 |
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 73 |
styled_sofa = cv2.cvtColor(styled_sofa, cv2.COLOR_BGR2RGB)
|
styleTransfer.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import torch
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
import os
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
import numpy as np
|
| 3 |
import os
|