{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['CUDA_VISIBLE_DEVICES']='3' " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import fastai\n", "import ffmpeg\n", "from fastai import *\n", "from fastai.vision import *\n", "from fastai.callbacks.tensorboard import *\n", "from fastai.vision.gan import *\n", "from fasterai.dataset import *\n", "from fasterai.visualize import *\n", "from fasterai.loss import *\n", "from fasterai.filters import *\n", "from fasterai.generators import *\n", "from pathlib import Path\n", "from itertools import repeat\n", "from IPython.display import HTML, display\n", "plt.style.use('dark_background')\n", "torch.backends.cudnn.benchmark=True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Adjust render_factor (int) if image doesn't look quite right (max 64 on 11GB GPU). The default here works for most photos. \n", "#It literally just is a number multiplied by 16 to get the square render resolution. \n", "#Note that this doesn't affect the resolution of the final output- the output is the same resolution as the input.\n", "#Example: render_factor=21 => color is rendered at 16x21 = 336x336 px. \n", "render_factor=21\n", "root_folder = Path('data/imagenet/ILSVRC/Data/CLS-LOC/bandw')\n", "#weights_name = 'ColorizeNew50_gen192_10'\n", "weights_name = 'ColorizeNew68_gen192_01_5'\n", "nf_factor = 1.25\n", "\n", "workfolder = Path('./video')\n", "source_folder = workfolder/\"source\"\n", "bwframes_root = workfolder/\"bwframes\"\n", "colorframes_root = workfolder/\"colorframes\"\n", "result_folder = workfolder/\"result\"\n", "#Make source_url None to just read from source_path directly without modification\n", "source_url = 'https://twitter.com/silentmoviegifs/status/1092793719173115905'\n", "#source_url=None\n", "source_name = 'video8.mp4'\n", "source_path = source_folder/source_name\n", "bwframes_folder = bwframes_root/(source_path.stem)\n", "colorframes_folder = colorframes_root/(source_path.stem)\n", "result_path = result_folder/source_name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def progress(value, max=100):\n", " return HTML(\"\"\"\n", " \n", " {value}\n", " \n", " \"\"\".format(value=value, max=max))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_fps():\n", " probe = ffmpeg.probe(str(source_path))\n", " stream_data = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)\n", " avg_frame_rate = stream_data['avg_frame_rate']\n", " print(avg_frame_rate)\n", " fps_num=avg_frame_rate.split(\"/\")[0]\n", " fps_den = avg_frame_rate.rsplit(\"/\")[1]\n", " return round(float(fps_num)/float(fps_den))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def purge_images(dir):\n", " for f in os.listdir(dir):\n", " if re.search('.*?\\.jpg', f):\n", " os.remove(os.path.join(dir, f))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download Video (optional via setting source_url)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Specify media_url. Many sources will work (YouTube, Imgur, Twitter, Reddit, etc). Complete list here: https://rg3.github.io/youtube-dl/supportedsites.html . The resulting file path can be used later." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if source_url is not None:\n", " if source_path.exists(): source_path.unlink()\n", " youtubdl_command = 'youtube-dl \"' + source_url + '\" -o \"' + str(source_path) + '\"'\n", " print(youtubdl_command)\n", " print('\\n')\n", " output = Path(os.popen(youtubdl_command).read())\n", " print(str(output))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extract Raw Frames" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bwframe_path_template = str(bwframes_folder/'%5d.jpg')\n", "bwframes_folder.mkdir(parents=True, exist_ok=True)\n", "purge_images(bwframes_folder)\n", "ffmpeg.input(str(source_path)).output(str(bwframe_path_template), format='image2', vcodec='mjpeg', qscale=0).run(capture_stdout=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "framecount = len(os.listdir(str(bwframes_folder)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DeOldify / Colorize" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis = get_colorize_visualizer(root_folder=root_folder, weights_name=weights_name, nf_factor=nf_factor, render_factor=render_factor)\n", "#vis = get_colorize_visualizer(render_factor=render_factor)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prog = 0\n", "out = display(progress(0, 100), display_id=True)\n", "colorframes_folder.mkdir(parents=True, exist_ok=True)\n", "purge_images(colorframes_folder)\n", "\n", "for img in os.listdir(str(bwframes_folder)):\n", " img_path = bwframes_folder/img\n", " if os.path.isfile(str(img_path)):\n", " color_image = vis.get_transformed_image(str(img_path), render_factor)\n", " color_image.save(str(colorframes_folder/img))\n", " prog += 1\n", " out.update(progress(prog, framecount))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build Video" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "colorframes_path_template = str(colorframes_folder/'%5d.jpg')\n", "result_path.parent.mkdir(parents=True, exist_ok=True)\n", "\n", "if result_path.exists(): result_path.unlink()\n", "\n", "fps = get_fps()\n", "print(fps)\n", "ffmpeg.input(str(colorframes_path_template), format='image2', vcodec='mjpeg', framerate=str(fps)).output(str(result_path), crf=17, vcodec='libx264').run(capture_stdout=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "67px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }