Spaces:
Running
Running
| 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() |