nami0342 commited on
Commit
7d093ef
·
1 Parent(s): 35b124b

Support HEIC format (iPhone snapshot) and image preprocessing

Browse files
Files changed (2) hide show
  1. app.py +53 -10
  2. requirements.txt +2 -1
app.py CHANGED
@@ -24,7 +24,7 @@ from preprocess.humanparsing.run_parsing import Parsing
24
  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
-
28
 
29
  def pil_to_binary_mask(pil_image, threshold=0):
30
  np_image = np.array(pil_image)
@@ -121,6 +121,41 @@ pipe = TryonPipeline.from_pretrained(
121
  )
122
  pipe.unet_encoder = UNet_Encoder
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  @spaces.GPU
125
  def start_tryon(dict,garm_img,garment_des,is_checked,denoise_steps,seed, is_checked_crop):
126
  device = "cuda"
@@ -133,15 +168,15 @@ def start_tryon(dict,garm_img,garment_des,is_checked,denoise_steps,seed, is_chec
133
  human_img_orig = dict["background"].convert("RGB")
134
 
135
  if is_checked_crop:
136
- width, height = human_img_orig.size
137
- target_width = int(min(width, height * (3 / 4)))
138
- target_height = int(min(height, width * (4 / 3)))
139
- left = (width - target_width) / 2
140
- top = (height - target_height) / 2
141
- right = (width + target_width) / 2
142
- bottom = (height + target_height) / 2
143
- cropped_img = human_img_orig.crop((left, top, right, bottom))
144
- crop_size = cropped_img.size
145
  human_img = cropped_img.resize((768,1024))
146
  else:
147
  human_img = human_img_orig.resize((768,1024))
@@ -301,6 +336,14 @@ with image_blocks as demo:
301
  # seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
302
 
303
  # is_checked = gr.Number(value=True)
 
 
 
 
 
 
 
 
304
  is_checked_crop = True
305
  denoise_steps = 30
306
  seed = 42
 
24
  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)
 
121
  )
122
  pipe.unet_encoder = UNet_Encoder
123
 
124
+
125
+ # 이미지 전처리 함수
126
+ def preprocess_image(image):
127
+ # HEIC 이미지 처리
128
+ if isinstance(image, np.ndarray):
129
+ image = Image.fromarray(image)
130
+
131
+ # HEIC 이미지를 JPEG로 변환
132
+ try:
133
+ output = io.BytesIO()
134
+ image.convert("RGB").save(output, format="JPEG")
135
+ image = Image.open(output)
136
+ except Exception as e:
137
+ print(f"Error converting image: {e}")
138
+
139
+ # 이미지 크기 가져오기
140
+ width, height = image.size
141
+
142
+ # 3:4 비율로 중앙 자르기
143
+ target_width = int(min(width, height * (3 / 4)))
144
+ target_height = int(min(height, width * (4 / 3)))
145
+ left = (width - target_width) / 2
146
+ top = (height - target_height) / 2
147
+ right = (width + target_width) / 2
148
+ bottom = (height + target_height) / 2
149
+
150
+ # 이미지 자르기
151
+ cropped_img = image.crop((left, top, right, bottom))
152
+
153
+ # 768x1024로 리사이징
154
+ resized_img = cropped_img.resize((768, 1024), resample=Image.Resampling.LANCZOS)
155
+
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"
 
168
  human_img_orig = dict["background"].convert("RGB")
169
 
170
  if is_checked_crop:
171
+ # width, height = human_img_orig.size
172
+ # target_width = int(min(width, height * (3 / 4)))
173
+ # target_height = int(min(height, width * (4 / 3)))
174
+ # left = (width - target_width) / 2
175
+ # top = (height - target_height) / 2
176
+ # right = (width + target_width) / 2
177
+ # bottom = (height + target_height) / 2
178
+ # cropped_img = human_img_orig.crop((left, top, right, bottom))
179
+ # crop_size = cropped_img.size
180
  human_img = cropped_img.resize((768,1024))
181
  else:
182
  human_img = human_img_orig.resize((768,1024))
 
336
  # seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
337
 
338
  # is_checked = gr.Number(value=True)
339
+
340
+ # 이미지 업로드 시 전처리
341
+ imgs.upload(
342
+ fn=preprocess_image,
343
+ inputs=imgs,
344
+ outputs=imgs, # 전처리된 이미지를 ImageEditor에 다시 표시
345
+ )
346
+
347
  is_checked_crop = True
348
  denoise_steps = 30
349
  seed = 42
requirements.txt CHANGED
@@ -21,4 +21,5 @@ fvcore
21
  cloudpickle
22
  omegaconf
23
  pycocotools
24
- huggingface_hub==0.25.0
 
 
21
  cloudpickle
22
  omegaconf
23
  pycocotools
24
+ huggingface_hub==0.25.0
25
+ pillow-heif