Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image
|
|
|
|
|
3 |
import base64
|
4 |
import spaces
|
5 |
from loadimg import load_img
|
@@ -41,6 +43,35 @@ transform_image = transforms.Compose([
|
|
41 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
42 |
])
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
def save_image(img):
|
45 |
unique_name = str(uuid.uuid4()) + ".png"
|
46 |
img.save(unique_name)
|
@@ -100,7 +131,9 @@ def process_image(input_image):
|
|
100 |
try:
|
101 |
clothes = ["Upper-clothes", "Skirt", "Pants", "Dress"]
|
102 |
results = detect_and_segment_persons(input_image, clothes)
|
103 |
-
|
|
|
|
|
104 |
except Exception as e:
|
105 |
return f"Error occurred: {e}"
|
106 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image,ImageFilter
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
import base64
|
6 |
import spaces
|
7 |
from loadimg import load_img
|
|
|
43 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
44 |
])
|
45 |
|
46 |
+
def refine_edges(image):
|
47 |
+
"""
|
48 |
+
Affine les contours de l'image en sortie en utilisant un filtre de détection de contours et du lissage.
|
49 |
+
"""
|
50 |
+
# Convertir l'image PIL en format numpy pour OpenCV
|
51 |
+
img_np = np.array(image)
|
52 |
+
|
53 |
+
# Convertir en niveaux de gris pour traiter les contours
|
54 |
+
gray = cv2.cvtColor(img_np, cv2.COLOR_RGBA2GRAY)
|
55 |
+
|
56 |
+
# Détection des bords avec Canny
|
57 |
+
edges = cv2.Canny(gray, threshold1=50, threshold2=150)
|
58 |
+
|
59 |
+
# Dilater les bords pour renforcer les contours
|
60 |
+
kernel = np.ones((3, 3), np.uint8)
|
61 |
+
edges_dilated = cv2.dilate(edges, kernel, iterations=1)
|
62 |
+
|
63 |
+
# Lisser les bords (anti-aliasing)
|
64 |
+
blurred = cv2.GaussianBlur(edges_dilated, (5, 5), 0)
|
65 |
+
|
66 |
+
# Ajouter les bords comme masque alpha
|
67 |
+
alpha = Image.fromarray(blurred).convert("L")
|
68 |
+
image.putalpha(alpha)
|
69 |
+
|
70 |
+
# Filtrage supplémentaire pour améliorer l'esthétique
|
71 |
+
refined_image = image.filter(ImageFilter.SMOOTH_MORE)
|
72 |
+
|
73 |
+
return refined_image
|
74 |
+
|
75 |
def save_image(img):
|
76 |
unique_name = str(uuid.uuid4()) + ".png"
|
77 |
img.save(unique_name)
|
|
|
131 |
try:
|
132 |
clothes = ["Upper-clothes", "Skirt", "Pants", "Dress"]
|
133 |
results = detect_and_segment_persons(input_image, clothes)
|
134 |
+
refined_results = [refine_edges(image) for image in results]
|
135 |
+
|
136 |
+
return refined_results
|
137 |
except Exception as e:
|
138 |
return f"Error occurred: {e}"
|
139 |
|