Spaces:
Runtime error
Runtime error
update working directory
Browse files
app.py
CHANGED
@@ -8,29 +8,50 @@ from PIL import Image
|
|
8 |
import sys, os
|
9 |
from rembg import remove
|
10 |
from utils import functions
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
def clean(text_output):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
else:
|
23 |
-
|
|
|
|
|
24 |
|
25 |
|
26 |
def clean_by_name(text_output):
|
27 |
-
text_output
|
28 |
-
if text_output
|
29 |
-
|
|
|
30 |
os.remove(f"{dir_name}.zip")
|
31 |
-
return ""
|
32 |
else:
|
33 |
-
|
|
|
34 |
|
35 |
|
36 |
|
@@ -45,7 +66,8 @@ def run_rembg(img):
|
|
45 |
|
46 |
|
47 |
def from_zip(inputs):
|
48 |
-
|
|
|
49 |
|
50 |
image_data_dict = {}
|
51 |
with ZipFile(inputs[0].name, "r") as zip_file:
|
@@ -68,27 +90,30 @@ def from_zip(inputs):
|
|
68 |
image_files.append(image_name)
|
69 |
|
70 |
image_array = np.array(image)
|
71 |
-
|
|
|
|
|
|
|
72 |
image_data_dict[image_name] = image_array
|
73 |
|
74 |
except Exception as e:
|
75 |
-
|
76 |
|
77 |
|
78 |
for image_name, image_data in image_data_dict.items():
|
79 |
-
|
80 |
output = remove(image_data)
|
81 |
output_pil = Image.fromarray(output)
|
82 |
# Remove margins
|
83 |
cropped_image = output_pil.crop(output_pil.getbbox())
|
84 |
|
85 |
image_name = image_name.replace("jpg", "png")
|
86 |
-
cropped_image.save(f"
|
87 |
|
88 |
-
shutil.make_archive(
|
89 |
-
shutil.rmtree(
|
90 |
|
91 |
-
return "
|
92 |
|
93 |
|
94 |
def from_image_files(images, text_class_name):
|
@@ -97,11 +122,12 @@ def from_image_files(images, text_class_name):
|
|
97 |
dir_name = text_class_name
|
98 |
else:
|
99 |
dir_name = functions.get_random_name()
|
100 |
-
|
|
|
101 |
|
102 |
for image in images:
|
103 |
image_name = image.name
|
104 |
-
|
105 |
|
106 |
# 読み込み
|
107 |
image_data = np.array(Image.open(image_name))
|
@@ -112,13 +138,14 @@ def from_image_files(images, text_class_name):
|
|
112 |
cropped_image = output_pil.crop(output_pil.getbbox())
|
113 |
|
114 |
image_name = image_name.split("/")[-1]
|
115 |
-
image_name = image_name.
|
|
|
116 |
cropped_image.save(f"{dir_name}/{image_name}")
|
117 |
|
118 |
shutil.make_archive(f"{dir_name}", "zip", f"{dir_name}")
|
119 |
shutil.rmtree(f"{dir_name}")
|
120 |
|
121 |
-
return f"{dir_name}.zip", complete()+"+"+dir_name
|
122 |
|
123 |
|
124 |
|
@@ -172,7 +199,6 @@ if __name__=="__main__":
|
|
172 |
|
173 |
|
174 |
|
175 |
-
|
176 |
with gr.Tab("Zip"):
|
177 |
gr.Markdown(
|
178 |
"""
|
@@ -196,7 +222,7 @@ if __name__=="__main__":
|
|
196 |
with gr.Row():
|
197 |
image_input = gr.File(file_count="multiple")
|
198 |
image_output = gr.File()
|
199 |
-
text_output = gr.Textbox(visible=False)
|
200 |
|
201 |
btn = gr.Button("Run!")
|
202 |
|
|
|
8 |
import sys, os
|
9 |
from rembg import remove
|
10 |
from utils import functions
|
11 |
+
import os, sys
|
12 |
+
import argparse
|
13 |
|
14 |
+
from logging import getLogger, StreamHandler, DEBUG
|
15 |
+
logger = getLogger(__name__)
|
16 |
+
handler = StreamHandler(); handler.setLevel(DEBUG)
|
17 |
+
logger.setLevel(DEBUG)
|
18 |
+
logger.addHandler(handler)
|
19 |
+
logger.propagate = False
|
20 |
|
21 |
+
# logger.debug('variable')
|
22 |
+
# logger.info('system')
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
def complete(work_dir):
|
27 |
+
work_dir = work_dir
|
28 |
+
# logger.debug(f"complete :", work_dir)
|
29 |
+
return work_dir
|
30 |
|
31 |
|
32 |
def clean(text_output):
|
33 |
+
# logger.debug(f"text_output : {text_output}")
|
34 |
+
|
35 |
+
if text_output!="idle_state":
|
36 |
+
logger.info(f"clean up : {text_output}.zip")
|
37 |
+
os.remove(f"{text_output}.zip")
|
38 |
+
return "idle_state"
|
39 |
else:
|
40 |
+
logger.info(f"reset")
|
41 |
+
return "idle_state"
|
42 |
+
|
43 |
|
44 |
|
45 |
def clean_by_name(text_output):
|
46 |
+
# logger.debug(f"text_output : {text_output}")
|
47 |
+
if text_output!="idle_state":
|
48 |
+
text_output, dir_name = text_output.split("+")
|
49 |
+
logger.info(f"clean up : {dir_name}.zip")
|
50 |
os.remove(f"{dir_name}.zip")
|
51 |
+
return "idle_state"
|
52 |
else:
|
53 |
+
logger.info(f"reset")
|
54 |
+
return "idle_state"
|
55 |
|
56 |
|
57 |
|
|
|
66 |
|
67 |
|
68 |
def from_zip(inputs):
|
69 |
+
work_dir = functions.get_random_name()
|
70 |
+
os.makedirs(work_dir, exist_ok=True)
|
71 |
|
72 |
image_data_dict = {}
|
73 |
with ZipFile(inputs[0].name, "r") as zip_file:
|
|
|
90 |
image_files.append(image_name)
|
91 |
|
92 |
image_array = np.array(image)
|
93 |
+
logger.debug(f"image name : {image_name}")
|
94 |
+
category_dir = image_name.split("/")[0]
|
95 |
+
# image_name = image_name.split("/")[1]
|
96 |
+
os.makedirs(f"{work_dir}/{category_dir}", exist_ok=True)
|
97 |
image_data_dict[image_name] = image_array
|
98 |
|
99 |
except Exception as e:
|
100 |
+
logger.info(f"Exception : {e}")
|
101 |
|
102 |
|
103 |
for image_name, image_data in image_data_dict.items():
|
104 |
+
|
105 |
output = remove(image_data)
|
106 |
output_pil = Image.fromarray(output)
|
107 |
# Remove margins
|
108 |
cropped_image = output_pil.crop(output_pil.getbbox())
|
109 |
|
110 |
image_name = image_name.replace("jpg", "png")
|
111 |
+
cropped_image.save(f"{work_dir}/{image_name}")
|
112 |
|
113 |
+
shutil.make_archive(work_dir, "zip", work_dir)
|
114 |
+
shutil.rmtree(work_dir)
|
115 |
|
116 |
+
return f"{work_dir}.zip", complete(work_dir)
|
117 |
|
118 |
|
119 |
def from_image_files(images, text_class_name):
|
|
|
122 |
dir_name = text_class_name
|
123 |
else:
|
124 |
dir_name = functions.get_random_name()
|
125 |
+
|
126 |
+
os.makedirs(dir_name, exist_ok=True)
|
127 |
|
128 |
for image in images:
|
129 |
image_name = image.name
|
130 |
+
# logger.debug(f"image name : {image_name}")
|
131 |
|
132 |
# 読み込み
|
133 |
image_data = np.array(Image.open(image_name))
|
|
|
138 |
cropped_image = output_pil.crop(output_pil.getbbox())
|
139 |
|
140 |
image_name = image_name.split("/")[-1]
|
141 |
+
image_name = image_name[:image_name.find("_", image_name.find("_") + 1)] + ".png"
|
142 |
+
# logger.debug(f"save image name : {image_name}")
|
143 |
cropped_image.save(f"{dir_name}/{image_name}")
|
144 |
|
145 |
shutil.make_archive(f"{dir_name}", "zip", f"{dir_name}")
|
146 |
shutil.rmtree(f"{dir_name}")
|
147 |
|
148 |
+
return f"{dir_name}.zip", complete("complete")+"+"+dir_name
|
149 |
|
150 |
|
151 |
|
|
|
199 |
|
200 |
|
201 |
|
|
|
202 |
with gr.Tab("Zip"):
|
203 |
gr.Markdown(
|
204 |
"""
|
|
|
222 |
with gr.Row():
|
223 |
image_input = gr.File(file_count="multiple")
|
224 |
image_output = gr.File()
|
225 |
+
text_output = gr.Textbox(visible=False, value="idle_state")
|
226 |
|
227 |
btn = gr.Button("Run!")
|
228 |
|