From d861745173c63b19114d9be359f1dd39ad05c6e7 Mon Sep 17 00:00:00 2001 From: graphemecluster Date: Sat, 1 Oct 2022 07:25:30 +0800 Subject: [PATCH] Fix Broken Demo --- demo.ipynb | 112 ++++++++++++++++++++++++++--------------------- demo.py | 52 ++++++++++------------ requirements.txt | 7 +-- 3 files changed, 90 insertions(+), 81 deletions(-) diff --git a/demo.ipynb b/demo.ipynb index a435b6be..a5795fd3 100644 --- a/demo.ipynb +++ b/demo.ipynb @@ -40,12 +40,12 @@ "id": "UCMFMJV7K-ag" }, "source": [ - "!pip install imageio==2.4.1 &> /dev/null", - "!pip install ffmpy &> /dev/null\n", - "!git init -q .\n", + "%%capture\n", + "%pip install ffmpeg-python imageio-ffmpeg\n", + "!git init .\n", "!git remote add origin https://github.com/AliaksandrSiarohin/first-order-model\n", - "!git pull -q origin master\n", - "!git clone -q https://github.com/graphemecluster/first-order-model-demo demo" + "!git pull origin master\n", + "!git clone https://github.com/graphemecluster/first-order-model-demo demo" ], "execution_count": null, "outputs": [] @@ -59,6 +59,7 @@ "import IPython.display\n", "import PIL.Image\n", "import cv2\n", + "import ffmpeg\n", "import imageio\n", "import io\n", "import ipywidgets\n", @@ -68,11 +69,12 @@ "import skimage.transform\n", "import warnings\n", "from base64 import b64encode\n", - "from demo import load_checkpoints, make_animation\n", - "from ffmpy import FFmpeg\n", + "from demo import load_checkpoints, make_animation # type: ignore (local file)\n", "from google.colab import files, output\n", "from IPython.display import HTML, Javascript\n", + "from shutil import copyfileobj\n", "from skimage import img_as_ubyte\n", + "from tempfile import NamedTemporaryFile\n", "warnings.filterwarnings(\"ignore\")\n", "os.makedirs(\"user\", exist_ok=True)\n", "\n", @@ -101,10 +103,10 @@ "\twidth: 250px;\n", "}\n", ".widget-checkbox {\n", - " width: 650px;\n", + "\twidth: 650px;\n", "}\n", ".widget-checkbox + .widget-checkbox {\n", - " margin-top: -6px;\n", + "\tmargin-top: -6px;\n", "}\n", ".input-widget .output_html {\n", "\ttext-align: center;\n", @@ -114,9 +116,6 @@ "\tcolor: lightgray;\n", "\tfont-size: 72px;\n", "}\n", - "div.stream {\n", - "\tdisplay: none;\n", - "}\n", ".title {\n", "\tfont-size: 20px;\n", "\tfont-weight: bold;\n", @@ -202,6 +201,9 @@ ".loading-label {\n", "\tcolor: gray;\n", "}\n", + ".video {\n", + "\tmargin: 0;\n", + "}\n", ".comparison-widget {\n", "\twidth: 256px;\n", "\theight: 256px;\n", @@ -226,10 +228,7 @@ "\treturn imageio.get_reader(file, mode='I', format='FFMPEG').get_next_data()\n", "\n", "def create_image(i, j):\n", - "\timage_widget = ipywidgets.Image(\n", - "\t\tvalue=open('demo/images/%d%d.png' % (i, j), 'rb').read(),\n", - "\t\tformat='png'\n", - "\t)\n", + "\timage_widget = ipywidgets.Image.from_file('demo/images/%d%d.png' % (i, j))\n", "\timage_widget.add_class('resource')\n", "\timage_widget.add_class('resource-image')\n", "\timage_widget.add_class('resource-image%d%d' % (i, j))\n", @@ -260,7 +259,7 @@ "def convert_output(button):\n", "\tcomplete.layout.display = 'none'\n", "\tloading.layout.display = ''\n", - "\tFFmpeg(inputs={'output.mp4': None}, outputs={'scaled.mp4': '-vf \"scale=1080x1080:flags=lanczos,pad=1920:1080:420:0\" -y'}).run()\n", + "\tffmpeg.input('output.mp4').output('scaled.mp4', vf='scale=1080x1080:flags=lanczos,pad=1920:1080:420:0').overwrite_output().run()\n", "\tfiles.download('scaled.mp4')\n", "\tloading.layout.display = 'none'\n", "\tcomplete.layout.display = ''\n", @@ -340,7 +339,8 @@ "loader.add_class(\"loader\")\n", "loading_label = ipywidgets.Label(\"This may take several minutes to process…\")\n", "loading_label.add_class(\"loading-label\")\n", - "loading = ipywidgets.VBox([loader, loading_label])\n", + "progress_bar = ipywidgets.Output()\n", + "loading = ipywidgets.VBox([loader, loading_label, progress_bar])\n", "loading.add_class('loading')\n", "\n", "output_widget = ipywidgets.Output()\n", @@ -421,10 +421,10 @@ "output.register_callback(\"notebook.select_video\", select_video)\n", "\n", "def resize(image, size=(256, 256)):\n", - " w, h = image.size\n", - " d = min(w, h)\n", - " r = ((w - d) // 2, (h - d) // 2, (w + d) // 2, (h + d) // 2)\n", - " return image.resize(size, resample=PIL.Image.LANCZOS, box=r)\n", + "\tw, h = image.size\n", + "\td = min(w, h)\n", + "\tr = ((w - d) // 2, (h - d) // 2, (w + d) // 2, (h + d) // 2)\n", + "\treturn image.resize(size, resample=PIL.Image.LANCZOS, box=r)\n", "\n", "def upload_image(change):\n", "\tglobal selected_image\n", @@ -476,39 +476,51 @@ "\tfor frame in reader:\n", "\t\tdriving_video.append(frame)\n", "\tgenerator, kp_detector = load_checkpoints(config_path='config/%s-256.yaml' % model.value, checkpoint_path=filename)\n", - "\tpredictions = make_animation(\n", - "\t\tskimage.transform.resize(numpy.asarray(selected_image), (256, 256)),\n", - "\t\t[skimage.transform.resize(frame, (256, 256)) for frame in driving_video],\n", - "\t\tgenerator,\n", - "\t\tkp_detector,\n", - "\t\trelative=relative.value,\n", - "\t\tadapt_movement_scale=adapt_movement_scale.value\n", - "\t)\n", + "\twith progress_bar:\n", + "\t\tpredictions = make_animation(\n", + "\t\t\tskimage.transform.resize(numpy.asarray(selected_image), (256, 256)),\n", + "\t\t\t[skimage.transform.resize(frame, (256, 256)) for frame in driving_video],\n", + "\t\t\tgenerator,\n", + "\t\t\tkp_detector,\n", + "\t\t\trelative=relative.value,\n", + "\t\t\tadapt_movement_scale=adapt_movement_scale.value\n", + "\t\t)\n", + "\tprogress_bar.clear_output()\n", + "\timageio.mimsave('output.mp4', [img_as_ubyte(frame) for frame in predictions], fps=fps)\n", "\tif selected_video.startswith('user/') or selected_video == 'demo/videos/0.mp4':\n", - "\t\timageio.mimsave('temp.mp4', [img_as_ubyte(frame) for frame in predictions], fps=fps)\n", - "\t\tFFmpeg(inputs={'temp.mp4': None, selected_video: None}, outputs={'output.mp4': '-c copy -y'}).run()\n", - "\telse:\n", - "\t\timageio.mimsave('output.mp4', [img_as_ubyte(frame) for frame in predictions], fps=fps)\n", - "\tloading.layout.display = 'none'\n", - "\tcomplete.layout.display = ''\n", + "\t\twith NamedTemporaryFile(suffix='.mp4') as output:\n", + "\t\t\tffmpeg.output(ffmpeg.input('output.mp4').video, ffmpeg.input(selected_video).audio, output.name, c='copy').overwrite_output().run()\n", + "\t\t\twith open('output.mp4', 'wb') as result:\n", + "\t\t\t\tcopyfileobj(output, result)\n", "\twith output_widget:\n", - "\t\tdisplay(HTML('