Spaces:
Runtime error
Runtime error
gdTharusha
commited on
Commit
•
6425c58
1
Parent(s):
b9a1c39
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,22 @@ import vtracer
|
|
7 |
from skimage import feature, filters, morphology
|
8 |
import cv2
|
9 |
from rembg import remove
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg):
|
12 |
"""Advanced preprocessing of the image before vectorization."""
|
@@ -44,13 +60,10 @@ def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail
|
|
44 |
|
45 |
if enhance_with_ai:
|
46 |
image_np = np.array(image)
|
47 |
-
# AI-based enhancement for smoothing edges
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
if remove_bg:
|
52 |
-
image_np = np.array(image)
|
53 |
-
image_np = remove(image_np)
|
54 |
image = Image.fromarray(image_np)
|
55 |
|
56 |
except Exception as e:
|
@@ -62,12 +75,17 @@ def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail
|
|
62 |
def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
|
63 |
color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
|
64 |
corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision,
|
65 |
-
enhance_with_ai, remove_bg):
|
66 |
"""Convert an image to SVG using vtracer with customizable and advanced parameters."""
|
67 |
-
|
68 |
# Preprocess the image with additional detail level settings
|
69 |
image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg)
|
70 |
|
|
|
|
|
|
|
|
|
|
|
71 |
# Convert Gradio image to bytes for vtracer compatibility
|
72 |
img_byte_array = io.BytesIO()
|
73 |
image.save(img_byte_array, format='PNG')
|
@@ -117,6 +135,7 @@ with iface:
|
|
117 |
noise_reduction_input = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="Noise Reduction")
|
118 |
enhance_with_ai_input = gr.Checkbox(label="AI Edge Enhance", value=False)
|
119 |
remove_bg_input = gr.Checkbox(label="Remove Background", value=False)
|
|
|
120 |
|
121 |
with gr.Row():
|
122 |
detail_level_input = gr.Slider(minimum=0, maximum=10, value=5, step=1, label="Detail Level")
|
@@ -150,11 +169,12 @@ with iface:
|
|
150 |
fn=convert_image,
|
151 |
inputs=[
|
152 |
image_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
|
153 |
-
color_mode_input, hierarchical_input, mode_input, filter_speckle_input,
|
154 |
-
|
155 |
-
splice_threshold_input, path_precision_input, enhance_with_ai_input, remove_bg_input
|
156 |
],
|
157 |
outputs=[svg_output, download_output]
|
158 |
)
|
159 |
|
|
|
160 |
iface.launch()
|
|
|
7 |
from skimage import feature, filters, morphology
|
8 |
import cv2
|
9 |
from rembg import remove
|
10 |
+
from sklearn.cluster import KMeans
|
11 |
+
|
12 |
+
def quantize_colors(image, num_colors):
|
13 |
+
"""Reduce the number of colors in an image."""
|
14 |
+
try:
|
15 |
+
image_np = np.array(image)
|
16 |
+
h, w, c = image_np.shape
|
17 |
+
image_reshaped = image_np.reshape((-1, 3))
|
18 |
+
|
19 |
+
kmeans = KMeans(n_clusters=num_colors, random_state=42).fit(image_reshaped)
|
20 |
+
new_colors = kmeans.cluster_centers_[kmeans.labels_].reshape(h, w, 3).astype(np.uint8)
|
21 |
+
|
22 |
+
return Image.fromarray(new_colors)
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error during color quantization: {e}")
|
25 |
+
raise
|
26 |
|
27 |
def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg):
|
28 |
"""Advanced preprocessing of the image before vectorization."""
|
|
|
60 |
|
61 |
if enhance_with_ai:
|
62 |
image_np = np.array(image)
|
63 |
+
# AI-based enhancement for smoothing edges without background removal
|
64 |
+
# Optionally apply background removal only if remove_bg is checked
|
65 |
+
if remove_bg:
|
66 |
+
image_np = remove(image_np)
|
|
|
|
|
|
|
67 |
image = Image.fromarray(image_np)
|
68 |
|
69 |
except Exception as e:
|
|
|
75 |
def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
|
76 |
color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
|
77 |
corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision,
|
78 |
+
enhance_with_ai, remove_bg, upscale_factor):
|
79 |
"""Convert an image to SVG using vtracer with customizable and advanced parameters."""
|
80 |
+
|
81 |
# Preprocess the image with additional detail level settings
|
82 |
image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg)
|
83 |
|
84 |
+
# Upscale the image if needed
|
85 |
+
if upscale_factor > 1:
|
86 |
+
new_size = (int(image.width * upscale_factor), int(image.height * upscale_factor))
|
87 |
+
image = image.resize(new_size, Image.LANCZOS)
|
88 |
+
|
89 |
# Convert Gradio image to bytes for vtracer compatibility
|
90 |
img_byte_array = io.BytesIO()
|
91 |
image.save(img_byte_array, format='PNG')
|
|
|
135 |
noise_reduction_input = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="Noise Reduction")
|
136 |
enhance_with_ai_input = gr.Checkbox(label="AI Edge Enhance", value=False)
|
137 |
remove_bg_input = gr.Checkbox(label="Remove Background", value=False)
|
138 |
+
upscale_factor_input = gr.Slider(minimum=1, maximum=4, value=1, step=0.1, label="Upscale Factor (1 = No Upscaling)")
|
139 |
|
140 |
with gr.Row():
|
141 |
detail_level_input = gr.Slider(minimum=0, maximum=10, value=5, step=1, label="Detail Level")
|
|
|
169 |
fn=convert_image,
|
170 |
inputs=[
|
171 |
image_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
|
172 |
+
color_mode_input, hierarchical_input, mode_input, filter_speckle_input,
|
173 |
+
color_precision_input, length_threshold_input, max_iterations_input,
|
174 |
+
splice_threshold_input, path_precision_input, enhance_with_ai_input, remove_bg_input, upscale_factor_input
|
175 |
],
|
176 |
outputs=[svg_output, download_output]
|
177 |
)
|
178 |
|
179 |
+
|
180 |
iface.launch()
|