Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -162,22 +162,6 @@ def gd_detect(img: Image.Image, prompt: str) -> BoundingBox | None:
|
|
| 162 |
bboxes = corners_to_pixels_format(results["boxes"].cpu(), width, height)
|
| 163 |
return bbox_union(bboxes.numpy().tolist())
|
| 164 |
|
| 165 |
-
def apply_mask(img: Image.Image, mask_img: Image.Image, defringe: bool = True) -> Image.Image:
|
| 166 |
-
"""마스크 적용 함수"""
|
| 167 |
-
assert img.size == mask_img.size
|
| 168 |
-
img = img.convert("RGB")
|
| 169 |
-
mask_img = mask_img.convert("L")
|
| 170 |
-
|
| 171 |
-
if defringe:
|
| 172 |
-
rgb, alpha = np.asarray(img) / 255.0, np.asarray(mask_img) / 255.0
|
| 173 |
-
foreground = cast(np.ndarray[Any, np.dtype[np.uint8]], estimate_foreground_ml(rgb, alpha))
|
| 174 |
-
img = Image.fromarray((foreground * 255).astype("uint8"))
|
| 175 |
-
|
| 176 |
-
# 투명한 배경으로 결과 생성
|
| 177 |
-
result = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
| 178 |
-
result.paste(img, (0, 0), mask_img)
|
| 179 |
-
|
| 180 |
-
return result
|
| 181 |
|
| 182 |
|
| 183 |
def adjust_size_to_multiple_of_8(width: int, height: int) -> tuple[int, int]:
|
|
@@ -270,33 +254,7 @@ def resize_object(image: Image.Image, scale_percent: float) -> Image.Image:
|
|
| 270 |
height = int(image.height * scale_percent / 100)
|
| 271 |
return image.resize((width, height), Image.Resampling.LANCZOS)
|
| 272 |
|
| 273 |
-
|
| 274 |
-
position: str = "bottom-center", scale_percent: float = 100) -> Image.Image:
|
| 275 |
-
"""전경과 배경 합성 함수"""
|
| 276 |
-
print(f"Combining with position: {position}, scale: {scale_percent}")
|
| 277 |
-
|
| 278 |
-
# 배경 이미지를 RGBA 모드로 변환
|
| 279 |
-
result = background.convert('RGBA')
|
| 280 |
-
|
| 281 |
-
# 전경 이미지가 RGBA가 아니면 변환
|
| 282 |
-
if foreground.mode != 'RGBA':
|
| 283 |
-
foreground = foreground.convert('RGBA')
|
| 284 |
-
|
| 285 |
-
# 스케일 조정
|
| 286 |
-
scaled_foreground = resize_object(foreground, scale_percent)
|
| 287 |
-
|
| 288 |
-
# 위치 계산
|
| 289 |
-
x, y = calculate_object_position(position, result.size, scaled_foreground.size)
|
| 290 |
-
print(f"Calculated position coordinates: ({x}, {y})")
|
| 291 |
-
|
| 292 |
-
# 투명한 배경의 새 이미지 생성
|
| 293 |
-
temp = Image.new('RGBA', result.size, (0, 0, 0, 0))
|
| 294 |
-
temp.paste(scaled_foreground, (x, y), scaled_foreground)
|
| 295 |
-
|
| 296 |
-
# 최종 합성
|
| 297 |
-
result = Image.alpha_composite(result, temp)
|
| 298 |
-
|
| 299 |
-
return result
|
| 300 |
|
| 301 |
@spaces.GPU(duration=30) # 120초에서 30초로 감소
|
| 302 |
def _gpu_process(img: Image.Image, prompt: str | BoundingBox | None) -> tuple[Image.Image, BoundingBox | None, list[str]]:
|
|
@@ -365,7 +323,35 @@ def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str
|
|
| 365 |
return gr.update(interactive=bool(img and prompt))
|
| 366 |
|
| 367 |
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
| 370 |
scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
|
| 371 |
try:
|
|
@@ -420,6 +406,23 @@ ddef process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
|
| 420 |
finally:
|
| 421 |
clear_memory()
|
| 422 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
|
| 424 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
| 425 |
try:
|
|
|
|
| 162 |
bboxes = corners_to_pixels_format(results["boxes"].cpu(), width, height)
|
| 163 |
return bbox_union(bboxes.numpy().tolist())
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
|
| 167 |
def adjust_size_to_multiple_of_8(width: int, height: int) -> tuple[int, int]:
|
|
|
|
| 254 |
height = int(image.height * scale_percent / 100)
|
| 255 |
return image.resize((width, height), Image.Resampling.LANCZOS)
|
| 256 |
|
| 257 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
@spaces.GPU(duration=30) # 120초에서 30초로 감소
|
| 260 |
def _gpu_process(img: Image.Image, prompt: str | BoundingBox | None) -> tuple[Image.Image, BoundingBox | None, list[str]]:
|
|
|
|
| 323 |
return gr.update(interactive=bool(img and prompt))
|
| 324 |
|
| 325 |
|
| 326 |
+
def combine_with_background(foreground: Image.Image, background: Image.Image,
|
| 327 |
+
position: str = "bottom-center", scale_percent: float = 100) -> Image.Image:
|
| 328 |
+
"""전경과 배경 합성 함수"""
|
| 329 |
+
print(f"Combining with position: {position}, scale: {scale_percent}")
|
| 330 |
+
|
| 331 |
+
# 배경 이미지를 RGBA 모드로 변환
|
| 332 |
+
result = background.convert('RGBA')
|
| 333 |
+
|
| 334 |
+
# 전경 이미지가 RGBA가 아니면 변환
|
| 335 |
+
if foreground.mode != 'RGBA':
|
| 336 |
+
foreground = foreground.convert('RGBA')
|
| 337 |
+
|
| 338 |
+
# 스케일 조정
|
| 339 |
+
scaled_foreground = resize_object(foreground, scale_percent)
|
| 340 |
+
|
| 341 |
+
# 위치 계산
|
| 342 |
+
x, y = calculate_object_position(position, result.size, scaled_foreground.size)
|
| 343 |
+
print(f"Calculated position coordinates: ({x}, {y})")
|
| 344 |
+
|
| 345 |
+
# 투명한 배경의 새 이미지 생성
|
| 346 |
+
temp = Image.new('RGBA', result.size, (0, 0, 0, 0))
|
| 347 |
+
temp.paste(scaled_foreground, (x, y), scaled_foreground)
|
| 348 |
+
|
| 349 |
+
# 최종 합성
|
| 350 |
+
result = Image.alpha_composite(result, temp)
|
| 351 |
+
|
| 352 |
+
return result
|
| 353 |
+
|
| 354 |
+
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
| 355 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
| 356 |
scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
|
| 357 |
try:
|
|
|
|
| 406 |
finally:
|
| 407 |
clear_memory()
|
| 408 |
|
| 409 |
+
def apply_mask(img: Image.Image, mask_img: Image.Image, defringe: bool = True) -> Image.Image:
|
| 410 |
+
"""마스크 적용 함수"""
|
| 411 |
+
assert img.size == mask_img.size
|
| 412 |
+
img = img.convert("RGB")
|
| 413 |
+
mask_img = mask_img.convert("L")
|
| 414 |
+
|
| 415 |
+
if defringe:
|
| 416 |
+
rgb, alpha = np.asarray(img) / 255.0, np.asarray(mask_img) / 255.0
|
| 417 |
+
foreground = cast(np.ndarray[Any, np.dtype[np.uint8]], estimate_foreground_ml(rgb, alpha))
|
| 418 |
+
img = Image.fromarray((foreground * 255).astype("uint8"))
|
| 419 |
+
|
| 420 |
+
# 투명한 배경으로 결과 생성
|
| 421 |
+
result = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
| 422 |
+
result.paste(img, (0, 0), mask_img)
|
| 423 |
+
|
| 424 |
+
return result
|
| 425 |
+
|
| 426 |
|
| 427 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
| 428 |
try:
|