Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648280
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-07-12 17:13:16

tflib.init_tf()

URL_FFHQ = "./karras2019stylegan-ffhq-1024x1024.pkl"

with dnnlib.util.open_url(URL_FFHQ, cache_dir=config.cache_dir) as f:

    generator_network, discriminator_network, Gs_network = pickle.load(f)

generator = Generator(Gs_network, batch_size=1, randomize_noise=False)

model_scale = int(2 * (math.log(1024, 2) - 1))

age_direction = np.load('./ffhq_dataset/latent_directions/age.npy')

horizontal_direction = np.load('./ffhq_dataset/latent_directions/angle_horizontal.npy')

vertical_direction = np.load('./ffhq_dataset/latent_directions/angle_vertical.npy')

eyes_open_direction = np.load('./ffhq_dataset/latent_directions/eyes_open.npy')

gender_direction = np.load('./ffhq_dataset/latent_directions/gender.npy')

smile_direction = np.load('./ffhq_dataset/latent_directions/smile.npy')

def get_watermarked(pil_image: Image) -> Image:

    try:

        image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)

        (h, w) = image.shape[:2]

        image = np.dstack([image, np.ones((h, w), dtype="uint8") * 255])

        pct = 0.08

        full_watermark = cv2.imread('./media/logo.png', cv2.IMREAD_UNCHANGED)

        (fwH, fwW) = full_watermark.shape[:2]

        wH = int(pct * h * 2)

        wW = int((wH * fwW) / fwH * 0.1)

        watermark =外汇跟单gendan5.com cv2.resize(full_watermark, (wH, wW), interpolation=cv2.INTER_AREA)

        overlay = np.zeros((h, w, 4), dtype="uint8")

        (wH, wW) = watermark.shape[:2]

        overlay[h - wH - 10: h - 10, 10: 10 + wW] = watermark

        output = image.copy()

        cv2.addWeighted(overlay, 0.5, output, 1.0, 0, output)

        rgb_image = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)

        return Image.fromarray(rgb_image)

    except:

        return pil_image

def generate_final_images(latent_vector, direction, coeffs, i):

    new_latent_vector = latent_vector.copy()

    new_latent_vector[:8] = (latent_vector + coeffs * direction)[:8]

    new_latent_vector = new_latent_vector.reshape((1, 18, 512))

    generator.set_dlatents(new_latent_vector)

    img_array = generator.generate_images()[0]

    img = PIL.Image.fromarray(img_array, 'RGB')

    if size[0] >= 512: img = get_watermarked(img)

    img_path = "./for_animation/" + str(i) + ".png"

    img.thumbnail(animation_size, PIL.Image.ANTIALIAS)

    img.save(img_path)

    face_img.append(imageio.imread(img_path))

    clear_output()

    return img

def generate_final_image(latent_vector, direction, coeffs):

    new_latent_vector = latent_vector.copy()

    new_latent_vector[:8] = (latent_vector + coeffs * direction)[:8]

    new_latent_vector = new_latent_vector.reshape((1, 18, 512))

    generator.set_dlatents(new_latent_vector)

    img_array = generator.generate_images()[0]

    img = PIL.Image.fromarray(img_array, 'RGB')

    if size[0] >= 512: img = get_watermarked(img)

    img.thumbnail(size, PIL.Image.ANTIALIAS)

    img.save("face.png")

    if download_image == True: files.download("face.png")

    return img

def plot_three_images(imgB, fs=10):

    f, axarr = plt.subplots(1, 3, figsize=(fs, fs))

    axarr[0].imshow(Image.open('./aligned_images/father_01.png'))

    axarr[0].title.set_text("Father's photo")

    axarr[1].imshow(imgB)

    axarr[1].title.set_text("Child's photo")

    axarr[2].imshow(Image.open('./aligned_images/mother_01.png'))

    axarr[2].title.set_text("Mother's photo")

    plt.setp(plt.gcf().get_axes(), xticks=[], yticks=[])

    plt.show()

阅读(330) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~