Spaces:
Runtime error
Runtime error
承弱
commited on
Commit
•
f97c615
1
Parent(s):
d41e710
init update
Browse files- .gitattributes +15 -11
- .gitignore +3 -0
- app.py +145 -0
- demo/gallery/20240726130056.png +3 -0
- demo/gallery/20240726141609.png +3 -0
- demo/gallery/20240727155807.png +3 -0
- demo/gallery/20240729171429.png +3 -0
- demo/gallery/20240729175335.png +3 -0
- demo/gallery/20240801104535.png +3 -0
- demo/gallery/20240826144458.png +3 -0
- demo/gallery/20240826222659.png +3 -0
- demo/gallery/20240827104724.png +3 -0
- demo/gallery/20240827115328.png +3 -0
- demo/gallery/20240828111412.png +3 -0
- demo/gallery/20240828130156.png +3 -0
- demo/gallery/20240828130319.png +3 -0
- demo/gallery/20240828143709.png +3 -0
- demo/gallery/20240828143748.png +3 -0
- demo/generation.py +131 -0
- demo/instructions.py +31 -0
- demo/log.py +18 -0
- demo/util.py +104 -0
- test_imgs/demo1.png +3 -0
- test_imgs/demo10.png +3 -0
- test_imgs/demo11.png +3 -0
- test_imgs/demo12.png +3 -0
- test_imgs/demo2.jpg +3 -0
- test_imgs/demo3.png +3 -0
- test_imgs/demo4.png +3 -0
- test_imgs/demo5.png +3 -0
- test_imgs/demo6.png +3 -0
- test_imgs/demo7.png +3 -0
- test_imgs/demo8.png +3 -0
- test_imgs/demo9.png +3 -0
.gitattributes
CHANGED
@@ -1,35 +1,39 @@
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
4 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
|
|
6 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
|
|
11 |
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
13 |
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
*.pb filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
17 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
|
|
20 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
21 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
|
|
22 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
|
|
24 |
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.db* filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.ark* filter=lfs diff=lfs merge=lfs -text
|
30 |
+
**/*ckpt*data* filter=lfs diff=lfs merge=lfs -text
|
31 |
+
**/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text
|
32 |
+
**/*ckpt*.index filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*.gguf* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.ggml filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.llamafile* filter=lfs diff=lfs merge=lfs -text
|
38 |
+
test_imgs/* filter=lfs diff=lfs merge=lfs -text
|
39 |
+
demo/gallery/* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
demo/__pycache__
|
2 |
+
workdir/
|
3 |
+
.DS_Store
|
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2, os
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from demo.generation import call_generation
|
5 |
+
from demo.instructions import INSTRUCTIONS_VECTORIZE_SIMPLIFY
|
6 |
+
|
7 |
+
VERSION = 'v0.1'
|
8 |
+
|
9 |
+
GALLERY_LIST = [os.path.join('demo/gallery',path) for path in os.listdir('demo/gallery')]
|
10 |
+
|
11 |
+
|
12 |
+
def resize_image(image, size):
|
13 |
+
# find the minimal size of the image, resize it to size
|
14 |
+
# H, W, C = image.shape
|
15 |
+
return cv2.resize(image, (size[0], size[1]), interpolation=cv2.INTER_LINEAR)
|
16 |
+
|
17 |
+
def HWC3(x):
|
18 |
+
assert x.dtype == np.uint8
|
19 |
+
if x.ndim == 2:
|
20 |
+
x = x[:, :, None]
|
21 |
+
assert x.ndim == 3
|
22 |
+
H, W, C = x.shape
|
23 |
+
assert C == 1 or C == 3 or C == 4
|
24 |
+
if C == 3:
|
25 |
+
return x
|
26 |
+
if C == 1:
|
27 |
+
return np.concatenate([x, x, x], axis=2)
|
28 |
+
if C == 4:
|
29 |
+
color = x[:, :, 0:3].astype(np.float32)
|
30 |
+
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
|
31 |
+
y = color * alpha + 255.0 * (1.0 - alpha)
|
32 |
+
y = y.clip(0, 255).astype(np.uint8)
|
33 |
+
return y
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
def process_vector(input_image, upsample_method, svg_simplify, svg_optimize, trace_mode, subsample_ratio, speckle_removal,sorting_method, sorting_order, use_gpu):
|
39 |
+
print("Processing vector:",upsample_method, svg_simplify, svg_optimize, trace_mode)
|
40 |
+
if input_image is not None:
|
41 |
+
## save input_image to a temp file
|
42 |
+
|
43 |
+
## process the image
|
44 |
+
file_list = call_generation(input_image,
|
45 |
+
preprocess=upsample_method,
|
46 |
+
simplify=svg_simplify,
|
47 |
+
optimize=svg_optimize,
|
48 |
+
mode=trace_mode,
|
49 |
+
subsample_ratio=subsample_ratio,
|
50 |
+
speckle_removal=speckle_removal,
|
51 |
+
sorting_method=sorting_method,
|
52 |
+
sorting_order=sorting_order,
|
53 |
+
use_gpu=use_gpu)
|
54 |
+
|
55 |
+
return file_list
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
block = gr.Blocks(
|
62 |
+
title = "VectorizeAnything",
|
63 |
+
theme=gr.themes.Soft(
|
64 |
+
radius_size=gr.themes.sizes.radius_none,
|
65 |
+
text_size=gr.themes.sizes.text_md
|
66 |
+
),
|
67 |
+
css="css/style.css",
|
68 |
+
).queue()
|
69 |
+
|
70 |
+
with block:
|
71 |
+
state = gr.State(value={
|
72 |
+
'gallery_selected_img_path': None, # 当前选中的图片路径
|
73 |
+
'gallery_selected_img_path_idx': 0, # 当前选中的图片路径索引
|
74 |
+
})
|
75 |
+
with gr.Row():
|
76 |
+
gr.HTML(f"""
|
77 |
+
</br>
|
78 |
+
<div>
|
79 |
+
<h1 style="font-size:3rem; "><center>Vectorize Anything: {VERSION} </center></h1>
|
80 |
+
</div>
|
81 |
+
</br>
|
82 |
+
""")
|
83 |
+
# tab_0 = gr.Tab(label="Gallery (画廊)")
|
84 |
+
# with tab_0:
|
85 |
+
# with gr.Row():
|
86 |
+
# gr.Gallery(label='图像生成结果', value=GALLERY_LIST,show_label=False, elem_id="Gallery", columns=5, height=1000)
|
87 |
+
|
88 |
+
|
89 |
+
tab_3 = gr.Tab(label="IMG to SVG")
|
90 |
+
with tab_3:
|
91 |
+
with gr.Accordion('🕹Usage', open=True,):
|
92 |
+
with gr.Tabs():
|
93 |
+
gr.HTML(INSTRUCTIONS_VECTORIZE_SIMPLIFY)
|
94 |
+
with gr.Row():
|
95 |
+
with gr.Column():
|
96 |
+
input_image = gr.Image(type="numpy")
|
97 |
+
run_vectorize = gr.Button(value="Vectorize",elem_id="btnVEC")
|
98 |
+
|
99 |
+
with gr.Accordion("Vector options", open=True):
|
100 |
+
upsample_method = gr.Dropdown(choices=["None", "x4", "x2"], type="value", value="None", label="Upsample Method")
|
101 |
+
sorting_method = gr.Dropdown(choices=["brightness","area"], type="value", value="brightness", label="Sorting Method")
|
102 |
+
sorting_order = gr.Dropdown(choices=["ascend","descend"], type="value", value="descend", label="Sorting Order")
|
103 |
+
trace_mode = gr.Radio(choices=["overlap", "cutout"], type="value", value="overlap", label="Trace Mode")
|
104 |
+
use_gpu = gr.Checkbox(label='use GPU', value=False, visible=True)
|
105 |
+
svg_simplify = gr.Checkbox(label='Simplify SVG', value=False, visible=True)
|
106 |
+
svg_optimize = gr.Checkbox(label='Optimize SVG', value=False, visible=True)
|
107 |
+
speckle_removal = gr.Checkbox(label='Remove small speckle', value=False)
|
108 |
+
subsample_ratio = gr.Slider(label="Subsample Ratio", minimum=1, maximum=10000, value=12, step=1, visible=False)
|
109 |
+
|
110 |
+
|
111 |
+
def exp_gen_click():
|
112 |
+
return [gr.Slider(value=512), gr.Slider(value=512)] # all examples are 512x512, refresh draw_img
|
113 |
+
|
114 |
+
with gr.Column():
|
115 |
+
result_vector_gallery = gr.Gallery(label='Output', show_label=False, elem_id="Gallery_vector")
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
with gr.Tab("Image Examples"):
|
120 |
+
exp_gen_en = gr.Examples(
|
121 |
+
[
|
122 |
+
["test_imgs/demo1.png"],
|
123 |
+
["test_imgs/demo2.jpg"],
|
124 |
+
["test_imgs/demo3.png"],
|
125 |
+
["test_imgs/demo4.png"],
|
126 |
+
["test_imgs/demo5.png"],
|
127 |
+
["test_imgs/demo6.png"],
|
128 |
+
["test_imgs/demo7.png"],
|
129 |
+
["test_imgs/demo8.png"],
|
130 |
+
["test_imgs/demo9.png"],
|
131 |
+
["test_imgs/demo10.png"],
|
132 |
+
["test_imgs/demo11.png"],
|
133 |
+
["test_imgs/demo12.png"],
|
134 |
+
],
|
135 |
+
[input_image],
|
136 |
+
examples_per_page=20,
|
137 |
+
label=''
|
138 |
+
)
|
139 |
+
exp_gen_en.dataset.click(exp_gen_click, None)
|
140 |
+
|
141 |
+
|
142 |
+
vector_ips = [input_image, upsample_method, svg_simplify, svg_optimize, trace_mode, subsample_ratio, speckle_removal,sorting_method, sorting_order, use_gpu]
|
143 |
+
run_vectorize.click(fn=process_vector, inputs=vector_ips, outputs=result_vector_gallery)
|
144 |
+
|
145 |
+
block.launch(server_name='0.0.0.0', share=False,debug=True, root_path=f"/{os.getenv('GRADIO_PROXY_PATH')}" if os.getenv('GRADIO_PROXY_PATH') else "")
|
demo/gallery/20240726130056.png
ADDED
Git LFS Details
|
demo/gallery/20240726141609.png
ADDED
Git LFS Details
|
demo/gallery/20240727155807.png
ADDED
Git LFS Details
|
demo/gallery/20240729171429.png
ADDED
Git LFS Details
|
demo/gallery/20240729175335.png
ADDED
Git LFS Details
|
demo/gallery/20240801104535.png
ADDED
Git LFS Details
|
demo/gallery/20240826144458.png
ADDED
Git LFS Details
|
demo/gallery/20240826222659.png
ADDED
Git LFS Details
|
demo/gallery/20240827104724.png
ADDED
Git LFS Details
|
demo/gallery/20240827115328.png
ADDED
Git LFS Details
|
demo/gallery/20240828111412.png
ADDED
Git LFS Details
|
demo/gallery/20240828130156.png
ADDED
Git LFS Details
|
demo/gallery/20240828130319.png
ADDED
Git LFS Details
|
demo/gallery/20240828143709.png
ADDED
Git LFS Details
|
demo/gallery/20240828143748.png
ADDED
Git LFS Details
|
demo/generation.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import requests
|
7 |
+
|
8 |
+
from demo.log import logger
|
9 |
+
from demo.util import download_svgs, upload_np_2_oss
|
10 |
+
|
11 |
+
API_KEY = os.getenv("API_KEY_GENERATION")
|
12 |
+
|
13 |
+
def convert_bool_to_str(value):
|
14 |
+
if value:
|
15 |
+
return "True"
|
16 |
+
else:
|
17 |
+
return "False"
|
18 |
+
|
19 |
+
def call_generation(input_path,
|
20 |
+
preprocess,
|
21 |
+
simplify,
|
22 |
+
optimize,
|
23 |
+
mode,
|
24 |
+
subsample_ratio,
|
25 |
+
speckle_removal,
|
26 |
+
sorting_method,
|
27 |
+
sorting_order,
|
28 |
+
use_gpu):
|
29 |
+
|
30 |
+
## generate image name based on time stamp
|
31 |
+
time_str = time.strftime("%Y%m%d%H%M%S", time.localtime())
|
32 |
+
img_name = f"upload_{time_str}.png"
|
33 |
+
svg_name = f"result_{time_str}"
|
34 |
+
BATCH_SIZE = 1
|
35 |
+
if simplify:
|
36 |
+
BATCH_SIZE += 1
|
37 |
+
if optimize:
|
38 |
+
BATCH_SIZE += 1
|
39 |
+
img_url = upload_np_2_oss(input_path, name=img_name)
|
40 |
+
simplify = convert_bool_to_str(simplify)
|
41 |
+
optimize = convert_bool_to_str(optimize)
|
42 |
+
speckle_removal = convert_bool_to_str(speckle_removal)
|
43 |
+
use_gpu = convert_bool_to_str(use_gpu)
|
44 |
+
headers = {
|
45 |
+
"Content-Type": "application/json",
|
46 |
+
"Accept": "application/json",
|
47 |
+
"Authorization": f"Bearer {API_KEY}",
|
48 |
+
"X-DashScope-Async": "enable",
|
49 |
+
}
|
50 |
+
data = {
|
51 |
+
"model": "pre-vectorize_anything-2333",
|
52 |
+
"input": {
|
53 |
+
"base_image_url": img_url
|
54 |
+
},
|
55 |
+
"parameters":{
|
56 |
+
"preprocess": preprocess,
|
57 |
+
"mode": mode,
|
58 |
+
"simplify": simplify,
|
59 |
+
"optimize": optimize,
|
60 |
+
"sorting_method": sorting_method,
|
61 |
+
"sorting_order": sorting_order,
|
62 |
+
"subsample_ratio": subsample_ratio,
|
63 |
+
"speckle_removal": speckle_removal,
|
64 |
+
"use_GPU": use_gpu
|
65 |
+
}
|
66 |
+
}
|
67 |
+
url_create_task = 'https://poc-dashscope.aliyuncs.com/api/v1/services/vision/image-process/process'
|
68 |
+
|
69 |
+
all_res_ = []
|
70 |
+
REPEAT = 1
|
71 |
+
for _ in range(REPEAT):
|
72 |
+
try:
|
73 |
+
res_ = requests.post(url_create_task, data=json.dumps(data), headers=headers, timeout=60)
|
74 |
+
print(json.dumps(data))
|
75 |
+
all_res_.append(res_)
|
76 |
+
except requests.Timeout:
|
77 |
+
# back off and retry
|
78 |
+
raise gr.Error("网络波动,请求失败,请再次尝试")
|
79 |
+
|
80 |
+
all_image_data = []
|
81 |
+
for res_ in all_res_:
|
82 |
+
respose_code = res_.status_code
|
83 |
+
if 200 == respose_code:
|
84 |
+
res = json.loads(res_.content.decode())
|
85 |
+
request_id = res['request_id']
|
86 |
+
task_id = res['output']['task_id']
|
87 |
+
logger.info(f"task_id: {task_id}: Create Poster Imitation request success. Params: {data}")
|
88 |
+
|
89 |
+
# 异步查询
|
90 |
+
is_running = True
|
91 |
+
while is_running:
|
92 |
+
# url_query = f'https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}'
|
93 |
+
url_query = f'https://poc-dashscope.aliyuncs.com/api/v1/tasks/{task_id}'
|
94 |
+
try:
|
95 |
+
res_ = requests.post(url_query, headers=headers, timeout=60)
|
96 |
+
except requests.Timeout:
|
97 |
+
# back off and retry
|
98 |
+
raise gr.Error("网络波动,请求失败,请再次尝试")
|
99 |
+
respose_code = res_.status_code
|
100 |
+
if 200 == respose_code:
|
101 |
+
res = json.loads(res_.content.decode())
|
102 |
+
if "SUCCEEDED" == res['output']['task_status']:
|
103 |
+
logger.info(f"task_id: {task_id}: Generation task query success.")
|
104 |
+
results = res['output']
|
105 |
+
img_urls = results['output_img']
|
106 |
+
logger.info(f"task_id: {task_id}: {res}")
|
107 |
+
break
|
108 |
+
elif "FAILED" != res['output']['task_status']:
|
109 |
+
logger.debug(f"task_id: {task_id}: query result...")
|
110 |
+
time.sleep(1)
|
111 |
+
else:
|
112 |
+
raise gr.Error('Fail to get results from Generation task.')
|
113 |
+
|
114 |
+
else:
|
115 |
+
logger.error(f'task_id: {task_id}: Fail to query task result: {res_.content}')
|
116 |
+
raise gr.Error("Fail to query task result.")
|
117 |
+
|
118 |
+
logger.info(f"task_id: {task_id}: download generated images.")
|
119 |
+
img_data = download_svgs(img_urls, BATCH_SIZE, svg_name)
|
120 |
+
logger.info(f"task_id: {task_id}: Generate done.")
|
121 |
+
all_image_data += img_data
|
122 |
+
else:
|
123 |
+
logger.error(f'Fail to create Generation task: {res_.content}')
|
124 |
+
raise gr.Error("Fail to create Generation task.")
|
125 |
+
|
126 |
+
if len(all_image_data) != REPEAT * BATCH_SIZE:
|
127 |
+
raise gr.Error("Fail to Generation.")
|
128 |
+
return all_image_data[-1:]
|
129 |
+
|
130 |
+
if __name__ == "__main__":
|
131 |
+
call_generation()
|
demo/instructions.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
INSTRUCTIONS_VECTORIZE_SIMPLIFY='''
|
2 |
+
<style>
|
3 |
+
body {
|
4 |
+
font-family: Arial, sans-serif;
|
5 |
+
line-height: 1.6;
|
6 |
+
margin: 20px;
|
7 |
+
}
|
8 |
+
ul {
|
9 |
+
list-style-type: disc;
|
10 |
+
margin-left: 20px;
|
11 |
+
}
|
12 |
+
</style>
|
13 |
+
|
14 |
+
<body>
|
15 |
+
|
16 |
+
<p>Step1: 上传一个需要矢量化的图片 Upload a image to vectorize</p>
|
17 |
+
|
18 |
+
<p>Step2: 调整矢量化参数 Adjust the parameters</p>
|
19 |
+
<ul>
|
20 |
+
<li>使用 GPU 加速 (GPU Accelerate): 对于颜色复杂的图形, 建议使用GPU加速</li>
|
21 |
+
<li>超分倍数 (Super Resolution Scale): suggested for small image 对于小图片, 可以选择 2-4 倍, 可提升矢量化效果</li>
|
22 |
+
<li>简化矢量图 (Simplify SVG): reduce control points number 是否简化矢量图, 可减少控制点数量</li>
|
23 |
+
<li>优化矢量图 (Optimize SVG): optimize svg to make it closer to input 是否优化矢量图, 可使得矢量图更接近原图,推荐配合cutout模式使用,可减少图形之间空隙</li>
|
24 |
+
<li>小面积图形 (Small Speckles): remove small areas 是否移除小面积图形</li>
|
25 |
+
<li>排序方法 (Sorting Method): sort shape in lightness or area 色块排序方式, 可以选择按亮度排序或者面积排序</li>
|
26 |
+
<li>排序顺序 (Sorting Order): sort in ascend or descend 从大到小或者从小到大</li>
|
27 |
+
<li>追踪模式 (Trace Mode): trace in cutout or overlap mode 路径追踪模式, 可以选择 Cutout 或者 Overlap。Cutout 模式图形没有重叠,Overlap 模式图形会根据排序方法和顺序进行重叠</li>
|
28 |
+
</ul>
|
29 |
+
<p>Step3: 点击转化矢量图 Press the button to vectorize</p>
|
30 |
+
</body>
|
31 |
+
'''
|
demo/log.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
from logging.handlers import RotatingFileHandler
|
4 |
+
|
5 |
+
log_file_name = "workdir/log_vectorize_anything.log"
|
6 |
+
os.makedirs(os.path.dirname(log_file_name), exist_ok=True)
|
7 |
+
|
8 |
+
format = '[%(levelname)s] %(asctime)s "%(filename)s", line %(lineno)d, %(message)s'
|
9 |
+
logging.basicConfig(
|
10 |
+
format=format,
|
11 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
12 |
+
level=logging.INFO)
|
13 |
+
logger = logging.getLogger(name="WordArt_Studio")
|
14 |
+
|
15 |
+
fh = RotatingFileHandler(log_file_name, maxBytes=20000000, backupCount=3)
|
16 |
+
formatter = logging.Formatter(format, datefmt="%Y-%m-%d %H:%M:%S")
|
17 |
+
fh.setFormatter(formatter)
|
18 |
+
logger.addHandler(fh)
|
demo/util.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import concurrent.futures
|
2 |
+
import io
|
3 |
+
import os
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import oss2
|
7 |
+
import requests
|
8 |
+
from PIL import Image, ImageDraw, ImageFont
|
9 |
+
|
10 |
+
from .log import logger
|
11 |
+
|
12 |
+
# oss
|
13 |
+
access_key_id = os.getenv("ACCESS_KEY_ID")
|
14 |
+
access_key_secret = os.getenv("ACCESS_KEY_SECRET")
|
15 |
+
bucket_name = os.getenv("BUCKET_NAME")
|
16 |
+
endpoint = os.getenv("ENDPOINT")
|
17 |
+
|
18 |
+
oss_path = "VectorizeAnything"
|
19 |
+
oss_path_img_gallery = "VectorizeAnything"
|
20 |
+
|
21 |
+
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
|
22 |
+
|
23 |
+
def download_img_pil(index, img_url):
|
24 |
+
# print(img_url)
|
25 |
+
r = requests.get(img_url, stream=True)
|
26 |
+
if r.status_code == 200:
|
27 |
+
img = Image.open(io.BytesIO(r.content))
|
28 |
+
return (index, img)
|
29 |
+
else:
|
30 |
+
logger.error(f"Fail to download: {img_url}")
|
31 |
+
|
32 |
+
|
33 |
+
def download_images(img_urls, batch_size):
|
34 |
+
imgs_pil = [None] * batch_size
|
35 |
+
# worker_results = []
|
36 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
|
37 |
+
to_do = []
|
38 |
+
for i, url in enumerate(img_urls):
|
39 |
+
future = executor.submit(download_img_pil, i, url)
|
40 |
+
to_do.append(future)
|
41 |
+
|
42 |
+
for future in concurrent.futures.as_completed(to_do):
|
43 |
+
ret = future.result()
|
44 |
+
# worker_results.append(ret)
|
45 |
+
index, img_pil = ret
|
46 |
+
imgs_pil[index] = img_pil # 按顺序排列url,后续下载关联的图片或者svg需要使用
|
47 |
+
|
48 |
+
return imgs_pil
|
49 |
+
|
50 |
+
def download_svg(index, img_url, svg_name):
|
51 |
+
# print(img_url)
|
52 |
+
r = requests.get(img_url, stream=True)
|
53 |
+
if r.status_code == 200:
|
54 |
+
img = io.BytesIO(r.content)
|
55 |
+
svg_url = upload_svg_2_oss(img, f"{svg_name}_{index}.svg")
|
56 |
+
return (index, svg_url)
|
57 |
+
else:
|
58 |
+
logger.error(f"Fail to download: {img_url}")
|
59 |
+
|
60 |
+
|
61 |
+
def download_svgs(img_urls, batch_size, svg_name):
|
62 |
+
imgs_pil = [None] * batch_size
|
63 |
+
# worker_results = []
|
64 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
|
65 |
+
to_do = []
|
66 |
+
for i, url in enumerate(img_urls):
|
67 |
+
future = executor.submit(download_svg, i, url, svg_name)
|
68 |
+
to_do.append(future)
|
69 |
+
|
70 |
+
for future in concurrent.futures.as_completed(to_do):
|
71 |
+
ret = future.result()
|
72 |
+
# worker_results.append(ret)
|
73 |
+
index, img_pil = ret
|
74 |
+
imgs_pil[index] = img_pil # 按顺序排列url,后续下载关联的图片或者svg需要使用
|
75 |
+
|
76 |
+
return imgs_pil
|
77 |
+
|
78 |
+
def upload_np_2_oss(input_image, name="cache.png", gallery=False):
|
79 |
+
imgByteArr = io.BytesIO()
|
80 |
+
Image.fromarray(input_image).save(imgByteArr, format="PNG")
|
81 |
+
imgByteArr = imgByteArr.getvalue()
|
82 |
+
|
83 |
+
if gallery:
|
84 |
+
path = oss_path_img_gallery
|
85 |
+
else:
|
86 |
+
path = oss_path
|
87 |
+
|
88 |
+
bucket.put_object(path + "/" + name, imgByteArr) # data为数据,可以是图片
|
89 |
+
ret = bucket.sign_url('GET', path + "/" + name, 60 * 60 * 24) # 返回值为链接,参数依次为,方法/oss上文件路径/过期时间(s)
|
90 |
+
del imgByteArr
|
91 |
+
return ret
|
92 |
+
|
93 |
+
|
94 |
+
def upload_svg_2_oss(input_svg, name="cache.png", gallery=False):
|
95 |
+
|
96 |
+
if gallery:
|
97 |
+
path = oss_path_img_gallery
|
98 |
+
else:
|
99 |
+
path = oss_path
|
100 |
+
|
101 |
+
bucket.put_object(path + "/" + name, input_svg) # data为数据,可以是图片
|
102 |
+
ret = bucket.sign_url('GET', path + "/" + name, 60 * 60 * 24) # 返回值为链接,参数依次为,方法/oss上文件路径/过期时间(s)
|
103 |
+
del input_svg
|
104 |
+
return ret
|
test_imgs/demo1.png
ADDED
Git LFS Details
|
test_imgs/demo10.png
ADDED
Git LFS Details
|
test_imgs/demo11.png
ADDED
Git LFS Details
|
test_imgs/demo12.png
ADDED
Git LFS Details
|
test_imgs/demo2.jpg
ADDED
Git LFS Details
|
test_imgs/demo3.png
ADDED
Git LFS Details
|
test_imgs/demo4.png
ADDED
Git LFS Details
|
test_imgs/demo5.png
ADDED
Git LFS Details
|
test_imgs/demo6.png
ADDED
Git LFS Details
|
test_imgs/demo7.png
ADDED
Git LFS Details
|
test_imgs/demo8.png
ADDED
Git LFS Details
|
test_imgs/demo9.png
ADDED
Git LFS Details
|