AkashKumarave commited on
Commit
1a1b45c
·
verified ·
1 Parent(s): 8052991

Upload 15 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,13 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ examples/01.jpg filter=lfs diff=lfs merge=lfs -text
37
+ examples/02.jpg filter=lfs diff=lfs merge=lfs -text
38
+ examples/03.jpg filter=lfs diff=lfs merge=lfs -text
39
+ examples/04.jpg filter=lfs diff=lfs merge=lfs -text
40
+ examples/05.jpg filter=lfs diff=lfs merge=lfs -text
41
+ examples/06.jpg filter=lfs diff=lfs merge=lfs -text
42
+ examples/07.png filter=lfs diff=lfs merge=lfs -text
43
+ examples/08.jpg filter=lfs diff=lfs merge=lfs -text
44
+ examples/09.jpg filter=lfs diff=lfs merge=lfs -text
45
+ examples/10.jpg filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Remove Background Web
3
- emoji: 🖼️
4
- colorFrom: red
5
- colorTo: yellow
6
- sdk: static
7
- pinned: false
8
- models:
9
- - briaai/RMBG-1.4
10
- short_description: In-browser background removal
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Image To Vector
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.25.2
8
+ app_file: app.py
9
+ pinned: true
10
+ short_description: Vectorizer AI | Convert Image to SVG
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,27 +1,198 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
- from PIL import Image
4
-
5
- # Função para remover o background da imagem
6
- def remove_background(image):
7
- pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
8
-
9
- # Obter a máscara da imagem
10
- pillow_mask = pipe(image, return_mask=True)
11
-
12
- # Aplicar máscara na imagem original
13
- pillow_image = pipe(image)
14
-
15
- return pillow_image
16
-
17
- # Criar uma interface Gradio
18
- app = gr.Interface(
19
- fn=remove_background,
20
- inputs=gr.components.Image(type="pil"),
21
- outputs=gr.components.Image(type="pil", format="png"), # Especificar saída como PNG
22
- title="Remoção de Background de Imagens",
23
- description="Envie uma imagem e veja o background sendo removido automaticamente. A imagem resultante será no formato PNG."
24
- )
25
-
26
- if __name__ == "__main__":
27
- app.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import vtracer
3
+ import os
4
+
5
+ def convert_to_vector(
6
+ image,
7
+ colormode="color",
8
+ hierarchical="stacked",
9
+ mode="spline",
10
+ filter_speckle=4,
11
+ color_precision=6,
12
+ layer_difference=16,
13
+ corner_threshold=60,
14
+ length_threshold=4.0,
15
+ max_iterations=10,
16
+ splice_threshold=45,
17
+ path_precision=3
18
+ ):
19
+ input_path = "temp_input.jpg"
20
+ output_path = "svg_output.svg"
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
+ output_path,
29
+ colormode=colormode,
30
+ hierarchical=hierarchical,
31
+ mode=mode,
32
+ filter_speckle=int(filter_speckle),
33
+ color_precision=int(color_precision),
34
+ layer_difference=int(layer_difference),
35
+ corner_threshold=int(corner_threshold),
36
+ length_threshold=float(length_threshold),
37
+ max_iterations=int(max_iterations),
38
+ splice_threshold=int(splice_threshold),
39
+ path_precision=int(path_precision)
40
+ )
41
+ # Read the SVG output
42
+ with open(output_path, "r") as f:
43
+ svg_content = f.read()
44
+
45
+ # Return the SVG file path instead of content
46
+ return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'), output_path
47
+ # return output_path,output_path
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
+ examples = [
55
+ "examples/11.jpg",
56
+ "examples/02.jpg",
57
+ "examples/03.jpg",
58
+ ]
59
+
60
+ css = """
61
+ #col-container {
62
+ margin: 0 auto;
63
+ max-width: 960px;
64
+ }
65
+ .generate-btn {
66
+ background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
67
+ border: none !important;
68
+ color: white !important;
69
+ }
70
+ .generate-btn:hover {
71
+ transform: translateY(-2px);
72
+ box-shadow: 0 5px 15px rgba(0,0,0,0.2);
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
+ <div style="text-align: center;">
81
+ <h2>Image to Vector Converter ⚡</h2>
82
+ <p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG).</p>
83
+ </div>
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)
91
+ filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle", info="Cleaner")
92
+ color_precision = gr.Slider(1, 8, value=6, step=1, label="Color Precision", info="More accurate")
93
+ layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step", info="Less layers")
94
+ hierarchical = gr.Radio([("STACKED","stacked"), ("CUTOUT","cutout")], value="stacked", label="Hierarchical Mode",show_label=False)
95
+ with gr.Accordion("Curve Fitting", open=False):
96
+ mode = gr.Radio([("SPLINE","spline"),("POLYGON", "polygon"), ("PIXEL","none")], value="spline", label="Mode", show_label=False)
97
+ corner_threshold = gr.Slider(0, 180, value=60, step=1, label="Corner Threshold", info="Smoother")
98
+ length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.1, label="Segment Length", info ="More coarse")
99
+ splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold", info="Less accurate")
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 SVG", variant='primary', elem_classes=["generate-btn"])
106
+
107
+ with gr.Column():
108
+ html = gr.HTML(label="SVG Output") # container=True, show_label=True
109
+ svg_output = gr.File(label="Download SVG")
110
+
111
+ gr.Examples(
112
+ examples = examples,
113
+ fn = convert_to_vector,
114
+ inputs = [image_input],
115
+ outputs = [html,svg_output],
116
+ cache_examples=False,
117
+ run_on_click = True
118
+ )
119
+ # Store default values for restoration
120
+ colormode.change(handle_color_mode, inputs=colormode,outputs=output_text)
121
+ hierarchical.change(handle_color_mode, inputs=hierarchical,outputs=output_text)
122
+ mode.change(handle_color_mode, inputs=mode,outputs=output_text)
123
+ default_values = {
124
+ "color_precision": 6,
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,
167
+ filter_speckle,
168
+ color_precision,
169
+ layer_difference,
170
+ corner_threshold,
171
+ length_threshold,
172
+ max_iterations,
173
+ splice_threshold,
174
+ path_precision
175
+ ]
176
+ )
177
+
178
+ convert_button.click(
179
+ convert_to_vector,
180
+ inputs=[
181
+ image_input,
182
+ colormode,
183
+ hierarchical,
184
+ mode,
185
+ filter_speckle,
186
+ color_precision,
187
+ layer_difference,
188
+ corner_threshold,
189
+ length_threshold,
190
+ max_iterations,
191
+ splice_threshold,
192
+ path_precision
193
+ ],
194
+ outputs=[html,svg_output]
195
+ )
196
+
197
+ # Launch the app
198
+ app.launch(debug=True)
examples/01.jpg ADDED

Git LFS Details

  • SHA256: bee019b6ebc352a12c82bfa6708f843f76e8618227707c33688a70f679754cd1
  • Pointer size: 131 Bytes
  • Size of remote file: 342 kB
examples/02.jpg ADDED

Git LFS Details

  • SHA256: 9870a9487cc72baade1b9a775f4a6ada849744c4d7068a7d0eca881573e37614
  • Pointer size: 131 Bytes
  • Size of remote file: 303 kB
examples/03.jpg ADDED

Git LFS Details

  • SHA256: f93e8919f2f284746deb83854b283d58b8a1a6dd6bf920cbfff25ab8d21ed3db
  • Pointer size: 131 Bytes
  • Size of remote file: 415 kB
examples/04.jpg ADDED

Git LFS Details

  • SHA256: d9a8f7adcff3d6d6da8571a1de657586a1c704724fc51ea46d75a670227b674d
  • Pointer size: 131 Bytes
  • Size of remote file: 361 kB
examples/05.jpg ADDED

Git LFS Details

  • SHA256: 220f112a0099d321e92a65c1e83b43b175676c53e8748953c613d66c71e53e66
  • Pointer size: 131 Bytes
  • Size of remote file: 486 kB
examples/06.jpg ADDED

Git LFS Details

  • SHA256: f5ea3ae998b4553b042dc982ebdb1e22a571b62bd2b6da1c6590d46e81653657
  • Pointer size: 131 Bytes
  • Size of remote file: 266 kB
examples/07.png ADDED

Git LFS Details

  • SHA256: 9418f6ebe2a3032b8e1424eacacc3393635ff48b4f140dfa9a7e3247bf0693b4
  • Pointer size: 131 Bytes
  • Size of remote file: 238 kB
examples/08.jpg ADDED

Git LFS Details

  • SHA256: 524e8e7cfdd1706277a5bbba448c41880654b3cf10aed1b368e08d458527f60c
  • Pointer size: 131 Bytes
  • Size of remote file: 128 kB
examples/09.jpg ADDED

Git LFS Details

  • SHA256: db4422cd6541b38dc9b9395cb9904390ea2f8798f218ccc9b7444405d0fd497c
  • Pointer size: 131 Bytes
  • Size of remote file: 152 kB
examples/10.jpg ADDED

Git LFS Details

  • SHA256: b75967e869b11c0672c99ea01a1d6cb60aae3bfd52c6815a66d92d6fc298f56f
  • Pointer size: 131 Bytes
  • Size of remote file: 157 kB
examples/11.jpg ADDED
requirements.txt CHANGED
@@ -1,5 +1 @@
1
- gradio
2
- transformers
3
- Pillow
4
- torchvision
5
- scikit-image
 
1
+ vtracer