File size: 2,690 Bytes
f2fa83b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import cv2
import numpy as np
import gradio as gr
from inference import Predictor
from utils.image_processing import resize_image

os.makedirs('output', exist_ok=True)


def inference(
    image: np.ndarray,
    style,
    imgsz=None,
):
    retain_color = False

    weight = {
        "AnimeGAN_Hayao": "hayao",
        "AnimeGAN_Shinkai": "shinkai",
        "AnimeGANv2_Hayao": "hayao:v2",
        "AnimeGANv2_Shinkai": "shinkai:v2",
        "AnimeGANv2_Arcane": "arcane:v2",
    }[style]
    predictor = Predictor(
        weight,
        device='cpu',
        retain_color=retain_color,
        imgsz=imgsz,
    )

    save_path = f"output/out.jpg"    
    image = resize_image(image, width=imgsz)
    anime_image = predictor.transform(image)[0]
    cv2.imwrite(save_path, anime_image[..., ::-1])
    return anime_image, save_path


title = "AnimeGANv2: To produce your own animation."
description = r"""Turn your photo into anime style 😊"""
article = r"""
[![GitHub Stars](https://img.shields.io/github/stars/ptran1203/pytorch-animeGAN?style=social)](https://github.com/ptran1203/pytorch-animeGAN)    
### πŸ—» Demo

"""

gr.Interface(
    fn=inference,
    inputs=[
        gr.components.Image(label="Input"),
        gr.Dropdown(
            [
                'AnimeGAN_Hayao',
                'AnimeGAN_Shinkai',
                'AnimeGANv2_Hayao',
                'AnimeGANv2_Shinkai',
                'AnimeGANv2_Arcane',
            ],
            type="value",
            value='AnimeGANv2_Hayao',
            label='Style'
        ),
        gr.Dropdown(
            [
                None,
                416,
                512,
                768,
                1024,
                1536,
            ],
            type="value",
            value=None,
            label='Image size'
        )
    ],
    outputs=[
        gr.components.Image(type="numpy", label="Output (The whole image)"),
        gr.components.File(label="Download the output image")
    ],
    title=title,
    description=description,
    article=article,
    allow_flagging="never",
    examples=[
        ['example/arcane/girl4.jpg', 'AnimeGANv2_Arcane', "Yes"],
        ['example/arcane/leo.jpg', 'AnimeGANv2_Arcane', "Yes"],
        ['example/arcane/girl.jpg', 'AnimeGANv2_Arcane', "Yes"],
        ['example/arcane/anne.jpg', 'AnimeGANv2_Arcane', "Yes"],
        # ['example/boy2.jpg', 'AnimeGANv3_Arcane', "No"],
        # ['example/cap.jpg', 'AnimeGANv3_Arcane', "No"],
        ['example/more/hayao_v2/pexels-camilacarneiro-6318793.jpg', 'AnimeGANv2_Hayao', "Yes"],
        ['example/more/hayao_v2/pexels-nandhukumar-450441.jpg', 'AnimeGANv2_Hayao', "Yes"],
    ]
).launch()