Skip to content

Commit

Permalink
Add colab demo
Browse files Browse the repository at this point in the history
  • Loading branch information
xhlulu committed Nov 8, 2021
1 parent 6c2de96 commit 98e4692
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions colab_demo.ipynb
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
}

0 comments on commit 98e4692

Please sign in to comment.