File size: 2,099 Bytes
19b551e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import cv2
import numpy as np
from PIL import Image
import torch
from AnimeGANv2.test import inference, load_model

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

generator = None

def get_generator():
    global generator
    if generator is None:
        generator = load_model(style="Hayao", device=DEVICE)
    return generator

def ghibli_transform(image):
    if image is None:
        return None, "Kein Bild"

    try:
        gen = get_generator()
        img_np = np.array(image)
        img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
        cartoon_bgr = inference.inference(gen, img_bgr, device=DEVICE)
        cartoon_rgb = cv2.cvtColor(cartoon_bgr, cv2.COLOR_BGR2RGB)
        result = Image.fromarray(cartoon_rgb)
        return result, "fertig"
    except Exception as e:
        return None, str(e)

css = """
.gradio-container { max-width: 1100px; margin: auto; padding: 1rem; }
h1 { color: #4CAF50; text-align: center; }
button { min-height: 50px; font-size: 1.1rem !important; }
"""

with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="green")) as demo:
    gr.Markdown("# Foto → Studio Ghibli Style (Hayao)")
    
    with gr.Row():
        input_image = gr.Image(type="pil", label="Original", sources=["upload", "clipboard"], height=480)
        
        with gr.Column(min_width=280):
            btn = gr.Button("In Ghibli-Stil umwandeln", variant="primary")
        
        output_image = gr.Image(label="Ergebnis", height=480)

    status = gr.Textbox(label="Status", interactive=False)

    gr.Examples(
        examples=[
            "https://images.unsplash.com/photo-1506905925346-21bda4d32df4",
            "https://images.unsplash.com/photo-1543466835-00a7907e9de1",
            "https://images.unsplash.com/photo-1501785888041-af3ef285b470",
            "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05",
        ],
        inputs=input_image
    )

    btn.click(
        fn=ghibli_transform,
        inputs=input_image,
        outputs=[output_image, status]
    )

if __name__ == "__main__":
    demo.launch()