Spaces:
Runtime error
Runtime error
update
Browse files
app.py
CHANGED
@@ -1,4 +1,9 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
import sys, os
|
@@ -9,34 +14,113 @@ def run_rembg(img):
|
|
9 |
output = remove(img)
|
10 |
return output
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
if __name__=="__main__":
|
14 |
|
15 |
|
16 |
with gr.Blocks() as demo:
|
17 |
|
18 |
-
gr.
|
|
|
19 |
"""
|
20 |
<center><h1>Image Matting using U<sup>2</sup>-Net</h1></center>
|
21 |
"""
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
gr.
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
btn.click(
|
42 |
fn=run_rembg,
|
|
|
1 |
+
from zipfile import ZipFile
|
2 |
+
import numpy as np
|
3 |
+
from matplotlib import pyplot as plt
|
4 |
+
|
5 |
+
import os, shutil
|
6 |
+
|
7 |
import gradio as gr
|
8 |
from PIL import Image
|
9 |
import sys, os
|
|
|
14 |
output = remove(img)
|
15 |
return output
|
16 |
|
17 |
+
def load_zip(inputs):
|
18 |
+
os.makedirs("results", exist_ok=True)
|
19 |
+
|
20 |
+
image_data_dict = {}
|
21 |
+
with ZipFile(inputs[0].name, "r") as zip_file:
|
22 |
+
image_names = zip_file.namelist()
|
23 |
+
|
24 |
+
prefix = ""
|
25 |
+
for name in image_names:
|
26 |
+
if prefix=="":
|
27 |
+
prefix = name.split("/")[0]
|
28 |
+
else:
|
29 |
+
break
|
30 |
+
|
31 |
+
image_files = []
|
32 |
+
|
33 |
+
for image_name in image_names:
|
34 |
+
if image_name[-3:] in "pngjpg":
|
35 |
+
try:
|
36 |
+
with zip_file.open(image_name) as f:
|
37 |
+
image = Image.open(f)
|
38 |
+
image_files.append(image_name)
|
39 |
+
|
40 |
+
image_array = np.array(image)
|
41 |
+
image_name = image_name.split("/")[1]
|
42 |
+
image_data_dict[image_name] = image_array
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
print("Exception : ", e)
|
46 |
+
|
47 |
+
|
48 |
+
for image_name, image_data in image_data_dict.items():
|
49 |
+
img_array = remove(image_data)
|
50 |
+
|
51 |
+
image_name = image_name.replace("jpg", "png")
|
52 |
+
plt.imsave(f"results/{image_name}", img_array)
|
53 |
+
|
54 |
+
shutil.make_archive("results", "zip", "results")
|
55 |
+
with ZipFile("objects.zip", "w") as zip_file:
|
56 |
+
zip_file.write("results.zip")
|
57 |
+
|
58 |
+
shutil.rmtree("results")
|
59 |
+
|
60 |
+
return "objects.zip"
|
61 |
+
|
62 |
+
|
63 |
|
64 |
if __name__=="__main__":
|
65 |
|
66 |
|
67 |
with gr.Blocks() as demo:
|
68 |
|
69 |
+
with gr.Tab("Zip"):
|
70 |
+
gr.Markdown(
|
71 |
"""
|
72 |
<center><h1>Image Matting using U<sup>2</sup>-Net</h1></center>
|
73 |
"""
|
74 |
+
)
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column():
|
77 |
+
gr.Markdown(
|
78 |
+
"""
|
79 |
+
Input Zip File
|
80 |
+
"""
|
81 |
+
)
|
82 |
+
image_input = gr.File(file_count="multiple")
|
83 |
+
|
84 |
+
with gr.Column():
|
85 |
+
gr.Markdown(
|
86 |
+
"""
|
87 |
+
Output Zip File
|
88 |
+
"""
|
89 |
+
)
|
90 |
+
image_output = gr.File()
|
91 |
+
|
92 |
+
btn = gr.Button("Run!")
|
93 |
+
|
94 |
+
btn.click(
|
95 |
+
fn=load_zip,
|
96 |
+
inputs=image_input,
|
97 |
+
outputs=image_output
|
98 |
+
)
|
99 |
+
|
100 |
+
|
101 |
+
with gr.Tab("Image"):
|
102 |
+
gr.Markdown(
|
103 |
+
"""
|
104 |
+
<center><h1>Image Matting using U<sup>2</sup>-Net</h1></center>
|
105 |
+
"""
|
106 |
+
)
|
107 |
+
with gr.Row():
|
108 |
+
with gr.Column():
|
109 |
+
gr.Markdown(
|
110 |
+
"""
|
111 |
+
Input Image
|
112 |
+
"""
|
113 |
+
)
|
114 |
+
image_input = gr.Image(type="numpy")
|
115 |
+
with gr.Column():
|
116 |
+
gr.Markdown(
|
117 |
+
"""
|
118 |
+
Output Image
|
119 |
+
"""
|
120 |
+
)
|
121 |
+
image_output = gr.Image(type="numpy")
|
122 |
+
|
123 |
+
btn = gr.Button("Run!")
|
124 |
|
125 |
btn.click(
|
126 |
fn=run_rembg,
|