Spaces:
Running
Running
File size: 2,721 Bytes
f2fa83b e92fa7c f2fa83b 3ea42a4 f2fa83b 4f56633 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 96 97 98 |
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,
):
if imgsz is not None:
imgsz = int(imgsz)
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"""
[](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/face/girl4.jpg', 'AnimeGANv2_Arcane', None],
['example/face/leo.jpg', 'AnimeGANv2_Arcane', None],
['example/face/cap.jpg', 'AnimeGANv2_Arcane', None],
['example/face/anne.jpg', 'AnimeGANv2_Arcane', None],
# ['example/boy2.jpg', 'AnimeGANv3_Arcane', "No"],
# ['example/cap.jpg', 'AnimeGANv3_Arcane', "No"],
['example/landscape/pexels-camilacarneiro-6318793.jpg', 'AnimeGANv2_Hayao', None],
['example/landscape/pexels-nandhukumar-450441.jpg', 'AnimeGANv2_Hayao', None],
]
).launch() |