Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,99 +7,129 @@ 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."""
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
56 |
return image
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
|
59 |
-
color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
|
60 |
corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision,
|
61 |
-
enhance_with_ai, remove_bg):
|
62 |
"""Convert an image to SVG using vtracer with customizable and advanced parameters."""
|
63 |
-
|
64 |
# Preprocess the image with additional detail level settings
|
65 |
image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg)
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
# Convert Gradio image to bytes for vtracer compatibility
|
68 |
img_byte_array = io.BytesIO()
|
69 |
image.save(img_byte_array, format='PNG')
|
70 |
img_bytes = img_byte_array.getvalue()
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# Gradio interface
|
99 |
iface = gr.Blocks()
|
100 |
|
101 |
with iface:
|
102 |
-
gr.Markdown("# Super-Advanced Image to SVG Converter")
|
103 |
|
104 |
with gr.Row():
|
105 |
image_input = gr.Image(type="pil", label="Upload Image")
|
@@ -133,6 +163,8 @@ with iface:
|
|
133 |
splice_threshold_input = gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold")
|
134 |
path_precision_input = gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
|
135 |
|
|
|
|
|
136 |
convert_button = gr.Button("Convert Image to SVG")
|
137 |
svg_output = gr.HTML(label="SVG Output")
|
138 |
download_output = gr.File(label="Download SVG")
|
@@ -143,7 +175,7 @@ with iface:
|
|
143 |
image_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
|
144 |
color_mode_input, hierarchical_input, mode_input, filter_speckle_input, color_precision_input,
|
145 |
layer_difference_input, corner_threshold_input, length_threshold_input, max_iterations_input,
|
146 |
-
splice_threshold_input, path_precision_input, enhance_with_ai_input, remove_bg_input
|
147 |
],
|
148 |
outputs=[svg_output, download_output]
|
149 |
)
|
|
|
7 |
from skimage import feature, filters, morphology
|
8 |
import cv2
|
9 |
from rembg import remove
|
10 |
+
import torch
|
11 |
+
from transformers import AutoModelForImageSegmentation, AutoProcessor
|
12 |
+
import requests
|
13 |
+
from huggingface_hub import hf_hub_download
|
14 |
+
|
15 |
+
# Load additional Hugging Face models
|
16 |
+
segmentation_model = AutoModelForImageSegmentation.from_pretrained("facebook/dino-vitb16")
|
17 |
+
segmentation_processor = AutoProcessor.from_pretrained("facebook/dino-vitb16")
|
18 |
|
19 |
def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg):
|
20 |
"""Advanced preprocessing of the image before vectorization."""
|
21 |
+
try:
|
22 |
+
if blur_radius > 0:
|
23 |
+
image = image.filter(ImageFilter.GaussianBlur(blur_radius))
|
24 |
+
|
25 |
+
if sharpen_radius > 0:
|
26 |
+
image = image.filter(ImageFilter.UnsharpMask(radius=sharpen_radius, percent=150, threshold=3))
|
27 |
+
|
28 |
+
if noise_reduction > 0:
|
29 |
+
image_np = np.array(image)
|
30 |
+
image_np = cv2.fastNlMeansDenoisingColored(image_np, None, h=noise_reduction, templateWindowSize=7, searchWindowSize=21)
|
31 |
+
image = Image.fromarray(image_np)
|
32 |
+
|
33 |
+
if detail_level > 0:
|
34 |
+
sigma = max(0.5, 3.0 - (detail_level * 0.5))
|
35 |
+
image_np = np.array(image.convert('L'))
|
36 |
+
|
37 |
+
if edge_method == 'Canny':
|
38 |
+
edges = feature.canny(image_np, sigma=sigma)
|
39 |
+
elif edge_method == 'Sobel':
|
40 |
+
edges = filters.sobel(image_np)
|
41 |
+
elif edge_method == 'Scharr':
|
42 |
+
edges = filters.scharr(image_np)
|
43 |
+
else: # Prewitt
|
44 |
+
edges = filters.prewitt(image_np)
|
45 |
+
|
46 |
+
edges = morphology.dilation(edges, morphology.square(max(1, 6 - detail_level)))
|
47 |
+
edges_img = Image.fromarray((edges * 255).astype(np.uint8))
|
48 |
+
image = Image.blend(image.convert('RGB'), edges_img.convert('RGB'), alpha=0.5)
|
49 |
+
|
50 |
+
if color_quantization > 0:
|
51 |
+
image = quantize_colors(image, color_quantization)
|
52 |
+
|
53 |
+
if enhance_with_ai:
|
54 |
+
image_np = np.array(image)
|
55 |
+
# AI-based enhancement for smoothing edges and improving vectorization
|
56 |
+
image_np = remove(image_np)
|
57 |
+
image = Image.fromarray(image_np)
|
58 |
+
|
59 |
+
if remove_bg:
|
60 |
+
image_np = np.array(image)
|
61 |
+
image_np = remove(image_np)
|
62 |
+
image = Image.fromarray(image_np)
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Error during preprocessing: {e}")
|
66 |
+
raise
|
67 |
+
|
68 |
return image
|
69 |
|
70 |
+
def vectorize_with_hf_model(image):
|
71 |
+
"""Vectorizes the image using a Hugging Face model for segmentation or enhancement."""
|
72 |
+
inputs = segmentation_processor(images=image, return_tensors="pt")
|
73 |
+
outputs = segmentation_model(**inputs)
|
74 |
+
mask = outputs["masks"][0][0].cpu().detach().numpy()
|
75 |
+
mask = (mask > 0.5).astype(np.uint8) * 255
|
76 |
+
mask_image = Image.fromarray(mask)
|
77 |
+
return mask_image
|
78 |
+
|
79 |
def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
|
80 |
+
color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
|
81 |
corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision,
|
82 |
+
enhance_with_ai, remove_bg, model_choice):
|
83 |
"""Convert an image to SVG using vtracer with customizable and advanced parameters."""
|
84 |
+
|
85 |
# Preprocess the image with additional detail level settings
|
86 |
image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg)
|
87 |
+
|
88 |
+
# If a specific model is chosen, use it to process the image before vectorization
|
89 |
+
if model_choice == "Hugging Face Segmentation Model":
|
90 |
+
image = vectorize_with_hf_model(image)
|
91 |
+
|
92 |
# Convert Gradio image to bytes for vtracer compatibility
|
93 |
img_byte_array = io.BytesIO()
|
94 |
image.save(img_byte_array, format='PNG')
|
95 |
img_bytes = img_byte_array.getvalue()
|
96 |
+
|
97 |
+
try:
|
98 |
+
# Perform the conversion
|
99 |
+
svg_str = vtracer.convert_raw_image_to_svg(
|
100 |
+
img_bytes,
|
101 |
+
img_format='png',
|
102 |
+
colormode=color_mode.lower(),
|
103 |
+
hierarchical=hierarchical.lower(),
|
104 |
+
mode=mode.lower(),
|
105 |
+
filter_speckle=int(filter_speckle),
|
106 |
+
color_precision=int(color_precision),
|
107 |
+
layer_difference=int(layer_difference),
|
108 |
+
corner_threshold=int(corner_threshold),
|
109 |
+
length_threshold=float(length_threshold),
|
110 |
+
max_iterations=int(max_iterations),
|
111 |
+
splice_threshold=int(splice_threshold),
|
112 |
+
path_precision=int(path_precision)
|
113 |
+
)
|
114 |
+
|
115 |
+
# Save the SVG string to a temporary file
|
116 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
|
117 |
+
temp_file.write(svg_str.encode('utf-8'))
|
118 |
+
temp_file.close()
|
119 |
+
|
120 |
+
# Display the SVG in the Gradio interface and provide the download link
|
121 |
+
svg_html = f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'
|
122 |
+
return gr.HTML(svg_html), temp_file.name
|
123 |
+
|
124 |
+
except Exception as e:
|
125 |
+
print(f"Error during vectorization: {e}")
|
126 |
+
return f"Error: {e}", None
|
127 |
|
128 |
# Gradio interface
|
129 |
iface = gr.Blocks()
|
130 |
|
131 |
with iface:
|
132 |
+
gr.Markdown("# Super-Advanced Image to SVG Converter with Enhanced Models")
|
133 |
|
134 |
with gr.Row():
|
135 |
image_input = gr.Image(type="pil", label="Upload Image")
|
|
|
163 |
splice_threshold_input = gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold")
|
164 |
path_precision_input = gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
|
165 |
|
166 |
+
model_choice_input = gr.Radio(choices=["None", "Hugging Face Segmentation Model"], value="None", label="Choose Model")
|
167 |
+
|
168 |
convert_button = gr.Button("Convert Image to SVG")
|
169 |
svg_output = gr.HTML(label="SVG Output")
|
170 |
download_output = gr.File(label="Download SVG")
|
|
|
175 |
image_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
|
176 |
color_mode_input, hierarchical_input, mode_input, filter_speckle_input, color_precision_input,
|
177 |
layer_difference_input, corner_threshold_input, length_threshold_input, max_iterations_input,
|
178 |
+
splice_threshold_input, path_precision_input, enhance_with_ai_input, remove_bg_input, model_choice_input
|
179 |
],
|
180 |
outputs=[svg_output, download_output]
|
181 |
)
|