|
from PIL import Image, ImageDraw, ImageFont, ImageEnhance |
|
import torch |
|
import torchvision |
|
import gradio |
|
import numpy as np |
|
|
|
Instructuction = "Select a Unique Portrait Image of yourself" |
|
title="I am something of a Painter, Anime-Edition (AnimeGAN-V2)" |
|
description = "Drag/Drop or Upload a cute portrait Image of yourself or anyone you find interesting 😉, then observe how this Generative model\ |
|
is able to Generate a cute Anime-Cartoon version of your Image." |
|
article = """ |
|
- Select an image from the examples provided as demo image, |
|
- Click submit to Generate Image, |
|
- Tips: Quality Images with Great brightness/without pre-existing filters work better (Image Noise). |
|
- Privacy: No user data is collected or saved, |
|
- Credits to akhaliq/AnimeGANv2 for original AnimeGanV2""" |
|
|
|
model = torch.hub.load( |
|
"AK391/animegan2-pytorch:main", |
|
"generator", |
|
pretrained=True, |
|
|
|
progress=False |
|
) |
|
face2paint = torch.hub.load( |
|
'AK391/animegan2-pytorch:main', 'face2paint', |
|
size=512, |
|
side_by_side=False |
|
) |
|
|
|
def enhance_logo_brightness(logo_image, factor): |
|
enhancer = ImageEnhance.Brightness(logo_image) |
|
return enhancer.enhance(factor) |
|
|
|
def add_logo(image): |
|
logo_path = "./logo.jpg" |
|
logo_size = (100, 30) |
|
logo = Image.open(logo_path).convert("RGBA") |
|
logo = logo.resize(logo_size, resample=Image.BICUBIC) |
|
logo = enhance_logo_brightness(logo, 1.5) |
|
return logo |
|
|
|
def inference(img): |
|
img_pil = Image.fromarray(img.astype('uint8'), 'RGB') |
|
output_image_pil = face2paint(model, img_pil.resize((512, 512))) |
|
output_image_pil = output_image_pil.convert('RGB') |
|
logo = add_logo(output_image_pil) |
|
output_image_pil.paste(logo, (512 - logo.size[0], 512 - logo.size[1]), logo) |
|
output_image = np.array(output_image_pil) |
|
return output_image |
|
|
|
gradio.Interface(inference, |
|
inputs=gradio.Image(type='numpy'), |
|
outputs=gradio.Image(type='numpy'), |
|
Instructuction=Instructuction, title=title, description=description, article=article).launch() |
|
|