Spaces:
Sleeping
Sleeping
File size: 23,495 Bytes
e3af00f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
from pathlib import Path
import cv2
import numpy as np
import pandas as pd
def maskblur(mask, kernel_size, sigma=1):
mask_blur = cv2.GaussianBlur(mask, (kernel_size, kernel_size), sigma)
return mask_blur
def erosion(mask, kernel_size):
kernel = np.ones((kernel_size, kernel_size), np.uint8)
erosion_image = cv2.erode(mask, kernel, iterations=1) # // make erosion image
return erosion_image
def dilate(mask, kernel_size):
kernel = np.ones((kernel_size, kernel_size), np.uint8)
erosion_image = cv2.dilate(mask, kernel, iterations=1) # // make erosion image
return erosion_image
def resize_adapt(model_out, crop_region):
def inter_alg(target_size, img):
if isinstance(target_size, tuple):
w, h = target_size
else:
w, h = target_size, target_size
return inter_alg_(w, h, img)
def inter_alg_(w, h, img):
if w * h < img.shape[0] * img.shape[1]:
return cv2.INTER_AREA
else:
return cv2.INTER_CUBIC
x1, y1, x2, y2 = crop_region
h, w = y2 - y1 + 1, x2 - x1 + 1
sz = model_out.shape[0] # h,w ๋์ผํ๋ค.
if h == sz and w == sz:
return model_out
r = max(h, w) / sz
max_hw = max(h, w)
temp_ = cv2.resize(model_out, (max_hw, max_hw), inter_alg(max_hw, model_out))
temp_ = temp_[
(max_hw - h) // 2 : (max_hw - h) // 2 + h,
(max_hw - w) // 2 : (max_hw - w) // 2 + w,
]
return temp_
def get_face_mask(
img_size, df_fan_row, blur_ratio=0.3, dilate_ratio=0.2, erosion_ratio=0
):
assert blur_ratio >= 0 and blur_ratio <= 1
assert erosion_ratio >= 0 and erosion_ratio <= 1
assert dilate_ratio >= 0 and dilate_ratio <= 1
def _masking(img, pts, value):
img = cv2.fillPoly(img, [pts], value)
return img
def _get_face_pts_n_box(img_size, df_fan_row):
box = df_fan_row["cropped_box"]
pts2d = df_fan_row["pts2d"] - np.array([box[0], box[1]])
if isinstance(df_fan_row["cropped_size"], float):
cropped_size = df_fan_row["cropped_size"]
else:
cropped_size = df_fan_row["cropped_size"][0]
ratio = img_size[0] / cropped_size
pts2d = pts2d * ratio
xs, ys = pts2d[:, 0], pts2d[:, 1]
l, t, r, b = min(xs), min(ys), max(xs), max(ys)
return np.concatenate([pts2d[0:17, :], pts2d[17:27, :][::-1]]).astype(
np.uint832
), (l, t, r, b)
if df_fan_row["pts2d"] is None:
mask = np.zeros((img_size[1], img_size[0]), dtype=np.uint8)
if len(mask.shape) == 2:
mask = np.expand_dims(mask, axis=2)
return {"crop": mask, "origin": 1 - mask}
pts, box = _get_face_pts_n_box(img_size, df_fan_row)
h = max(box[2] - box[0], box[3] - box[1])
mask = np.zeros((img_size[1], img_size[0]), dtype=np.uint8)
mask = _masking(mask, pts, (255))
if dilate_ratio != 0:
mask = dilate(mask, int(h * dilate_ratio) // 2 * 2 + 1)
if erosion_ratio != 0:
mask = erosion(mask, int(h * erosion_ratio) // 2 * 2 + 1)
if blur_ratio != 0:
blur_kernel_size = int(h * blur_ratio) // 2 * 2 + 1
mask = maskblur(mask, blur_kernel_size, 0)
mask = mask / 255
if len(mask.shape) == 2:
mask = np.expand_dims(mask, axis=2)
return {"crop": mask, "origin": 1 - mask}
def cromakey_green(img):
r = img[:, :, 0]
g = img[:, :, 1]
b = img[:, :, 2]
g_alpha = g > 50
r_alpha = (g * 1.0) > r
b_alpha = (g * 0.7) > b
alpha = g_alpha & (r_alpha & b_alpha)
alpha = (1 - alpha) * 255
alpha = alpha.astype(np.uint8)
alpha = maskblur(alpha, kernel_size=13)
alpha[np.where(alpha > 100)] = 255
alpha = erosion(alpha, kernel_size=5)
if len(alpha.shape) == 2:
alpha2 = np.expand_dims(alpha, axis=2)
else:
alpha2 = alpha
new = np.concatenate((img, alpha2), axis=2)
return new
def cromakey_green_binary(img):
img = cromakey_green(img)
alpha = img[:, :, 3]
alpha[np.where(alpha <= 128)] = 0
alpha[np.where(alpha > 128)] = 1
def cromakey_green_hunet_lmy(img):
r = img[:, :, 0]
g = img[:, :, 1]
b = img[:, :, 2]
g_alpha = g > 70
r_alpha = g > r
b_alpha = (g * 0.8) > b
alpha = g_alpha & (r_alpha & b_alpha)
alpha = (1 - alpha) * 255
alpha = alpha.astype(np.uint8)
alpha = maskblur(alpha, kernel_size=11)
alpha[np.where(alpha > 100)] = 255
alpha = maskblur(alpha, kernel_size=3)
alpha = erosion(alpha, kernel_size=3)
if len(alpha.shape) == 2:
alpha2 = np.expand_dims(alpha, axis=2)
else:
alpha2 = alpha
new = np.concatenate((img, alpha2), axis=2)
return new
# ybm ์์์ฉ ํฌ๋ก๋งํค ํจ์
def cromakey_green_ybm_front(img):
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
g_alpha = g > 70
# r_alpha = (g * 0.7) > r
# b_alpha = (g * 0.7) > b
r_alpha = g > r
b_alpha = (g * 0.9) > b
alpha = g_alpha & (r_alpha & b_alpha)
alpha = (1 - alpha) * 255
alpha = alpha.astype(np.uint8)
alpha = maskblur(alpha, kernel_size=11)
alpha[np.where(alpha > 100)] = 255
alpha = maskblur(alpha, kernel_size=3)
alpha = maskblur(alpha, kernel_size=3)
grey_alpha = alpha < 255
g[grey_alpha] = r[grey_alpha] * 0.8
if len(alpha.shape) == 2:
alpha2 = np.expand_dims(alpha, axis=2)
else:
alpha2 = alpha
new = np.concatenate((img, alpha2), axis=2)
return new
# ybm ์์์ฉ ํฌ๋ก๋งํค ํจ์
def cromakey_green_ybm_side(img):
img = img.copy()
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
g_alpha = g > 50
r_alpha = g > r
b_alpha = (g * 0.9) > b
alpha = g_alpha & (r_alpha & b_alpha)
alpha = (1 - alpha) * 255
alpha = alpha.astype(np.uint8)
alpha = maskblur(alpha, kernel_size=11)
alpha[np.where(alpha > 100)] = 255
alpha = maskblur(alpha, kernel_size=3)
alpha = maskblur(alpha, kernel_size=3)
grey_alpha = alpha < 255
g[grey_alpha] = r[grey_alpha] * 0.8
if len(alpha.shape) == 2:
alpha2 = np.expand_dims(alpha, axis=2)
else:
alpha2 = alpha
new = np.concatenate((img, alpha2), axis=2)
return new
# devin ์์์ฉ ํฌ๋ก๋งํค ํจ์
def cromakey_green_devin_side(img):
img = img.copy()
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
g_alpha = g > 70
r_alpha = (g * 0.8) > r
# r_alpha = g > r
b_alpha = (g * 0.9) > b
alpha = g_alpha & (r_alpha & b_alpha)
alpha = (1 - alpha) * 255
alpha = alpha.astype(np.uint8)
alpha = maskblur(alpha, kernel_size=7, sigma=3)
alpha[np.where(alpha < 150)] = 0
alpha = maskblur(alpha, kernel_size=5, sigma=2)
if len(alpha.shape) == 2:
alpha = np.expand_dims(alpha, axis=2)
new = np.concatenate((img, alpha), axis=2)
return new
def get_cromakey_func(args):
if "cromakey" not in args.keys():
return cromakey_green_hunet_lmy
if "cromakey_green_ybm_front" == args.cromakey:
return cromakey_green_ybm_front
if "cromakey_green_ybm_side" == args.cromakey:
return cromakey_green_ybm_side
if "cromakey_green_devin_side" == args.cromakey:
return cromakey_green_devin_side
raise "cromakey not found"
def compose_default_(model_out, org_image_with_alpha, mask, **kwargs):
# 1. ๋ง์คํฌ ์๊ธฐ : ์๋ ๋น๋์ค์ ํฌ๋ช
๊ฐ๊ณผ ๊ณ์ฐํ ๋ง์คํฌ๋ฅผ ์๋๋ค.
mask = mask[:, :, 0]
mask[np.where(mask > 0)] = 1 # ๋ง์คํฌ ์์ญ์ 128 -> 1 ๋ก ๋ง๋ ๋ค.
model_out[:, :, 3] = (
org_image_with_alpha[:, :, 3] * (1 - mask) + model_out[:, :, 3] * mask
)
# 2. ์์ธ ๋ง์คํฌ๊ฐ ์ข ์์ฐ์ค๋ฝ๊ฒ ์์ด๋๋ก ํจ.
model_out[:, :, 3] = maskblur(model_out[:, :, 3], kernel_size=3, sigma=1)
return model_out
def compose_devin_(model_out, org_image_with_alpha, mask, debug=False, **kwargs):
mask = mask[:, :, 0]
mask[np.where(mask > 0)] = 1 # ๋ง์คํฌ ์์ญ์ 128 -> 1 ๋ก ๋ง๋ ๋ค.
mask = mask.astype(np.float32)
# 1. ๊ธฐ์กด๋ง์คํฌ์ ๊ฒฝ๊ณ๊ฐ ์ ์๋ณด์ด๋๋ก ๋ง์คํฌ๋ฅผ ๋ถ๋๋ฝ๊ฒ ๋ง๋ ๋ค.
kernel_size = int(mask.shape[0] * 0.03) // 2 * 2 + 1 # ์ด๋ฏธ์ง ํฌ๊ธฐ์ 3% ์ ๋ ๋ง์คํฌ๋ฅผ ํ์ฅํ๋ค.
if debug:
print(
f"## compose_devin_: kernel_size:{kernel_size}, mask_height:{mask.shape[0]}"
)
if kernel_size >= 3:
mask = dilate(mask, kernel_size=kernel_size)
mask = maskblur(mask, kernel_size=kernel_size, sigma=kernel_size // 2)
mask = maskblur(mask, kernel_size=kernel_size, sigma=kernel_size // 2)
mask = erosion(mask, kernel_size=3) # 1pixel ๋ง ์ค์
# 2. ๋ง์คํฌ ์๊ธฐ : ์๋ ๋น๋์ค์ ํฌ๋ช
๊ฐ๊ณผ ๊ณ์ฐํ ๋ง์คํฌ๋ฅผ ์๋๋ค.
model_out[:, :, 3] = (
org_image_with_alpha[:, :, 3] * (1 - mask) + model_out[:, :, 3] * mask
)
# 3. ์์ธ ๋ง์คํฌ๊ฐ ๋ถ๋๋ฝ๊ฒ ํ๋ฒ๋ ๋ธ๋ฌ๋ฅผ ํ๋ค.
model_out[:, :, 3] = maskblur(model_out[:, :, 3], kernel_size=3, sigma=1)
return model_out
def get_compose_mask_func(args):
if "cromakey" in args.keys():
if "cromakey_green_devin_side" == args.cromakey:
return compose_devin_
if "compose" in args.keys():
if "compose_smooth" == args.compose:
return compose_devin_
return compose_default_
def get_keying_func(template):
cromakey_func = get_cromakey_func(template.model.args)
compose_func = get_compose_mask_func(template.model.args)
def keying_(pred, idx, box=None):
model_out, mask, alpha = pred["pred"], pred["mask"], pred["img_gt_with_alpha"]
if pred["filename"].endswith("_no.jpg") or pred["filename"].endswith("_no.png"):
return alpha[:, :, [2, 1, 0, 3]]
if (
alpha.shape[0] != mask.shape[0]
or alpha.shape[1] != mask.shape[1]
or alpha.shape[0] != model_out.shape[0]
or alpha.shape[1] != model_out.shape[1]
):
raise Exception(
f"not matched keying shape. "
f"alpha: {alpha.shape[0]}, {alpha.shape[1]}, {alpha.shape[2]}, "
f"mask: {mask.shape[0]}, {mask.shape[1]}, "
f"model_out: {model_out.shape[0]}, {model_out.shape[1]}"
)
if box is not None:
model_h = model_out.shape[0]
box_h = box[3] - box[1]
if box_h > model_h:
model_out = resize_adapt(model_out, box)
mask = resize_adapt(mask, box)
alpha = resize_adapt(alpha, box)
model_out = cromakey_func(model_out)
model_out = compose_func(
model_out=model_out, org_image_with_alpha=alpha, mask=mask
)
return model_out
return keying_
def get_box_mask(width, height, config, verbose=False):
def get_mask_(
width, height, gradation_width, gradation_bottom=None, box_mask_erosion=None
):
mask = np.ones((height, width, 1))
r = list(range(0, gradation_width, 1))
for s, e in zip(r, r[1:]):
g = s / gradation_width
# print(f'---- s:{s}, e:{e}, g:{g}')
mask[s:e, s : width - s, :] = g
mask[height - e : height - s, s : width - s, :] = g
mask[s : height - s, s:e, :] = g
mask[s : height - s, width - e : width - s, :] = g
if gradation_bottom is not None:
r = list(range(0, gradation_bottom, 1))
for s, e in zip(r, r[1:]):
g = s / gradation_bottom
mask[height - e : height - s, s : width - s, :] = g
if box_mask_erosion is not None:
mask = erosion(mask, box_mask_erosion * 2 + 1)
if len(mask.shape) == 2:
mask = np.expand_dims(mask, 2) # mask shape ex: (352,352,1)
return mask
gradation_width = int(height * 0.1)
gradation_bottom = (
int(height * config["gradation_bottom"])
if "gradation_bottom" in config.keys()
else None
)
box_mask_erosion = (
int(height * config["box_mask_erosion"])
if "box_mask_erosion" in config.keys()
else None
)
# if verbose:
# print('gradation_width : ', gradation_width)
# print('gradation_bottom : ', gradation_bottom)
# print('box_mask_erosion : ', box_mask_erosion)
mask = get_mask_(width, height, gradation_width, gradation_bottom, box_mask_erosion)
mask_crop = mask
mask_origin = 1 - mask
return {"crop": mask_crop, "origin": mask_origin}
def get_compose_func_without_keying_move(template, ratio, verbose=False):
args = template.model.args
df = pd.read_pickle(
f"{template.crop_mp4_dir}/{Path(template.template_video_path).stem}_000/df_fan.pickle"
)
df = df.set_index("frame_idx")
move_head_box_size = (
(df.loc[0]["cropped_box"][2] - df.loc[0]["cropped_box"][0] - 20) // 10 * 10
)
def resize_and_scale(model_out, head_box_idx):
# ratio 1.0 ์ ๋ง๋ ํฌ๊ธฐ๋ก resize ํ๊ณ ,
# ์๋ ์์์์ 10์ ๋ฐฐ์์ ํด๋นํ๋ ์์น๋ก (head_box, model_out) ๋ชจ๋ ์๋ผ๋ธ๋ค.
head_box = df["cropped_box"][head_box_idx]
if ratio == 1.0:
return model_out, head_box
# ์ผ๋จ ์๋ ํฌ๊ธฐ๋ก ๋ง๋ ๋ค.
model_out = resize_adapt(model_out, head_box)
# ์๋ ํฌ๊ธฐ์์์ ๋ฐ์ค์์ 10์ ๋ฐฐ์์ ํด๋นํ๋ ์ขํ๋ฅผ ์ฐพ๋๋ค.
l, t = (np.array(head_box[:2]) + 9) // 10 * 10
new_head_box = np.array(
[l, t, l + move_head_box_size - 1, t + move_head_box_size - 1]
) # ์์ชฝํฌํจ์ด๋ผ์ 1์ ๋นผ์ค๋ค.
# 10์ ๋ฐฐ์์ ๋ง์ถฐ์ ์ด๋ฏธ์ง๋ฅผ ์๋ผ๋ธ๋ค.
diff_box = new_head_box - head_box
new_model_out = model_out[diff_box[1] : diff_box[3], diff_box[0] : diff_box[2]]
# if verbose and head_box_idx == 0:
# print('org head_box:', head_box, ', new_head_box:', new_head_box)
# print('alpah2.shape:', model_out.shape, ', new_model_out:', new_model_out.shape)
if (
new_model_out.shape[0] % 10 != 0 or new_model_out.shape[1] % 10 != 0
): # ํฌ๊ธฐ๋ 10์ ๋ฐฐ์์ฌ์ผ ํ๋ค.
raise Exception(f"new_model_out.shape % 10 != 0, {new_model_out.shape}")
# ratio์ ๋ง๋ ํฌ๊ธฐ๋ก ๋ณ๊ฒฝํ๋ค.
x1, y1, _, _ = np.round(new_head_box * ratio).astype(np.uint8)
# ์์ชฝํฌํจ์ด๋ผ์ -1์ ํด์ค๋ค.
new_head_box = (
x1,
y1,
x1 + int(move_head_box_size * ratio) - 1,
y1 + int(move_head_box_size * ratio) - 1,
)
new_model_out = resize_adapt(new_model_out, new_head_box)
# if verbose and head_box_idx == 0:
# print('org head_box:', head_box, ', new_head_box:', new_head_box)
# print('alpah2.shape:', model_out.shape, ', new_model_out:', new_model_out.shape)
return new_model_out, new_head_box
def compose_one(model_out, full_img, head_box_idx):
model_out, box = resize_and_scale(model_out, head_box_idx)
x1, y1, x2, y2 = box
img = resize_adapt(model_out, (x1, y1, x2, y2))
if (
"compose" in template.config.keys()
and template.config.compose == "face_only"
):
row = df.loc[head_box_idx]
mask_box = get_face_mask(
(img.shape[1], img.shape[0]), row, **get_compose_option(template.config)
)
else:
mask_box = get_box_mask(
x2 - x1 + 1, y2 - y1 + 1, config=args, verbose=verbose
)
if y2 - y1 + 1 != img.shape[0] or x2 - x1 + 1 != img.shape[1]:
raise Exception(
f"not matched compose shape. x2-x1+1: {x2 - x1 + 1}, y2-y1+1:{y2 - y1 + 1}, img: {img.shape[1]}, {img.shape[0]}"
)
# Compose the image
if full_img.shape[2] == 3:
alpha = np.zeros_like(full_img[:, :, :1])
alpha.fill(255)
full_img = np.concatenate([full_img, alpha], axis=2)
out_memory = full_img.copy()
alpha = img[:, :, 3]
alpha = cv2.merge([alpha, alpha, alpha])
back = out_memory[y1 : y2 + 1, x1 : x2 + 1].copy()
front = img[:, :, 0:3]
img = np.concatenate(
[np.where(alpha < (255, 255, 255), back[:, :, :3], front), back[:, :, 3:]],
axis=2,
)
out_memory[y1 : y2 + 1, x1 : x2 + 1] = (
full_img[y1 : y2 + 1, x1 : x2 + 1] * mask_box["origin"]
+ img * mask_box["crop"]
)
return out_memory
return compose_one
def get_compose_func_without_keying_default(template, ratio, verbose=False):
args = template.model.args
df = pd.read_pickle(
f"{template.crop_mp4_dir}/{Path(template.template_video_path).stem}_000/df_fan.pickle"
)
# sz = df['cropped_size'].values[0]
# ์๋ 4k ํ
ํ๋ฆฟ์์ ์ถ์๋ ๋น์จ๋งํผ cropped_box ํฌ๊ธฐ๋ฅผ ์ค์ฌ์ค๋ค.
x1, y1, x2, y2 = np.round(np.array(df["cropped_box"].values[0]) * ratio).astype(
np.uint8
)
del df
mask_box = get_box_mask(x2 - x1 + 1, y2 - y1 + 1, config=args, verbose=verbose)
img_size = args.img_size
if verbose:
print("croped size: ", x2 - x1 + 1, y2 - y1 + 1)
print("croped region(x1,y1,x2,y2): ", x1, y1, x2, y2)
def compose_one(model_out, full_img, _):
img = resize_adapt(model_out, (x1, y1, x2, y2))
if y2 - y1 + 1 != img.shape[0] or x2 - x1 + 1 != img.shape[1]:
raise Exception(
f"not matched compose shape. x2-x1+1: {x2 - x1 + 1}, y2-y1+1:{y2 - y1 + 1}, img: {img.shape[1]}, {img.shape[0]}"
)
# ๋ถ์ฌ๋ฃ๊ธฐ
if full_img.shape[2] == 3:
alpha = np.zeros_like(full_img[:, :, :1])
alpha.fill(255)
full_img = np.concatenate([full_img, alpha], axis=2)
out_memory = full_img.copy()
alpha = img[:, :, 3]
alpha = cv2.merge([alpha, alpha, alpha])
back = out_memory[y1 : y2 + 1, x1 : x2 + 1].copy()
front = img[:, :, 0:3]
img = np.concatenate(
[np.where(alpha < (255, 255, 255), back[:, :, :3], front), back[:, :, 3:]],
axis=2,
)
out_memory[y1 : y2 + 1, x1 : x2 + 1] = (
full_img[y1 : y2 + 1, x1 : x2 + 1] * mask_box["origin"]
+ img * mask_box["crop"]
)
return out_memory
return compose_one
def get_compose_option(config):
blur_ratio = 0.3
dilate_ratio = 0.2
erosion_ratio = 0.0
if "compose_args" in config.keys():
if "blur_ratio" in config.compose_args.keys():
blur_ratio = config.compose_args.blur_ratio
if "dilate_ratio" in config.compose_args.keys():
dilate_ratio = config.compose_args.dilate_ratio
if "erosion_ratio" in config.compose_args.keys():
erosion_ratio = config.compose_args.erosion_ratio
return {
"blur_ratio": blur_ratio,
"dilate_ratio": dilate_ratio,
"erosion_ratio": erosion_ratio,
}
def get_compose_func_without_keying_face_only(template, ratio, verbose=False):
df = pd.read_pickle(
f"{template.crop_mp4_dir}/{Path(template.template_video_path).stem}_000/df_fan.pickle"
)
x1, y1, x2, y2 = np.round(np.array(df["cropped_box"].values[0]) * ratio).astype(
np.uint8
)
df = df.set_index("frame_idx")
if verbose:
print("get_compose_option")
print(get_compose_option(template.config))
def compose_one(model_out, full_img, head_box_idx):
try:
row = df.loc[head_box_idx]
except Exception as e:
print("exception get_compose_func_without_keying_face_only", e)
raise Exception("exception get_compose_func_without_keying_face_only", e)
img = resize_adapt(model_out, (x1, y1, x2, y2))
if y2 - y1 + 1 != img.shape[0] or x2 - x1 + 1 != img.shape[1]:
raise Exception(
f"not matched compose shape. x2-x1+1: {x2 - x1 + 1}, y2-y1+1:{y2 - y1 + 1}, img: {img.shape[1]}, {img.shape[0]}"
)
mask_box = get_face_mask(
(img.shape[1], img.shape[0]), row, **get_compose_option(template.config)
)
# ๋ถ์ฌ๋ฃ๊ธฐ
out_memory = full_img.copy()
out_memory[y1 : y2 + 1, x1 : x2 + 1] = (
full_img[y1 : y2 + 1, x1 : x2 + 1] * mask_box["origin"]
+ img * mask_box["crop"]
)
return out_memory
return compose_one
# template video ์ frame ๊ณผ model inference ๊ฒฐ๊ณผ๋ฅผ ํฉ์ฑํ๋ ํจ์๋ฅผ ๋ฆฌํดํ๋ค.
# params
# ratio : ํ
ํ๋ฆฟ scale ๋น์จ.
# 1.0: ํ
ํ๋ฆฟ ํฌ๊ธฐ ๊ทธ๋๋ก
# 0.5: width, height ๋ฅผ ์ ๋ฐ์ผ๋ก ์ค์ธ ํฌ๊ธฐ
def get_compose_func_without_keying(template, ratio, verbose=False):
if "move" in template.config.keys() and template.config.move:
return get_compose_func_without_keying_move(
template=template, ratio=ratio, verbose=verbose
)
if "compose" in template.config.keys() and template.config.compose == "face_only":
return get_compose_func_without_keying_face_only(
template=template, ratio=ratio, verbose=verbose
)
return get_compose_func_without_keying_default(
template=template, ratio=ratio, verbose=verbose
)
def compose_direct(box, model_args, ratio, model_out, full_img):
x1, y1, x2, y2 = box
mask_box = get_box_mask(x2 - x1 + 1, y2 - y1 + 1, config=model_args)
img_size = model_args.img_size
img = resize_adapt(model_out, (x1, y1, x2, y2))
if y2 - y1 + 1 != img.shape[0] or x2 - x1 + 1 != img.shape[1]:
raise Exception(
f"not matched compose shape. x2-x1+1: {x2 - x1 + 1}, y2-y1+1:{y2 - y1 + 1}, img: {img.shape[1]}, {img.shape[0]}"
)
# ๋ถ์ฌ๋ฃ๊ธฐ
out_memory = full_img.copy()
out_memory[y1 : y2 + 1, x1 : x2 + 1] = (
full_img[y1 : y2 + 1, x1 : x2 + 1] * mask_box["origin"] + img * mask_box["crop"]
)
return out_memory
def keying_direct(model_args, pred, box=None):
cromakey_func = get_cromakey_func(model_args)
compose_func = get_compose_mask_func(model_args)
model_out, mask, alpha = pred["pred"], pred["mask"], pred["img_gt_with_alpha"]
if pred["filename"].endswith("_no.jpg") or pred["filename"].endswith("_no.png"):
return alpha[:, :, [2, 1, 0, 3]]
if (
alpha.shape[0] != mask.shape[0]
or alpha.shape[1] != mask.shape[1]
or alpha.shape[0] != model_out.shape[0]
or alpha.shape[1] != model_out.shape[1]
or alpha.shape[2] != 4
):
raise Exception(
f"not matched keying shape. "
f"alpha: {alpha.shape[0]}, {alpha.shape[1]}, {alpha.shape[2]}, "
f"mask: {mask.shape[0]}, {mask.shape[1]}, "
f"model_out: {model_out.shape[0]}, {model_out.shape[1]}"
)
if box is not None:
model_h = model_out.shape[0]
box_h = box[3] - box[1]
if box_h > model_h:
model_out = resize_adapt(model_out, box)
mask = resize_adapt(mask, box)
alpha = resize_adapt(alpha, box)
model_out = cromakey_func(model_out)
model_out = compose_func(model_out=model_out, org_image_with_alpha=alpha, mask=mask)
return model_out
|