Spaces:
Runtime error
Runtime error
add example image
Browse files- app.py +55 -41
- assets/cat-3038243_1280.jpg +0 -0
- assets/input_zip.png +0 -0
- assets/objects/cat-3038243_1280.png +0 -0
- assets/output_zip.png +0 -0
app.py
CHANGED
@@ -5,17 +5,22 @@ from matplotlib import pyplot as plt
|
|
5 |
import os, shutil
|
6 |
|
7 |
import gradio as gr
|
8 |
-
from PIL import Image
|
9 |
import sys, os
|
10 |
from rembg import remove
|
11 |
|
12 |
|
13 |
def run_rembg(img):
|
14 |
output = remove(img)
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def load_zip(inputs):
|
18 |
-
os.makedirs("
|
19 |
|
20 |
image_data_dict = {}
|
21 |
with ZipFile(inputs[0].name, "r") as zip_file:
|
@@ -46,18 +51,23 @@ def load_zip(inputs):
|
|
46 |
|
47 |
|
48 |
for image_name, image_data in image_data_dict.items():
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
image_name = image_name.replace("jpg", "png")
|
52 |
-
|
|
|
53 |
|
54 |
-
shutil.make_archive("
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
shutil.rmtree("results")
|
59 |
|
60 |
-
|
|
|
|
|
61 |
|
62 |
|
63 |
|
@@ -73,21 +83,22 @@ if __name__=="__main__":
|
|
73 |
"""
|
74 |
)
|
75 |
with gr.Row():
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
|
92 |
btn = gr.Button("Run!")
|
93 |
|
@@ -105,20 +116,22 @@ if __name__=="__main__":
|
|
105 |
"""
|
106 |
)
|
107 |
with gr.Row():
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
122 |
|
123 |
btn = gr.Button("Run!")
|
124 |
|
@@ -138,7 +151,8 @@ if __name__=="__main__":
|
|
138 |
- Cloned on 2023/3/12
|
139 |
- Algorithm
|
140 |
- Library Git hub : [U<sup>2</sup>-Net](https://github.com/xuebinqin/U-2-Net)
|
141 |
-
|
|
|
142 |
"""
|
143 |
)
|
144 |
|
|
|
5 |
import os, shutil
|
6 |
|
7 |
import gradio as gr
|
8 |
+
from PIL import Image, ImageOps
|
9 |
import sys, os
|
10 |
from rembg import remove
|
11 |
|
12 |
|
13 |
def run_rembg(img):
|
14 |
output = remove(img)
|
15 |
+
output_pil = Image.fromarray(output)
|
16 |
+
|
17 |
+
# Remove margins
|
18 |
+
cropped_image = output_pil.crop(output_pil.getbbox())
|
19 |
+
|
20 |
+
return cropped_image
|
21 |
|
22 |
def load_zip(inputs):
|
23 |
+
os.makedirs("objects", exist_ok=True)
|
24 |
|
25 |
image_data_dict = {}
|
26 |
with ZipFile(inputs[0].name, "r") as zip_file:
|
|
|
51 |
|
52 |
|
53 |
for image_name, image_data in image_data_dict.items():
|
54 |
+
output = remove(image_data)
|
55 |
+
output_pil = Image.fromarray(output)
|
56 |
+
# Remove margins
|
57 |
+
cropped_image = output_pil.crop(output_pil.getbbox())
|
58 |
|
59 |
image_name = image_name.replace("jpg", "png")
|
60 |
+
cropped_image.save(f"objects/{image_name}")
|
61 |
+
# plt.imsave(f"objects/{image_name}", img_array)
|
62 |
|
63 |
+
shutil.make_archive("objects", "zip", "objects")
|
64 |
+
# ここはresults
|
65 |
+
with ZipFile("results.zip", "w") as zip_file:
|
66 |
+
zip_file.write("objects.zip")
|
|
|
67 |
|
68 |
+
shutil.rmtree("objects")
|
69 |
+
# ここはresults
|
70 |
+
return "results.zip"
|
71 |
|
72 |
|
73 |
|
|
|
83 |
"""
|
84 |
)
|
85 |
with gr.Row():
|
86 |
+
gr.Markdown(
|
87 |
+
"""
|
88 |
+
### Input Zip File
|
89 |
+
<img src='file/assets/input_zip.png' width="85%" height="85%">
|
90 |
+
"""
|
91 |
+
)
|
92 |
+
gr.Markdown(
|
93 |
+
"""
|
94 |
+
### Output Zip File
|
95 |
+
<img src='file/assets/output_zip.png' width="85%" height="85%">
|
96 |
+
"""
|
97 |
+
)
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
image_input = gr.File(file_count="multiple")
|
101 |
+
image_output = gr.File()
|
102 |
|
103 |
btn = gr.Button("Run!")
|
104 |
|
|
|
116 |
"""
|
117 |
)
|
118 |
with gr.Row():
|
119 |
+
gr.Markdown(
|
120 |
+
"""
|
121 |
+
### Input Image
|
122 |
+
<img src='file/assets/cat-3038243_1280.jpg' width="50%" height="50%">
|
123 |
+
"""
|
124 |
+
)
|
125 |
+
|
126 |
+
gr.Markdown(
|
127 |
+
"""
|
128 |
+
### Output Image
|
129 |
+
<img src='file/assets/objects/cat-3038243_1280.png' width="20%" height="20%">
|
130 |
+
"""
|
131 |
+
)
|
132 |
+
with gr.Row():
|
133 |
+
image_input = gr.Image(type="numpy")
|
134 |
+
image_output = gr.Image(type="pil")
|
135 |
|
136 |
btn = gr.Button("Run!")
|
137 |
|
|
|
151 |
- Cloned on 2023/3/12
|
152 |
- Algorithm
|
153 |
- Library Git hub : [U<sup>2</sup>-Net](https://github.com/xuebinqin/U-2-Net)
|
154 |
+
- Image
|
155 |
+
- Cat Image from [Pixabay](https://pixabay.com/images/id-3038243/)
|
156 |
"""
|
157 |
)
|
158 |
|
assets/cat-3038243_1280.jpg
ADDED
![]() |
assets/input_zip.png
ADDED
![]() |
assets/objects/cat-3038243_1280.png
ADDED
![]() |
assets/output_zip.png
ADDED
![]() |