VirtualTryonFR1 / overlay.py
RojaKatta's picture
Update overlay.py
ade4c47 verified
raw
history blame contribute delete
727 Bytes
import cv2
import numpy as np
def overlay_hairstyle(img, hairstyle_img, mask, x_offset=0, y_offset=0, scale=1.0):
# Resize the hairstyle
h, w = img.shape[:2]
hs = cv2.resize(hairstyle_img, (0, 0), fx=scale, fy=scale)
hh, hw = hs.shape[:2]
# Position
center_x, center_y = w // 2 + x_offset, h // 5 + y_offset
top_left_x = max(0, center_x - hw // 2)
top_left_y = max(0, center_y - hh // 2)
roi = img[top_left_y:top_left_y+hh, top_left_x:top_left_x+hw]
if roi.shape[:2] != hs.shape[:2]:
return img
if hs.shape[2] == 4:
alpha = hs[:, :, 3] / 255.0
for c in range(3):
roi[:, :, c] = roi[:, :, c] * (1 - alpha) + hs[:, :, c] * alpha
else:
roi[:] = hs
img[top_left_y:top_left_y+hh, top_left_x:top_left_x+hw] = roi
return img