Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +48 -53
- fashionlook1.png +0 -0
- model.py +32 -0
app.py
CHANGED
|
@@ -1,65 +1,60 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def create_interface():
|
| 12 |
-
with gr.Blocks() as interface:
|
| 13 |
-
gr.HTML(
|
| 14 |
-
"""<style>
|
| 15 |
-
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600&family=Roboto:wght@400&display=swap');
|
| 16 |
-
body { background-color: #2c2c2c; font-family: 'Roboto', sans-serif; }
|
| 17 |
-
h1 { font-family: 'Poppins', sans-serif; color: #ff9800; text-align: center; font-size: 2.5em; margin-bottom: 20px; }
|
| 18 |
-
h3 { font-family: 'Roboto', sans-serif; color: #dddddd; text-align: center; margin-bottom: 10px; }
|
| 19 |
-
h5 { font-family: 'Roboto', sans-serif; color: #dddddd; text-align: center; margin-bottom: 10px; }
|
| 20 |
-
.segment-btn {
|
| 21 |
-
background-color: #ff9800;
|
| 22 |
-
border-radius: 8px;
|
| 23 |
-
padding: 10px 20px;
|
| 24 |
-
color: white;
|
| 25 |
-
transition: background-color 0.3s ease, transform 0.2s ease;
|
| 26 |
-
}
|
| 27 |
-
.segment-btn:hover {
|
| 28 |
-
background-color: #e67e00;
|
| 29 |
-
transform: scale(1.05);
|
| 30 |
-
}
|
| 31 |
-
.logo {
|
| 32 |
-
display: block;
|
| 33 |
-
margin: 0 auto;
|
| 34 |
-
width: 150px;
|
| 35 |
-
height: auto;
|
| 36 |
-
padding: 20px 0;
|
| 37 |
-
}
|
| 38 |
-
</style>"""
|
| 39 |
-
)
|
| 40 |
-
#logo_path = os.path.join(os.getcwd(), 'fashionlookl1_2.png')
|
| 41 |
-
gr.Markdown(f"<img src='fashionlookl1_2.png' class='logo' alt='Logo'>")
|
| 42 |
-
gr.Markdown("<h1>FashionLook - Segment Clothes</h1>")
|
| 43 |
-
gr.Markdown("<h3 style='text-align: center;'>" "Upload an image and let our model detect and segment clothes such as shirts, pants, skirts...""</h3>")
|
| 44 |
-
with gr.Row():
|
| 45 |
-
with gr.Column(scale=1):
|
| 46 |
-
gr.Markdown("<h5>Upload your image</h5>")
|
| 47 |
-
image_input = gr.Image(type='pil', label="Upload Image")
|
| 48 |
-
with gr.Row():
|
| 49 |
-
segment_button = gr.Button("Run Segmentation", elem_id="segment-btn")
|
| 50 |
-
with gr.Column(scale=1):
|
| 51 |
-
gr.Markdown("<h5>Segmented Image with Overlay</h5>")
|
| 52 |
-
segmented_image_output = gr.Image(type="pil", label="Segmented Image", interactive=False)
|
| 53 |
-
|
| 54 |
# Actions liées aux inputs/outputs
|
| 55 |
segment_button.click(
|
| 56 |
-
fn=
|
| 57 |
inputs=[image_input],
|
| 58 |
outputs=[segmented_image_output]
|
| 59 |
)
|
| 60 |
-
|
| 61 |
-
return interface
|
| 62 |
|
| 63 |
# Lancer l'interface
|
| 64 |
-
|
| 65 |
-
interface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
from model import kmeans, mean_shift
|
| 5 |
|
| 6 |
+
with gr.Blocks() as demo:
|
| 7 |
+
gr.HTML(
|
| 8 |
+
"""<style>
|
| 9 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600&family=Roboto:wght@400&display=swap');
|
| 10 |
+
|
| 11 |
+
body { background-color: #f0f0f0; font-family: 'Roboto', sans-serif; }
|
| 12 |
+
h1 { font-family: 'Poppins', sans-serif; color: #4CAF50; text-align: center; }
|
| 13 |
+
h3 { font-family: 'Roboto', sans-serif; color: #333; text-align: center; }
|
| 14 |
+
.segment-btn {
|
| 15 |
+
background-color: #4CAF50;
|
| 16 |
+
border-radius: 8px;
|
| 17 |
+
padding: 10px 20px;
|
| 18 |
+
color: white;
|
| 19 |
+
transition: background-color 0.3s ease, transform 0.2s ease;
|
| 20 |
+
}
|
| 21 |
+
.segment-btn:hover {
|
| 22 |
+
background-color: #45a049;
|
| 23 |
+
transform: scale(1.05);
|
| 24 |
+
}
|
| 25 |
+
.logo {
|
| 26 |
+
display: block;
|
| 27 |
+
margin: 0 auto;
|
| 28 |
+
width: 150px;
|
| 29 |
+
height: auto;
|
| 30 |
+
padding: 20px 0;
|
| 31 |
+
}
|
| 32 |
+
</style>"""
|
| 33 |
+
)
|
| 34 |
+
#logo_path = os.path.join(os.getcwd(), 'assets/img/fashionlook1.png')
|
| 35 |
+
gr.Markdown("""
|
| 36 |
+
<div style="text-align: center">
|
| 37 |
+
<img src='./fashionlook1.png' class='logo' alt='Logo' style='max-width: 300px'>
|
| 38 |
+
</div>
|
| 39 |
+
""")
|
| 40 |
+
gr.Markdown("<h1>FashionLook - Segment Clothes</h1>")
|
| 41 |
+
gr.Markdown("<h3 style='text-align: center;'>" "Upload an image and let our model detect and segment clothes such as shirts, pants, skirts...""</h3>")
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column(scale=1):
|
| 44 |
+
gr.Markdown("<h5>Upload your image</h5>")
|
| 45 |
+
image_input = gr.Image(type='pil', label="Upload Image")
|
| 46 |
+
with gr.Row():
|
| 47 |
+
segment_button = gr.Button("Run Segmentation", elem_id="segment-btn")
|
| 48 |
+
with gr.Column(scale=1):
|
| 49 |
+
gr.Markdown("<h5>Segmented Image with Overlay</h5>")
|
| 50 |
+
segmented_image_output = gr.Image(type="pil", label="Segmented Image", interactive=False)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Actions liées aux inputs/outputs
|
| 53 |
segment_button.click(
|
| 54 |
+
fn=kmeans,
|
| 55 |
inputs=[image_input],
|
| 56 |
outputs=[segmented_image_output]
|
| 57 |
)
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Lancer l'interface
|
| 60 |
+
demo.launch(share=False)
|
|
|
fashionlook1.png
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
|
| 4 |
+
def kmeans(image, k=5, alpha=0.5):
|
| 5 |
+
image = np.array(image)
|
| 6 |
+
pixel_vals = image.reshape((-1,3))
|
| 7 |
+
pixel_vals = np.float32(pixel_vals)
|
| 8 |
+
|
| 9 |
+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.85)
|
| 10 |
+
retval, labels, centers = cv2.kmeans(pixel_vals, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
|
| 11 |
+
|
| 12 |
+
centers = np.uint8(centers)
|
| 13 |
+
# Attribuer des couleurs données à nos clusters
|
| 14 |
+
cluster_colors = np.random.randint(0, 255, size=(k, 3), dtype=np.uint8)
|
| 15 |
+
segmented_data = cluster_colors[labels.flatten()]
|
| 16 |
+
segmented_image = segmented_data.reshape((image.shape))
|
| 17 |
+
|
| 18 |
+
return segmented_image
|
| 19 |
+
|
| 20 |
+
def mean_shift(image, spatial_radius=5, color_radius=60, max_iter=4):
|
| 21 |
+
image = np.array(image)
|
| 22 |
+
mean_shift_result = cv2.pyrMeanShiftFiltering(image, sp=spatial_radius, sr=color_radius, maxLevel=max_iter)
|
| 23 |
+
|
| 24 |
+
flat_image = mean_shift_result.reshape((-1, 3))
|
| 25 |
+
|
| 26 |
+
unique_colors, labels = np.unique(flat_image, axis=0, return_inverse=True)
|
| 27 |
+
|
| 28 |
+
cluster_colors = np.random.randint(0, 255, size=(len(unique_colors), 3), dtype=np.uint8)
|
| 29 |
+
segmented_image = cluster_colors[labels].reshape(image.shape)
|
| 30 |
+
|
| 31 |
+
return segmented_image
|
| 32 |
+
|