svjack commited on
Commit
79e3682
·
verified ·
1 Parent(s): a669fe3

Create app_exp2.py

Browse files
Files changed (1) hide show
  1. app_exp2.py +357 -0
app_exp2.py ADDED
@@ -0,0 +1,357 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=30):
68
+ """Process SVG file and create a video with specified duration using exponential 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
+ # If chunk_size is 0, use automatic calculation (start with 1)
79
+ total_rows = len(df_middle)
80
+ if chunk_size == 0:
81
+ initial_chunk = 1 # Start with 1 path element
82
+ else:
83
+ initial_chunk = max(1, min(chunk_size, total_rows)) # Ensure it's within valid range
84
+
85
+ # Create a temporary directory for images
86
+ temp_dir = tempfile.mkdtemp()
87
+ frame_files = []
88
+
89
+ # Process with exponential chunk sizes
90
+ current_chunk_size = initial_chunk
91
+ processed_rows = 0
92
+
93
+ while processed_rows < total_rows:
94
+ # Calculate end index for this chunk
95
+ end_idx = min(processed_rows + current_chunk_size, total_rows)
96
+ current_chunk = df_middle.iloc[:end_idx]
97
+
98
+ # Combine with head and tail
99
+ combined_df = pd.concat([df_head, current_chunk, df_tail])
100
+ svg_content = "\n".join(combined_df[0].astype(str).values.tolist())
101
+
102
+ # Convert to image using original dimensions
103
+ img = rasterize_svg(svg_content, width, height)
104
+ img_filename = os.path.join(temp_dir, f"frame_{processed_rows:04d}.png")
105
+ img.save(img_filename)
106
+ frame_files.append(img_filename)
107
+
108
+ # Update counters
109
+ processed_rows = end_idx
110
+ current_chunk_size *= 2 # Double the chunk size for next iteration
111
+
112
+ # Create output video path
113
+ output_video_path = os.path.join(temp_dir, "output_video.mp4")
114
+
115
+ # Create video from frames
116
+ create_video_from_frames(frame_files, output_video_path, video_duration_seconds, width, height)
117
+
118
+ # Clean up temporary files (except the video)
119
+ for file in frame_files:
120
+ os.remove(file)
121
+
122
+ return output_video_path, temp_dir
123
+
124
+ def convert_to_vector_and_video(
125
+ image,
126
+ video_duration=10,
127
+ chunk_size=30,
128
+ colormode="color",
129
+ hierarchical="stacked",
130
+ mode="spline",
131
+ filter_speckle=4,
132
+ color_precision=6,
133
+ layer_difference=16,
134
+ corner_threshold=60,
135
+ length_threshold=4.0,
136
+ max_iterations=10,
137
+ splice_threshold=45,
138
+ path_precision=3
139
+ ):
140
+ # Create temporary directory
141
+ temp_dir = tempfile.mkdtemp()
142
+ input_path = os.path.join(temp_dir, "temp_input.jpg")
143
+ output_svg_path = os.path.join(temp_dir, "svg_output.svg")
144
+
145
+ # Save the input image to a temporary file
146
+ image.save(input_path)
147
+
148
+ # Get original dimensions from the uploaded image
149
+ original_width, original_height = image.size
150
+
151
+ # Convert the image to SVG using VTracer
152
+ vtracer.convert_image_to_svg_py(
153
+ input_path,
154
+ output_svg_path,
155
+ colormode=colormode,
156
+ hierarchical=hierarchical,
157
+ mode=mode,
158
+ filter_speckle=int(filter_speckle),
159
+ color_precision=int(color_precision),
160
+ layer_difference=int(layer_difference),
161
+ corner_threshold=int(corner_threshold),
162
+ length_threshold=float(length_threshold),
163
+ max_iterations=int(max_iterations),
164
+ splice_threshold=int(splice_threshold),
165
+ path_precision=int(path_precision)
166
+ )
167
+
168
+ # Process SVG to create video using the original dimensions
169
+ video_path, video_temp_dir = process_svg_to_video(
170
+ output_svg_path,
171
+ original_width,
172
+ original_height,
173
+ video_duration_seconds=video_duration,
174
+ chunk_size=chunk_size
175
+ )
176
+
177
+ # Read the SVG output
178
+ with open(output_svg_path, "r") as f:
179
+ svg_content = f.read()
180
+
181
+ # Return the SVG preview, SVG file, and video file
182
+ return (
183
+ gr.HTML(f'<svg viewBox="0 0 {original_width} {original_height}">{svg_content}</svg>'),
184
+ output_svg_path,
185
+ video_path
186
+ )
187
+
188
+ def handle_color_mode(value):
189
+ return value
190
+
191
+ def clear_inputs():
192
+ return (
193
+ gr.Image(value=None),
194
+ gr.Slider(value=10),
195
+ gr.Slider(value=30),
196
+ gr.Radio(value="color"),
197
+ gr.Radio(value="stacked"),
198
+ gr.Radio(value="spline"),
199
+ gr.Slider(value=4),
200
+ gr.Slider(value=6),
201
+ gr.Slider(value=16),
202
+ gr.Slider(value=60),
203
+ gr.Slider(value=4.0),
204
+ gr.Slider(value=10),
205
+ gr.Slider(value=45),
206
+ gr.Slider(value=3)
207
+ )
208
+
209
+ def update_interactivity_and_visibility(colormode, color_precision_value, layer_difference_value):
210
+ is_color_mode = colormode == "color"
211
+ return (
212
+ gr.update(interactive=is_color_mode),
213
+ gr.update(interactive=is_color_mode),
214
+ gr.update(visible=is_color_mode)
215
+ )
216
+
217
+ def update_interactivity_and_visibility_for_mode(mode):
218
+ is_spline_mode = mode == "spline"
219
+ return (
220
+ gr.update(interactive=is_spline_mode),
221
+ gr.update(interactive=is_spline_mode),
222
+ gr.update(interactive=is_spline_mode)
223
+ )
224
+
225
+ css = """
226
+ #col-container {
227
+ margin: 0 auto;
228
+ max-width: 960px;
229
+ }
230
+ .generate-btn {
231
+ background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
232
+ border: none !important;
233
+ color: white !important;
234
+ }
235
+ .generate-btn:hover {
236
+ transform: translateY(-2px);
237
+ box-shadow: 0 5px 15px rgba(0,0,0,0.2);
238
+ }
239
+ """
240
+
241
+ examples = [
242
+ "examples/01.jpg",
243
+ "examples/02.jpg",
244
+ "examples/03.jpg",
245
+ ]
246
+
247
+ # Define the Gradio interface
248
+ with gr.Blocks(css=css) as app:
249
+ with gr.Column(elem_id="col-container"):
250
+ gr.HTML("""
251
+ <div style="text-align: center;">
252
+ <h2>Image to Vector Video Converter ⚡</h2>
253
+ <p>Converts raster images to vector graphics and creates progressive rendering videos.</p>
254
+ </div>
255
+ """)
256
+ with gr.Row():
257
+ with gr.Column():
258
+ image_input = gr.Image(type="pil", label="Upload Image")
259
+ video_duration = gr.Slider(1, 60, value=10, step=1, label="Video Duration (seconds)")
260
+ chunk_size = gr.Slider(0, 1000, value=300, step=1, label="Chunk Size (0=auto)",
261
+ info="Number of SVG path elements to add per frame (0 for automatic calculation)")
262
+
263
+ with gr.Accordion("Advanced Settings", open=False):
264
+ with gr.Accordion("Clustering", open=False):
265
+ colormode = gr.Radio([("COLOR","color"),("B/W", "binary")], value="color", label="Color Mode", show_label=False)
266
+ filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle", info="Cleaner")
267
+ color_precision = gr.Slider(1, 8, value=6, step=1, label="Color Precision", info="More accurate")
268
+ layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step", info="Less layers")
269
+ hierarchical = gr.Radio([("STACKED","stacked"), ("CUTOUT","cutout")], value="stacked", label="Hierarchical Mode",show_label=False)
270
+ with gr.Accordion("Curve Fitting", open=False):
271
+ mode = gr.Radio([("SPLINE","spline"),("POLYGON", "polygon"), ("PIXEL","none")], value="spline", label="Mode", show_label=False)
272
+ corner_threshold = gr.Slider(0, 180, value=60, step=1, label="Corner Threshold", info="Smoother")
273
+ length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.1, label="Segment Length", info ="More coarse")
274
+ splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold", info="Less accurate")
275
+ max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations", visible=False)
276
+ path_precision = gr.Slider(1, 10, value=3, step=1, label="Path Precision", visible=False)
277
+ output_text = gr.Textbox(label="Selected Mode", visible=False)
278
+
279
+ with gr.Row():
280
+ clear_button = gr.Button("Clear")
281
+ convert_button = gr.Button("✨ Convert to Video", variant='primary', elem_classes=["generate-btn"])
282
+
283
+ with gr.Column():
284
+ html = gr.HTML(label="SVG Preview")
285
+ svg_output = gr.File(label="Download SVG")
286
+ video_output = gr.Video(label="Rendering Video")
287
+
288
+ gr.Examples(
289
+ examples=examples,
290
+ fn=convert_to_vector_and_video,
291
+ inputs=[image_input],
292
+ outputs=[html, svg_output, video_output],
293
+ cache_examples=False,
294
+ run_on_click=True
295
+ )
296
+
297
+ # Event handlers
298
+ colormode.change(handle_color_mode, inputs=colormode, outputs=output_text)
299
+ hierarchical.change(handle_color_mode, inputs=hierarchical, outputs=output_text)
300
+ mode.change(handle_color_mode, inputs=mode, outputs=output_text)
301
+
302
+ colormode.change(
303
+ update_interactivity_and_visibility,
304
+ inputs=[colormode, color_precision, layer_difference],
305
+ outputs=[color_precision, layer_difference, hierarchical]
306
+ )
307
+
308
+ mode.change(
309
+ update_interactivity_and_visibility_for_mode,
310
+ inputs=[mode],
311
+ outputs=[corner_threshold, length_threshold, splice_threshold]
312
+ )
313
+
314
+ clear_button.click(
315
+ clear_inputs,
316
+ outputs=[
317
+ image_input,
318
+ video_duration,
319
+ chunk_size,
320
+ colormode,
321
+ hierarchical,
322
+ mode,
323
+ filter_speckle,
324
+ color_precision,
325
+ layer_difference,
326
+ corner_threshold,
327
+ length_threshold,
328
+ max_iterations,
329
+ splice_threshold,
330
+ path_precision
331
+ ]
332
+ )
333
+
334
+ convert_button.click(
335
+ convert_to_vector_and_video,
336
+ inputs=[
337
+ image_input,
338
+ video_duration,
339
+ chunk_size,
340
+ colormode,
341
+ hierarchical,
342
+ mode,
343
+ filter_speckle,
344
+ color_precision,
345
+ layer_difference,
346
+ corner_threshold,
347
+ length_threshold,
348
+ max_iterations,
349
+ splice_threshold,
350
+ path_precision
351
+ ],
352
+ outputs=[html, svg_output, video_output]
353
+ )
354
+
355
+ # Launch the app
356
+ if __name__ == "__main__":
357
+ app.launch(share=True)