Spaces:
Runtime error
Runtime error
gdTharusha
commited on
Commit
•
bb3573e
1
Parent(s):
cbbdd98
Update app.py
Browse files
app.py
CHANGED
@@ -6,27 +6,55 @@ import tempfile
|
|
6 |
import vtracer
|
7 |
from skimage import color, filters
|
8 |
from skimage.feature import canny
|
9 |
-
from skimage.
|
10 |
-
|
11 |
-
def
|
12 |
-
"""
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
filter_speckle, color_precision, layer_difference, corner_threshold, length_threshold,
|
27 |
max_iterations, splice_threshold, path_precision):
|
28 |
"""Converts an image to SVG using vtracer with customizable parameters."""
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
# Preprocess the image
|
31 |
image = preprocess_image(image, blur_radius, edge_enhance, edge_threshold)
|
32 |
|
@@ -61,38 +89,52 @@ def convert_image(image, blur_radius, edge_enhance, edge_threshold, color_mode,
|
|
61 |
svg_html = f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'
|
62 |
return gr.HTML(svg_html), temp_file.name
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
# Gradio interface
|
65 |
iface = gr.Blocks()
|
66 |
|
67 |
with iface:
|
68 |
-
gr.Markdown("# CPU-Optimized AI-Enhanced Image to SVG Vectors")
|
69 |
-
gr.Markdown("Upload an image and
|
70 |
|
71 |
with gr.Row():
|
72 |
image_input = gr.Image(type="pil", label="Upload Image")
|
|
|
|
|
|
|
73 |
blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.5, label="Blur Radius (for smoothing)")
|
74 |
edge_enhance_input = gr.Checkbox(value=False, label="AI Edge Enhance")
|
75 |
edge_threshold_input = gr.Slider(minimum=0.1, maximum=3.0, value=1.0, step=0.1, label="Edge Detection Threshold")
|
76 |
-
|
77 |
-
with gr.Row():
|
78 |
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
79 |
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
|
80 |
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
|
81 |
-
|
82 |
-
with gr.Row():
|
83 |
filter_speckle_input = gr.Slider(minimum=1, maximum=100, value=4, step=1, label="Filter Speckle")
|
84 |
color_precision_input = gr.Slider(minimum=1, maximum=100, value=6, step=1, label="Color Precision")
|
85 |
layer_difference_input = gr.Slider(minimum=1, maximum=100, value=16, step=1, label="Layer Difference")
|
86 |
-
|
87 |
-
with gr.Row():
|
88 |
corner_threshold_input = gr.Slider(minimum=1, maximum=100, value=60, step=1, label="Corner Threshold")
|
89 |
length_threshold_input = gr.Slider(minimum=1, maximum=100, value=4.0, step=0.5, label="Length Threshold")
|
90 |
max_iterations_input = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Max Iterations")
|
91 |
-
|
92 |
-
with gr.Row():
|
93 |
splice_threshold_input = gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold")
|
94 |
path_precision_input = gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
|
95 |
|
|
|
|
|
|
|
|
|
|
|
96 |
convert_button = gr.Button("Convert Image to SVG")
|
97 |
svg_output = gr.HTML(label="SVG Output")
|
98 |
download_output = gr.File(label="Download SVG")
|
@@ -100,10 +142,10 @@ with iface:
|
|
100 |
convert_button.click(
|
101 |
fn=convert_image,
|
102 |
inputs=[
|
103 |
-
image_input, blur_radius_input, edge_enhance_input, edge_threshold_input,
|
104 |
-
hierarchical_input, mode_input, filter_speckle_input, color_precision_input,
|
105 |
-
corner_threshold_input, length_threshold_input, max_iterations_input,
|
106 |
-
path_precision_input
|
107 |
],
|
108 |
outputs=[svg_output, download_output]
|
109 |
)
|
|
|
6 |
import vtracer
|
7 |
from skimage import color, filters
|
8 |
from skimage.feature import canny
|
9 |
+
from skimage.measure import shannon_entropy
|
10 |
+
|
11 |
+
def analyze_image(image):
|
12 |
+
"""Automatically adjust settings based on image analysis."""
|
13 |
+
# Convert image to grayscale for analysis
|
14 |
+
gray_image = np.array(image.convert('L'))
|
15 |
+
|
16 |
+
# Calculate basic metrics
|
17 |
+
entropy = shannon_entropy(gray_image)
|
18 |
+
edges = canny(gray_image, sigma=1.0).mean() # Mean edge intensity
|
19 |
+
|
20 |
+
# Adjust parameters based on metrics
|
21 |
+
if entropy > 5.0:
|
22 |
+
blur_radius = 0
|
23 |
+
edge_enhance = True
|
24 |
+
edge_threshold = 2.0
|
25 |
+
else:
|
26 |
+
blur_radius = 2
|
27 |
+
edge_enhance = False
|
28 |
+
edge_threshold = 1.0
|
29 |
+
|
30 |
+
color_mode = "Color" if entropy > 4.0 else "Binary"
|
31 |
+
hierarchical = "Stacked" if edges > 0.1 else "Cutout"
|
32 |
+
mode = "Spline" if edges > 0.15 else "Polygon"
|
33 |
+
|
34 |
+
filter_speckle = int(5 if edges > 0.12 else 15)
|
35 |
+
color_precision = int(8 if entropy > 4.5 else 10)
|
36 |
+
layer_difference = int(12 if edges > 0.2 else 20)
|
37 |
+
corner_threshold = int(60 if edges > 0.12 else 75)
|
38 |
+
length_threshold = float(5.0 if edges > 0.15 else 10.0)
|
39 |
+
max_iterations = int(15 if entropy > 4.5 else 10)
|
40 |
+
splice_threshold = int(50 if entropy > 4.0 else 70)
|
41 |
+
path_precision = int(10 if edges > 0.15 else 5)
|
42 |
+
|
43 |
+
return (blur_radius, edge_enhance, edge_threshold, color_mode, hierarchical, mode,
|
44 |
+
filter_speckle, color_precision, layer_difference, corner_threshold, length_threshold,
|
45 |
+
max_iterations, splice_threshold, path_precision)
|
46 |
+
|
47 |
+
def convert_image(image, auto_settings, blur_radius, edge_enhance, edge_threshold, color_mode, hierarchical, mode,
|
48 |
filter_speckle, color_precision, layer_difference, corner_threshold, length_threshold,
|
49 |
max_iterations, splice_threshold, path_precision):
|
50 |
"""Converts an image to SVG using vtracer with customizable parameters."""
|
51 |
|
52 |
+
# Auto-adjust settings based on image if auto_settings is enabled
|
53 |
+
if auto_settings:
|
54 |
+
(blur_radius, edge_enhance, edge_threshold, color_mode, hierarchical, mode,
|
55 |
+
filter_speckle, color_precision, layer_difference, corner_threshold, length_threshold,
|
56 |
+
max_iterations, splice_threshold, path_precision) = analyze_image(image)
|
57 |
+
|
58 |
# Preprocess the image
|
59 |
image = preprocess_image(image, blur_radius, edge_enhance, edge_threshold)
|
60 |
|
|
|
89 |
svg_html = f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'
|
90 |
return gr.HTML(svg_html), temp_file.name
|
91 |
|
92 |
+
def preprocess_image(image, blur_radius, edge_enhance, edge_threshold):
|
93 |
+
"""Applies advanced preprocessing steps to the image before tracing."""
|
94 |
+
if blur_radius > 0:
|
95 |
+
image = image.filter(ImageFilter.GaussianBlur(blur_radius))
|
96 |
+
|
97 |
+
if edge_enhance:
|
98 |
+
# Convert image to grayscale and apply edge detection
|
99 |
+
gray_image = np.array(image.convert('L'))
|
100 |
+
edges = canny(gray_image, sigma=edge_threshold)
|
101 |
+
edges_img = Image.fromarray((edges * 255).astype(np.uint8))
|
102 |
+
image = Image.blend(image.convert('RGB'), edges_img.convert('RGB'), alpha=0.5)
|
103 |
+
|
104 |
+
return image
|
105 |
+
|
106 |
# Gradio interface
|
107 |
iface = gr.Blocks()
|
108 |
|
109 |
with iface:
|
110 |
+
gr.Markdown("# CPU-Optimized AI-Enhanced Image to SVG Vectors with Auto Settings")
|
111 |
+
gr.Markdown("Upload an image and either use custom parameters or let the auto settings analyze and adjust everything for you.")
|
112 |
|
113 |
with gr.Row():
|
114 |
image_input = gr.Image(type="pil", label="Upload Image")
|
115 |
+
auto_settings_input = gr.Checkbox(value=False, label="Auto Settings")
|
116 |
+
|
117 |
+
with gr.Row(visible=False) as manual_settings:
|
118 |
blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.5, label="Blur Radius (for smoothing)")
|
119 |
edge_enhance_input = gr.Checkbox(value=False, label="AI Edge Enhance")
|
120 |
edge_threshold_input = gr.Slider(minimum=0.1, maximum=3.0, value=1.0, step=0.1, label="Edge Detection Threshold")
|
|
|
|
|
121 |
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
122 |
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
|
123 |
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
|
|
|
|
|
124 |
filter_speckle_input = gr.Slider(minimum=1, maximum=100, value=4, step=1, label="Filter Speckle")
|
125 |
color_precision_input = gr.Slider(minimum=1, maximum=100, value=6, step=1, label="Color Precision")
|
126 |
layer_difference_input = gr.Slider(minimum=1, maximum=100, value=16, step=1, label="Layer Difference")
|
|
|
|
|
127 |
corner_threshold_input = gr.Slider(minimum=1, maximum=100, value=60, step=1, label="Corner Threshold")
|
128 |
length_threshold_input = gr.Slider(minimum=1, maximum=100, value=4.0, step=0.5, label="Length Threshold")
|
129 |
max_iterations_input = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Max Iterations")
|
|
|
|
|
130 |
splice_threshold_input = gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold")
|
131 |
path_precision_input = gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
|
132 |
|
133 |
+
def toggle_settings(auto_settings):
|
134 |
+
return gr.update(visible=not auto_settings)
|
135 |
+
|
136 |
+
auto_settings_input.change(toggle_settings, inputs=[auto_settings_input], outputs=manual_settings)
|
137 |
+
|
138 |
convert_button = gr.Button("Convert Image to SVG")
|
139 |
svg_output = gr.HTML(label="SVG Output")
|
140 |
download_output = gr.File(label="Download SVG")
|
|
|
142 |
convert_button.click(
|
143 |
fn=convert_image,
|
144 |
inputs=[
|
145 |
+
image_input, auto_settings_input, blur_radius_input, edge_enhance_input, edge_threshold_input,
|
146 |
+
color_mode_input, hierarchical_input, mode_input, filter_speckle_input, color_precision_input,
|
147 |
+
layer_difference_input, corner_threshold_input, length_threshold_input, max_iterations_input,
|
148 |
+
splice_threshold_input, path_precision_input
|
149 |
],
|
150 |
outputs=[svg_output, download_output]
|
151 |
)
|