Skip to content

Commit

Permalink
Fix uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
xhlulu committed Nov 17, 2021
1 parent cd65ad5 commit 927c6c1
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions colab_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
},
"outputs": [],
"source": [
"#@title Anime FaceGAN Colab app\n",
"\n",
"from io import BytesIO\n",
"import torch\n",
"from PIL import Image\n",
Expand All @@ -85,38 +87,30 @@
"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",
"image_format = \"png\" #@param [\"jpeg\", \"png\"]\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",
" uploaded = files.upload()\n",
"\n",
" for fname in uploader.value:\n",
" bytes_in = uploader.value[fname]['content']\n",
" for fname in uploaded:\n",
" bytes_in = uploaded[fname]\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",
Expand All @@ -131,7 +125,7 @@
" button.disabled = False\n",
"\n",
"button.on_click(run)\n",
"display.display(uploader, button, output)"
"display.display(button, output)"
]
}
],
Expand Down

0 comments on commit 927c6c1

Please sign in to comment.