Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,14 +7,6 @@ import vtracer
|
|
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."""
|
@@ -67,28 +59,15 @@ def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail
|
|
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
|
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')
|
@@ -163,8 +142,6 @@ with iface:
|
|
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,7 +152,7 @@ with iface:
|
|
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
|
179 |
],
|
180 |
outputs=[svg_output, download_output]
|
181 |
)
|
|
|
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."""
|
|
|
59 |
|
60 |
return image
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
142 |
splice_threshold_input = gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold")
|
143 |
path_precision_input = gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
|
144 |
|
|
|
|
|
145 |
convert_button = gr.Button("Convert Image to SVG")
|
146 |
svg_output = gr.HTML(label="SVG Output")
|
147 |
download_output = gr.File(label="Download SVG")
|
|
|
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, color_precision_input,
|
154 |
layer_difference_input, corner_threshold_input, length_threshold_input, max_iterations_input,
|
155 |
+
splice_threshold_input, path_precision_input, enhance_with_ai_input, remove_bg_input
|
156 |
],
|
157 |
outputs=[svg_output, download_output]
|
158 |
)
|