forked from bryandlee/animegan2-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from io import BytesIO\n", | ||
"import torch\n", | ||
"from PIL import Image\n", | ||
"\n", | ||
"import ipywidgets as widgets\n", | ||
"import IPython.display as display\n", | ||
"from google.colab import files\n", | ||
"\n", | ||
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", | ||
"model = torch.hub.load(\"bryandlee/animegan2-pytorch:main\", \"generator\", device=device).eval()\n", | ||
"face2paint = torch.hub.load(\"bryandlee/animegan2-pytorch:main\", \"face2paint\", device=device)\n", | ||
"\n", | ||
"#@title Anime FaceGAN Colab app\n", | ||
"image_format = \"jpeg\" #@param [\"jpeg\", \"png\"]\n", | ||
"\n", | ||
"\n", | ||
"button = widgets.Button(description=\"Start\")\n", | ||
"output = widgets.Output()\n", | ||
"\n", | ||
"uploader = widgets.FileUpload(\n", | ||
" accept='image/*', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n", | ||
" multiple=False # True to accept multiple files upload else False\n", | ||
")\n", | ||
"\n", | ||
"def run(b):\n", | ||
" button.disabled = True\n", | ||
"\n", | ||
" with output:\n", | ||
" display.clear_output()\n", | ||
"\n", | ||
" for fname in uploader.value:\n", | ||
" bytes_in = uploader.value[fname]['content']\n", | ||
"\n", | ||
" im_in = Image.open(BytesIO(bytes_in)).convert(\"RGB\")\n", | ||
" im_out = face2paint(model, im_in, side_by_side=False)\n", | ||
"\n", | ||
" buffer_out = BytesIO()\n", | ||
" im_out.save(buffer_out, format=image_format)\n", | ||
"\n", | ||
" bytes_out = buffer_out.getvalue()\n", | ||
" wi1 = widgets.Image(value=bytes_in, format=image_format)\n", | ||
" wi2 = widgets.Image(value=bytes_out, format=image_format)\n", | ||
"\n", | ||
" wi1.layout.max_width = '500px'\n", | ||
" wi1.layout.max_height = '500px'\n", | ||
" wi2.layout.max_width = '500px'\n", | ||
" wi2.layout.max_height = '500px'\n", | ||
"\n", | ||
" ## Side by side thanks to HBox widgets\n", | ||
" sidebyside = widgets.HBox([wi1, wi2])\n", | ||
" ## Finally, show.\n", | ||
" with output:\n", | ||
" display.display(sidebyside)\n", | ||
" \n", | ||
" button.disabled = False\n", | ||
"\n", | ||
"button.on_click(run)\n", | ||
"display.display(uploader, button, output)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "plaintext" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |