szk1ck commited on
Commit
be5d475
·
1 Parent(s): fd9eb26

Input IMage Filesを追加

Browse files
app.py CHANGED
@@ -7,6 +7,7 @@ import gradio as gr
7
  from PIL import Image
8
  import sys, os
9
  from rembg import remove
 
10
 
11
 
12
  def complete():
@@ -20,6 +21,15 @@ def clean(text_output):
20
  return ""
21
  else:
22
  pass
 
 
 
 
 
 
 
 
 
23
 
24
 
25
 
@@ -32,7 +42,8 @@ def run_rembg(img):
32
 
33
  return cropped_image
34
 
35
- def load_zip(inputs):
 
36
  os.makedirs("objects", exist_ok=True)
37
 
38
  image_data_dict = {}
@@ -64,6 +75,7 @@ def load_zip(inputs):
64
 
65
 
66
  for image_name, image_data in image_data_dict.items():
 
67
  output = remove(image_data)
68
  output_pil = Image.fromarray(output)
69
  # Remove margins
@@ -78,6 +90,33 @@ def load_zip(inputs):
78
  return "objects.zip", complete()
79
 
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  if __name__=="__main__":
83
 
@@ -112,7 +151,7 @@ if __name__=="__main__":
112
  btn = gr.Button("Run!")
113
 
114
  btn.click(
115
- fn=load_zip,
116
  inputs=image_input,
117
  outputs=[image_output, text_output]
118
  )
@@ -149,14 +188,59 @@ if __name__=="__main__":
149
  image_output = gr.Image(type="pil")
150
 
151
  btn = gr.Button("Run!")
 
 
 
 
 
 
 
 
 
 
152
 
153
- btn.click(
154
- fn=run_rembg,
155
- inputs=image_input,
156
- outputs=image_output,
157
- api_name="imageMatting"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  )
159
 
 
 
 
160
  gr.Markdown(
161
  """
162
  ---
 
7
  from PIL import Image
8
  import sys, os
9
  from rembg import remove
10
+ from utils import functions
11
 
12
 
13
  def complete():
 
21
  return ""
22
  else:
23
  pass
24
+
25
+ def clean_by_name(text_output):
26
+ text_output, dir_name = text_output.split("+")
27
+ if text_output=="complete":
28
+ print("clean up")
29
+ os.remove(f"{dir_name}.zip")
30
+ return ""
31
+ else:
32
+ pass
33
 
34
 
35
 
 
42
 
43
  return cropped_image
44
 
45
+
46
+ def from_zip(inputs):
47
  os.makedirs("objects", exist_ok=True)
48
 
49
  image_data_dict = {}
 
75
 
76
 
77
  for image_name, image_data in image_data_dict.items():
78
+ print(type(image_data))
79
  output = remove(image_data)
80
  output_pil = Image.fromarray(output)
81
  # Remove margins
 
90
  return "objects.zip", complete()
91
 
92
 
93
+ def from_image_files(images):
94
+ dir_name = functions.get_random_name()
95
+ os.makedirs(dir_name, exist_ok=True)
96
+
97
+ for image in images:
98
+ image_name = image.name
99
+
100
+
101
+ # 読み込み
102
+ image_data = np.array(Image.open(image_name))
103
+
104
+ output = remove(image_data)
105
+ output_pil = Image.fromarray(output)
106
+ # Remove margins
107
+ cropped_image = output_pil.crop(output_pil.getbbox())
108
+
109
+ image_name = image_name.split("/")[-1]
110
+ image_name = image_name.replace("jpg", "png")
111
+ cropped_image.save(f"{dir_name}/{image_name}")
112
+
113
+ shutil.make_archive(f"{dir_name}", "zip", f"{dir_name}")
114
+ shutil.rmtree(f"{dir_name}")
115
+
116
+ return f"{dir_name}.zip", complete()+"+"+dir_name
117
+
118
+
119
+
120
 
121
  if __name__=="__main__":
122
 
 
151
  btn = gr.Button("Run!")
152
 
153
  btn.click(
154
+ fn=from_zip,
155
  inputs=image_input,
156
  outputs=[image_output, text_output]
157
  )
 
188
  image_output = gr.Image(type="pil")
189
 
190
  btn = gr.Button("Run!")
191
+
192
+
193
+ btn.click(
194
+ fn=run_rembg,
195
+ inputs=image_input,
196
+ outputs=image_output,
197
+ api_name="imageMatting"
198
+ )
199
+
200
+
201
 
202
+ with gr.Tab("Images"):
203
+ gr.Markdown(
204
+ """
205
+ <center><h1>Image Matting using U<sup>2</sup>-Net</h1></center>
206
+ """
207
+ )
208
+ with gr.Row():
209
+ gr.Markdown(
210
+ """
211
+ ### Input Image Files
212
+ <img src='file/assets/input_zip.png' width="85%" height="85%">
213
+ """
214
+ )
215
+ gr.Markdown(
216
+ """
217
+ ### Output Zip File
218
+ <img src='file/assets/output_zip.png' width="85%" height="85%">
219
+ """
220
+ )
221
+
222
+ with gr.Row():
223
+ image_input = gr.File(file_count="multiple")
224
+ image_output = gr.File()
225
+ text_output = gr.Textbox(visible=False)
226
+
227
+ btn = gr.Button("Run!")
228
+
229
+ btn.click(
230
+ fn=from_image_files,
231
+ inputs=image_input,
232
+ outputs=[image_output, text_output]
233
+ )
234
+ text_output.change(
235
+ fn=clean_by_name,
236
+ inputs=text_output,
237
+ outputs=text_output
238
+
239
  )
240
 
241
+
242
+
243
+
244
  gr.Markdown(
245
  """
246
  ---
utils/__pycache__/functions.cpython-39.pyc ADDED
Binary file (678 Bytes). View file
 
utils/functions.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ def get_random_name():
4
+ famous_painters = [
5
+ "Leonardo", "DaVinci",
6
+ "Michelangelo",
7
+ "Pablo", "Picasso",
8
+ "Vincent", "VanGogh",
9
+ "Rembrandt", "VanRijn",
10
+ "Claude", "Monet",
11
+ "Salvador", "Dali",
12
+ "Jackson", "Pollock",
13
+ "Andy", "Warhol",
14
+ "Henri", "Matisse",
15
+ "Georgia", "Keeffe",
16
+ "Edvard", "Munch",
17
+ "Wassily", "Kandinsky",
18
+ "Gustav", "Klimt",
19
+ "Rene", "Magritte",
20
+ "Frida", "Kahlo",
21
+ "Edgar", "Degas",
22
+ "Johannes", "Vermeer",
23
+ "Paul", "Cezanne",
24
+ "Marc", "Chagall",
25
+ ]
26
+
27
+ random_painter = random.choice(famous_painters)
28
+
29
+
30
+ return random_painter