nami0342 commited on
Commit
c225b36
ยท
1 Parent(s): c10d068

Add image link input box

Browse files
Files changed (2) hide show
  1. app.py +44 -0
  2. requirements.txt +1 -0
app.py CHANGED
@@ -25,6 +25,9 @@ from preprocess.openpose.run_openpose import OpenPose
25
  from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
26
  from torchvision.transforms.functional import to_pil_image
27
  import pillow_heif # HEIC ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์šฉ (์•„์ดํฐ ์ดฌ์˜ ์‚ฌ์ง„ ํฌ๋งท)
 
 
 
28
 
29
  def pil_to_binary_mask(pil_image, threshold=0):
30
  np_image = np.array(pil_image)
@@ -156,6 +159,30 @@ def preprocess_image(image):
156
  return resized_img
157
 
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  @spaces.GPU
160
  def start_tryon(dict,garm_img,garment_des,is_checked,denoise_steps,seed, is_checked_crop):
161
  device = "cuda"
@@ -301,6 +328,7 @@ with image_blocks as demo:
301
  with gr.Row():
302
  with gr.Column():
303
  imgs = gr.ImageEditor(sources='upload', type="pil", label='๋Œ€์ƒ ์ด๋ฏธ์ง€', interactive=True)
 
304
  with gr.Row():
305
  with gr.Row():
306
  is_checked = gr.Checkbox(label="Yes", info="์ž๋™ ๋งˆ์Šคํ‚น",value=True)
@@ -312,6 +340,7 @@ with image_blocks as demo:
312
 
313
  with gr.Column():
314
  garm_img = gr.Image(label="์˜์ƒ ์ด๋ฏธ์ง€", sources='upload', type="pil")
 
315
  with gr.Row(elem_id="prompt-container"):
316
  with gr.Row():
317
  prompt = gr.Textbox(placeholder="Description of garment ex) Short Sleeve Round Neck T-shirts", show_label=False, elem_id="prompt")
@@ -344,6 +373,21 @@ with image_blocks as demo:
344
  outputs=imgs, # ์ „์ฒ˜๋ฆฌ๋œ ์ด๋ฏธ์ง€๋ฅผ ImageEditor์— ๋‹ค์‹œ ํ‘œ์‹œ
345
  )
346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  is_checked_crop = True
348
  denoise_steps = 30
349
  seed = 42
 
25
  from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
26
  from torchvision.transforms.functional import to_pil_image
27
  import pillow_heif # HEIC ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์šฉ (์•„์ดํฐ ์ดฌ์˜ ์‚ฌ์ง„ ํฌ๋งท)
28
+ import requests
29
+ from urllib.parse import urlparse
30
+
31
 
32
  def pil_to_binary_mask(pil_image, threshold=0):
33
  np_image = np.array(pil_image)
 
159
  return resized_img
160
 
161
 
162
+ # URL์—์„œ ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ ํ•จ์ˆ˜
163
+ def load_image_from_url(url):
164
+ try:
165
+ response = requests.get(url, stream=True)
166
+ response.raise_for_status() # HTTP ์˜ค๋ฅ˜ ํ™•์ธ
167
+ img = Image.open(response.raw).convert("RGB")
168
+ return img
169
+ except Exception as e:
170
+ print(f"Error loading image from URL: {e}")
171
+ return None
172
+
173
+
174
+ #URL ์ž…๋ ฅ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
175
+ def process_url_image(url, is_garment=False):
176
+ if not url:
177
+ return None
178
+ img = load_image_from_url(url)
179
+ if img and is_garment: # ์˜์ƒ ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ ์ „์ฒ˜๋ฆฌ ์ƒ๋žต ๊ฐ€๋Šฅ
180
+ return img
181
+ elif img:
182
+ return preprocess_image(img) # ๋Œ€์ƒ ์ด๋ฏธ์ง€๋Š” ์ „์ฒ˜๋ฆฌ ์ ์šฉ
183
+ return None
184
+
185
+
186
  @spaces.GPU
187
  def start_tryon(dict,garm_img,garment_des,is_checked,denoise_steps,seed, is_checked_crop):
188
  device = "cuda"
 
328
  with gr.Row():
329
  with gr.Column():
330
  imgs = gr.ImageEditor(sources='upload', type="pil", label='๋Œ€์ƒ ์ด๋ฏธ์ง€', interactive=True)
331
+ img_url_input = gr.Textbox(label="๋Œ€์ƒ ์ด๋ฏธ์ง€ URL", placeholder="์˜ˆ) https://example.com/human_image.jpg")
332
  with gr.Row():
333
  with gr.Row():
334
  is_checked = gr.Checkbox(label="Yes", info="์ž๋™ ๋งˆ์Šคํ‚น",value=True)
 
340
 
341
  with gr.Column():
342
  garm_img = gr.Image(label="์˜์ƒ ์ด๋ฏธ์ง€", sources='upload', type="pil")
343
+ garm_url_input = gr.Textbox(label="์˜์ƒ ์ด๋ฏธ์ง€ URL", placeholder="์˜ˆ) https://example.com/garment.jpg")
344
  with gr.Row(elem_id="prompt-container"):
345
  with gr.Row():
346
  prompt = gr.Textbox(placeholder="Description of garment ex) Short Sleeve Round Neck T-shirts", show_label=False, elem_id="prompt")
 
373
  outputs=imgs, # ์ „์ฒ˜๋ฆฌ๋œ ์ด๋ฏธ์ง€๋ฅผ ImageEditor์— ๋‹ค์‹œ ํ‘œ์‹œ
374
  )
375
 
376
+
377
+ # ๋Œ€์ƒ ์ด๋ฏธ์ง€: URL ์ž…๋ ฅ ์ฒ˜๋ฆฌ
378
+ img_url_input.change(
379
+ fn=lambda url: process_url_image(url, is_garment=False),
380
+ inputs=img_url_input,
381
+ outputs=imgs,
382
+ )
383
+
384
+ # ์˜์ƒ ์ด๋ฏธ์ง€: URL ์ž…๋ ฅ ์ฒ˜๋ฆฌ
385
+ garm_url_input.change(
386
+ fn=lambda url: process_url_image(url, is_garment=True),
387
+ inputs=garm_url_input,
388
+ outputs=garm_img,
389
+ )
390
+
391
  is_checked_crop = True
392
  denoise_steps = 30
393
  seed = 42
requirements.txt CHANGED
@@ -23,3 +23,4 @@ omegaconf
23
  pycocotools
24
  huggingface_hub==0.25.0
25
  pillow-heif
 
 
23
  pycocotools
24
  huggingface_hub==0.25.0
25
  pillow-heif
26
+ requests