ssoxye commited on
Commit
e9ebd5a
·
1 Parent(s): ed13881

update mask process

Browse files
Files changed (1) hide show
  1. app.py +50 -1
app.py CHANGED
@@ -158,9 +158,58 @@ def merge_white_regions_or(img1: Image.Image, img2: Image.Image) -> Image.Image:
158
 
159
 
160
  def preprocess_mask(mask_img: Image.Image) -> Image.Image:
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  m = np.array(mask_img.convert("L"), dtype=np.uint8)
 
 
 
 
 
 
 
162
  _, m = cv2.threshold(m, 127, 255, cv2.THRESH_BINARY)
163
- return Image.fromarray(m).convert("RGB")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
 
166
  def make_depth(depth_path: str) -> Image.Image:
 
158
 
159
 
160
  def preprocess_mask(mask_img: Image.Image) -> Image.Image:
161
+ """
162
+ (2번째 첨부 코드 규칙 반영)
163
+ - 입력(mask_img)을 전역 (W,H)로 resize (NEAREST)
164
+ - 이진화(threshold)
165
+ - padding 목표 width는 항상 1024로 고정
166
+ * width < 1024: 좌/우 padding (value=0)
167
+ * width > 1024: 중앙 crop
168
+ - dilation (kernel 17x17, iter=1)
169
+ - return: RGB mask PIL
170
+ """
171
+ global H, W
172
+
173
+ # 1) to grayscale np
174
  m = np.array(mask_img.convert("L"), dtype=np.uint8)
175
+
176
+ # 2) resize to global (W, H) (2번째 코드의 "resize는 전역 (W,H)" 컨벤션)
177
+ if (H is not None) and (W is not None):
178
+ # mask는 경계 보존 위해 NEAREST 권장
179
+ m = cv2.resize(m, (W, H), interpolation=cv2.INTER_NEAREST)
180
+
181
+ # 3) binarize
182
  _, m = cv2.threshold(m, 127, 255, cv2.THRESH_BINARY)
183
+
184
+ # 4) pad/crop to target width=1024 (항상 고정)
185
+ target_width = 1024
186
+ h, w = m.shape[:2]
187
+
188
+ if w < target_width:
189
+ total_padding = target_width - w
190
+ left_padding = total_padding // 2
191
+ right_padding = total_padding - left_padding
192
+ m = cv2.copyMakeBorder(
193
+ m,
194
+ top=0, bottom=0,
195
+ left=left_padding, right=right_padding,
196
+ borderType=cv2.BORDER_CONSTANT,
197
+ value=0,
198
+ )
199
+ elif w > target_width:
200
+ left = (w - target_width) // 2
201
+ m = m[:, left:left + target_width]
202
+
203
+ # 5) dilate (2번째 코드 동일)
204
+ kernel = np.ones((17, 17), np.uint8)
205
+ m = cv2.dilate(m, kernel, iterations=1)
206
+
207
+ if DEBUG_SAVE:
208
+ cv2.imwrite("mask_final_1024.png", m)
209
+
210
+ # 6) return as RGB PIL
211
+ return Image.fromarray(m, mode="L").convert("RGB")
212
+
213
 
214
 
215
  def make_depth(depth_path: str) -> Image.Image: