|
import os |
|
import cv2 |
|
import gradio as gr |
|
import requests |
|
from PIL import Image |
|
from PIL import UnidentifiedImageError |
|
import io |
|
from io import BytesIO |
|
import AnimeGANv3_src |
|
|
|
|
|
|
|
class AnimeGANv3: |
|
def __init__(self): |
|
|
|
os.makedirs('output', exist_ok=True) |
|
|
|
def inference(self, img_path, style, face_detection): |
|
print(img_path, style, face_detection) |
|
try: |
|
|
|
img = cv2.imread(img_path) |
|
if img is None: |
|
raise ValueError(f"Unable to read image at {img_path}") |
|
|
|
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
style_code = { |
|
"AnimeGANv3_Disney v1.0": "D", |
|
|
|
} |
|
f = style_code.get(style, "U") |
|
|
|
try: |
|
det_face = face_detection == "Yes" |
|
|
|
output = AnimeGANv3_src.Convert(img, f, det_face) |
|
|
|
|
|
save_path = f"output/out.{img_path.rsplit('.')[-1]}" |
|
cv2.imwrite(save_path, cv2.cvtColor(output, cv2.COLOR_RGB2BGR)) |
|
|
|
return output, save_path |
|
except RuntimeError as error: |
|
print('global exception', error) |
|
return None, None |
|
except Exception as error: |
|
print('global exception', error) |
|
return None, None |
|
|
|
|
|
|
|
|
|
|
|
|
|
DESCRIPTION = f""" |
|
# [AnimeGANv3_Disney v1.0 Implementation](https://github.com/TachibanaYoshino/AnimeGANv3) |
|
|
|
""" |
|
|
|
|
|
anime_gan_disney = AnimeGANv3() |
|
|
|
|
|
iface = gr.Interface( |
|
fn=anime_gan_disney.inference, |
|
inputs=[ |
|
gr.components.Image(type="filepath", label="Input"), |
|
gr.components.Dropdown(choices=[ |
|
'AnimeGANv3_Disney v1.0', |
|
|
|
], label='AnimeGANv3 Style'), |
|
gr.components.Radio(choices=["Yes", "No"], label='Extract face'), |
|
], |
|
outputs=[ |
|
gr.components.Image(type="numpy", label="Output (The whole image)"), |
|
gr.components.File(label="Download the output image") |
|
], |
|
description=DESCRIPTION, |
|
allow_flagging="never", |
|
examples=[ |
|
['samples/Angela_Aguilar_0.jpg', 'AnimeGANv3_Disney v1.0', "Yes"], |
|
|
|
['samples/Avantika_Vandanapu.jpg', 'AnimeGANv3_Disney v1.0', "Yes"], |
|
|
|
|
|
['samples/Ester_Exposito_.jpg', 'AnimeGANv3_Disney v1.0', "Yes"], |
|
['samples/Giorgia_Whigham.jpg', 'AnimeGANv3_Disney v1.0', "Yes"], |
|
|
|
|
|
['samples/Laura_Osma_.jpg', 'AnimeGANv3_Disney v1.0', "No"], |
|
|
|
['samples/Michelle_Pellicer_.jpeg', 'AnimeGANv3_Disney v1.0', "No"], |
|
|
|
['samples/Olivia_Rodrigo_.jpg', 'AnimeGANv3_Disney v1.0', "No"], |
|
|
|
] |
|
) |
|
|
|
|
|
iface.launch() |
|
|