Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,118 @@
|
|
1 |
import gradio as gr
|
2 |
import vtracer
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
image,
|
|
|
7 |
colormode="color",
|
8 |
hierarchical="stacked",
|
9 |
mode="spline",
|
@@ -16,16 +125,21 @@ def convert_to_vector(
|
|
16 |
splice_threshold=45,
|
17 |
path_precision=3
|
18 |
):
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
# Save the input image to a temporary file
|
23 |
image.save(input_path)
|
24 |
-
|
|
|
|
|
|
|
25 |
# Convert the image to SVG using VTracer
|
26 |
vtracer.convert_image_to_svg_py(
|
27 |
input_path,
|
28 |
-
|
29 |
colormode=colormode,
|
30 |
hierarchical=hierarchical,
|
31 |
mode=mode,
|
@@ -38,24 +152,61 @@ def convert_to_vector(
|
|
38 |
splice_threshold=int(splice_threshold),
|
39 |
path_precision=int(path_precision)
|
40 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Read the SVG output
|
42 |
-
with open(
|
43 |
svg_content = f.read()
|
44 |
-
|
45 |
-
# Return the SVG file
|
46 |
-
return
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
def handle_color_mode(value):
|
50 |
-
# You can change this to display the selected value without any prefix
|
51 |
return value
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
"
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
css = """
|
61 |
#col-container {
|
@@ -73,18 +224,26 @@ css = """
|
|
73 |
}
|
74 |
"""
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# Define the Gradio interface
|
77 |
with gr.Blocks(css=css) as app:
|
78 |
with gr.Column(elem_id="col-container"):
|
79 |
gr.HTML("""
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
with gr.Row():
|
86 |
with gr.Column():
|
87 |
image_input = gr.Image(type="pil", label="Upload Image")
|
|
|
|
|
88 |
with gr.Accordion("Advanced Settings", open=False):
|
89 |
with gr.Accordion("Clustering", open=False):
|
90 |
colormode = gr.Radio([("COLOR","color"),("B/W", "binary")], value="color", label="Color Mode", show_label=False)
|
@@ -100,67 +259,47 @@ with gr.Blocks(css=css) as app:
|
|
100 |
max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations", visible=False)
|
101 |
path_precision = gr.Slider(1, 10, value=3, step=1, label="Path Precision", visible=False)
|
102 |
output_text = gr.Textbox(label="Selected Mode", visible=False)
|
|
|
103 |
with gr.Row():
|
104 |
clear_button = gr.Button("Clear")
|
105 |
-
convert_button = gr.Button("✨ Convert to
|
106 |
|
107 |
with gr.Column():
|
108 |
-
html = gr.HTML(label="SVG
|
109 |
svg_output = gr.File(label="Download SVG")
|
|
|
110 |
|
111 |
gr.Examples(
|
112 |
-
examples
|
113 |
-
fn
|
114 |
-
inputs
|
115 |
-
outputs
|
116 |
cache_examples=False,
|
117 |
-
run_on_click
|
118 |
)
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
"layer_difference": 16
|
126 |
-
}
|
127 |
-
|
128 |
-
def clear_inputs():
|
129 |
-
return gr.Image(value=None), gr.Radio(value="color"), gr.Radio(value="stacked"), gr.Radio(value="spline"), gr.Slider(value=4), gr.Slider(value=6), gr.Slider(value=16), gr.Slider(value=60), gr.Slider(value=4.0), gr.Slider(value=10), gr.Slider(value=45), gr.Slider(value=3)
|
130 |
-
|
131 |
-
|
132 |
-
def update_interactivity_and_visibility(colormode, color_precision_value, layer_difference_value):
|
133 |
-
is_color_mode = colormode == "color"
|
134 |
-
return (
|
135 |
-
gr.update(interactive=is_color_mode),
|
136 |
-
gr.update(interactive=is_color_mode),
|
137 |
-
gr.update(visible=is_color_mode) # Show/Hide Hierarchical Mode
|
138 |
-
)
|
139 |
-
|
140 |
colormode.change(
|
141 |
update_interactivity_and_visibility,
|
142 |
inputs=[colormode, color_precision, layer_difference],
|
143 |
outputs=[color_precision, layer_difference, hierarchical]
|
144 |
)
|
145 |
|
146 |
-
def update_interactivity_and_visibility_for_mode(mode):
|
147 |
-
is_spline_mode = mode == "spline"
|
148 |
-
return (
|
149 |
-
gr.update(interactive=is_spline_mode),
|
150 |
-
gr.update(interactive=is_spline_mode),
|
151 |
-
gr.update(interactive=is_spline_mode)
|
152 |
-
)
|
153 |
-
|
154 |
mode.change(
|
155 |
update_interactivity_and_visibility_for_mode,
|
156 |
inputs=[mode],
|
157 |
-
outputs=[corner_threshold,length_threshold,splice_threshold]
|
158 |
)
|
159 |
|
160 |
clear_button.click(
|
161 |
clear_inputs,
|
162 |
outputs=[
|
163 |
image_input,
|
|
|
164 |
colormode,
|
165 |
hierarchical,
|
166 |
mode,
|
@@ -176,9 +315,10 @@ with gr.Blocks(css=css) as app:
|
|
176 |
)
|
177 |
|
178 |
convert_button.click(
|
179 |
-
|
180 |
inputs=[
|
181 |
image_input,
|
|
|
182 |
colormode,
|
183 |
hierarchical,
|
184 |
mode,
|
@@ -191,9 +331,9 @@ with gr.Blocks(css=css) as app:
|
|
191 |
splice_threshold,
|
192 |
path_precision
|
193 |
],
|
194 |
-
outputs=[html,svg_output]
|
195 |
)
|
196 |
|
197 |
# Launch the app
|
198 |
-
|
199 |
-
app.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import vtracer
|
3 |
import os
|
4 |
+
import pandas as pd
|
5 |
+
from io import BytesIO
|
6 |
+
from PIL import Image
|
7 |
+
import cairosvg
|
8 |
+
import cv2
|
9 |
+
import numpy as np
|
10 |
+
import tempfile
|
11 |
|
12 |
+
def clean_svg(svg_string):
|
13 |
+
"""Optional function to clean SVG if needed"""
|
14 |
+
return svg_string
|
15 |
+
|
16 |
+
def rasterize_svg(svg_string, width, height, dpi=128, scale=1):
|
17 |
+
"""Convert SVG string to PNG image while maintaining aspect ratio"""
|
18 |
+
try:
|
19 |
+
svg_raster_bytes = cairosvg.svg2png(
|
20 |
+
bytestring=svg_string,
|
21 |
+
background_color='white',
|
22 |
+
output_width=width,
|
23 |
+
output_height=height,
|
24 |
+
dpi=dpi,
|
25 |
+
scale=scale)
|
26 |
+
svg_raster = Image.open(BytesIO(svg_raster_bytes))
|
27 |
+
except:
|
28 |
+
try:
|
29 |
+
svg = clean_svg(svg_string)
|
30 |
+
svg_raster_bytes = cairosvg.svg2png(
|
31 |
+
bytestring=svg,
|
32 |
+
background_color='white',
|
33 |
+
output_width=width,
|
34 |
+
output_height=height,
|
35 |
+
dpi=dpi,
|
36 |
+
scale=scale)
|
37 |
+
svg_raster = Image.open(BytesIO(svg_raster_bytes))
|
38 |
+
except:
|
39 |
+
svg_raster = Image.new('RGB', (width, height), color='white')
|
40 |
+
return svg_raster
|
41 |
+
|
42 |
+
def create_video_from_frames(frame_files, output_path, duration_seconds, width, height):
|
43 |
+
"""Create video from sequence of frames with specified duration"""
|
44 |
+
# Calculate frame rate based on desired duration
|
45 |
+
num_frames = len(frame_files)
|
46 |
+
fps = max(1, num_frames / duration_seconds) # Ensure at least 1 fps
|
47 |
+
|
48 |
+
# Initialize video writer
|
49 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
50 |
+
video = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
51 |
+
|
52 |
+
# Read each frame and write to video
|
53 |
+
for frame_file in frame_files:
|
54 |
+
# Read image with PIL and convert to OpenCV format
|
55 |
+
pil_img = Image.open(frame_file)
|
56 |
+
cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
57 |
+
video.write(cv_img)
|
58 |
+
|
59 |
+
# Add last frame to fill remaining time if needed
|
60 |
+
if num_frames > 0:
|
61 |
+
remaining_frames = max(0, int(fps * duration_seconds) - num_frames)
|
62 |
+
for _ in range(remaining_frames):
|
63 |
+
video.write(cv_img)
|
64 |
+
|
65 |
+
video.release()
|
66 |
+
|
67 |
+
def process_svg_to_video(input_svg_path, original_width, original_height, video_duration_seconds=10, chunk_size=None):
|
68 |
+
"""Process SVG file and create a video with specified duration using exact row slicing"""
|
69 |
+
# Read SVG file as a table to maintain exact row slicing logic
|
70 |
+
df = pd.read_table(input_svg_path, header=None)
|
71 |
+
df_head = df.head(3)
|
72 |
+
df_tail = df.tail(1)
|
73 |
+
df_middle = df.iloc[3:-1, :]
|
74 |
+
|
75 |
+
# Use the original image dimensions
|
76 |
+
width, height = original_width, original_height
|
77 |
+
|
78 |
+
# Calculate chunk size if not specified
|
79 |
+
total_rows = len(df_middle)
|
80 |
+
if chunk_size is None:
|
81 |
+
chunk_size = max(1, total_rows // 30) # Default to ~30 steps
|
82 |
+
|
83 |
+
# Create a temporary directory for images
|
84 |
+
temp_dir = tempfile.mkdtemp()
|
85 |
+
frame_files = []
|
86 |
+
|
87 |
+
# Process each chunk and save as image
|
88 |
+
for i in range(0, total_rows, chunk_size):
|
89 |
+
current_chunk = df_middle.iloc[:i+chunk_size]
|
90 |
+
|
91 |
+
# Combine with head and tail
|
92 |
+
combined_df = pd.concat([df_head, current_chunk, df_tail])
|
93 |
+
svg_content = "\n".join(combined_df[0].astype(str).values.tolist())
|
94 |
+
|
95 |
+
# Convert to image using original dimensions
|
96 |
+
img = rasterize_svg(svg_content, width, height)
|
97 |
+
img_filename = os.path.join(temp_dir, f"frame_{i:04d}.png")
|
98 |
+
img.save(img_filename)
|
99 |
+
frame_files.append(img_filename)
|
100 |
+
|
101 |
+
# Create output video path
|
102 |
+
output_video_path = os.path.join(temp_dir, "output_video.mp4")
|
103 |
+
|
104 |
+
# Create video from frames
|
105 |
+
create_video_from_frames(frame_files, output_video_path, video_duration_seconds, width, height)
|
106 |
+
|
107 |
+
# Clean up temporary files (except the video)
|
108 |
+
for file in frame_files:
|
109 |
+
os.remove(file)
|
110 |
+
|
111 |
+
return output_video_path, temp_dir
|
112 |
+
|
113 |
+
def convert_to_vector_and_video(
|
114 |
image,
|
115 |
+
video_duration=10,
|
116 |
colormode="color",
|
117 |
hierarchical="stacked",
|
118 |
mode="spline",
|
|
|
125 |
splice_threshold=45,
|
126 |
path_precision=3
|
127 |
):
|
128 |
+
# Create temporary directory
|
129 |
+
temp_dir = tempfile.mkdtemp()
|
130 |
+
input_path = os.path.join(temp_dir, "temp_input.jpg")
|
131 |
+
output_svg_path = os.path.join(temp_dir, "svg_output.svg")
|
132 |
+
|
133 |
# Save the input image to a temporary file
|
134 |
image.save(input_path)
|
135 |
+
|
136 |
+
# Get original dimensions from the uploaded image
|
137 |
+
original_width, original_height = image.size
|
138 |
+
|
139 |
# Convert the image to SVG using VTracer
|
140 |
vtracer.convert_image_to_svg_py(
|
141 |
input_path,
|
142 |
+
output_svg_path,
|
143 |
colormode=colormode,
|
144 |
hierarchical=hierarchical,
|
145 |
mode=mode,
|
|
|
152 |
splice_threshold=int(splice_threshold),
|
153 |
path_precision=int(path_precision)
|
154 |
)
|
155 |
+
|
156 |
+
# Process SVG to create video using the original dimensions
|
157 |
+
video_path, video_temp_dir = process_svg_to_video(
|
158 |
+
output_svg_path,
|
159 |
+
original_width,
|
160 |
+
original_height,
|
161 |
+
video_duration_seconds=video_duration
|
162 |
+
)
|
163 |
+
|
164 |
# Read the SVG output
|
165 |
+
with open(output_svg_path, "r") as f:
|
166 |
svg_content = f.read()
|
167 |
+
|
168 |
+
# Return the SVG preview, SVG file, and video file
|
169 |
+
return (
|
170 |
+
gr.HTML(f'<svg viewBox="0 0 {original_width} {original_height}">{svg_content}</svg>'),
|
171 |
+
output_svg_path,
|
172 |
+
video_path
|
173 |
+
)
|
174 |
|
175 |
def handle_color_mode(value):
|
|
|
176 |
return value
|
177 |
|
178 |
+
def clear_inputs():
|
179 |
+
return (
|
180 |
+
gr.Image(value=None),
|
181 |
+
gr.Slider(value=10),
|
182 |
+
gr.Radio(value="color"),
|
183 |
+
gr.Radio(value="stacked"),
|
184 |
+
gr.Radio(value="spline"),
|
185 |
+
gr.Slider(value=4),
|
186 |
+
gr.Slider(value=6),
|
187 |
+
gr.Slider(value=16),
|
188 |
+
gr.Slider(value=60),
|
189 |
+
gr.Slider(value=4.0),
|
190 |
+
gr.Slider(value=10),
|
191 |
+
gr.Slider(value=45),
|
192 |
+
gr.Slider(value=3)
|
193 |
+
)
|
194 |
|
195 |
+
def update_interactivity_and_visibility(colormode, color_precision_value, layer_difference_value):
|
196 |
+
is_color_mode = colormode == "color"
|
197 |
+
return (
|
198 |
+
gr.update(interactive=is_color_mode),
|
199 |
+
gr.update(interactive=is_color_mode),
|
200 |
+
gr.update(visible=is_color_mode)
|
201 |
+
)
|
202 |
+
|
203 |
+
def update_interactivity_and_visibility_for_mode(mode):
|
204 |
+
is_spline_mode = mode == "spline"
|
205 |
+
return (
|
206 |
+
gr.update(interactive=is_spline_mode),
|
207 |
+
gr.update(interactive=is_spline_mode),
|
208 |
+
gr.update(interactive=is_spline_mode)
|
209 |
+
)
|
210 |
|
211 |
css = """
|
212 |
#col-container {
|
|
|
224 |
}
|
225 |
"""
|
226 |
|
227 |
+
examples = [
|
228 |
+
"examples/01.jpg",
|
229 |
+
"examples/02.jpg",
|
230 |
+
"examples/03.jpg",
|
231 |
+
]
|
232 |
+
|
233 |
# Define the Gradio interface
|
234 |
with gr.Blocks(css=css) as app:
|
235 |
with gr.Column(elem_id="col-container"):
|
236 |
gr.HTML("""
|
237 |
+
<div style="text-align: center;">
|
238 |
+
<h2>Image to Vector Video Converter ⚡</h2>
|
239 |
+
<p>Converts raster images to vector graphics and creates progressive rendering videos.</p>
|
240 |
+
</div>
|
241 |
+
""")
|
242 |
with gr.Row():
|
243 |
with gr.Column():
|
244 |
image_input = gr.Image(type="pil", label="Upload Image")
|
245 |
+
video_duration = gr.Slider(1, 60, value=10, step=1, label="Video Duration (seconds)")
|
246 |
+
|
247 |
with gr.Accordion("Advanced Settings", open=False):
|
248 |
with gr.Accordion("Clustering", open=False):
|
249 |
colormode = gr.Radio([("COLOR","color"),("B/W", "binary")], value="color", label="Color Mode", show_label=False)
|
|
|
259 |
max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations", visible=False)
|
260 |
path_precision = gr.Slider(1, 10, value=3, step=1, label="Path Precision", visible=False)
|
261 |
output_text = gr.Textbox(label="Selected Mode", visible=False)
|
262 |
+
|
263 |
with gr.Row():
|
264 |
clear_button = gr.Button("Clear")
|
265 |
+
convert_button = gr.Button("✨ Convert to Video", variant='primary', elem_classes=["generate-btn"])
|
266 |
|
267 |
with gr.Column():
|
268 |
+
html = gr.HTML(label="SVG Preview")
|
269 |
svg_output = gr.File(label="Download SVG")
|
270 |
+
video_output = gr.Video(label="Rendering Video")
|
271 |
|
272 |
gr.Examples(
|
273 |
+
examples=examples,
|
274 |
+
fn=convert_to_vector_and_video,
|
275 |
+
inputs=[image_input],
|
276 |
+
outputs=[html, svg_output, video_output],
|
277 |
cache_examples=False,
|
278 |
+
run_on_click=True
|
279 |
)
|
280 |
+
|
281 |
+
# Event handlers
|
282 |
+
colormode.change(handle_color_mode, inputs=colormode, outputs=output_text)
|
283 |
+
hierarchical.change(handle_color_mode, inputs=hierarchical, outputs=output_text)
|
284 |
+
mode.change(handle_color_mode, inputs=mode, outputs=output_text)
|
285 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
colormode.change(
|
287 |
update_interactivity_and_visibility,
|
288 |
inputs=[colormode, color_precision, layer_difference],
|
289 |
outputs=[color_precision, layer_difference, hierarchical]
|
290 |
)
|
291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
mode.change(
|
293 |
update_interactivity_and_visibility_for_mode,
|
294 |
inputs=[mode],
|
295 |
+
outputs=[corner_threshold, length_threshold, splice_threshold]
|
296 |
)
|
297 |
|
298 |
clear_button.click(
|
299 |
clear_inputs,
|
300 |
outputs=[
|
301 |
image_input,
|
302 |
+
video_duration,
|
303 |
colormode,
|
304 |
hierarchical,
|
305 |
mode,
|
|
|
315 |
)
|
316 |
|
317 |
convert_button.click(
|
318 |
+
convert_to_vector_and_video,
|
319 |
inputs=[
|
320 |
image_input,
|
321 |
+
video_duration,
|
322 |
colormode,
|
323 |
hierarchical,
|
324 |
mode,
|
|
|
331 |
splice_threshold,
|
332 |
path_precision
|
333 |
],
|
334 |
+
outputs=[html, svg_output, video_output]
|
335 |
)
|
336 |
|
337 |
# Launch the app
|
338 |
+
if __name__ == "__main__":
|
339 |
+
app.launch(share=True)
|