{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ['CUDA_VISIBLE_DEVICES']='3' "
]
},
{
"cell_type": "code",
"execution_count": 2,
"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": 3,
"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=25\n",
"root_folder = Path('data/imagenet/ILSVRC/Data/CLS-LOC/bandw')\n",
"weights_name = 'ColorizeNew44_gen19205'\n",
"#weights_name = 'ColorizeNew32_gen'\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 = 'video5.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": 4,
"metadata": {},
"outputs": [],
"source": [
"def progress(value, max=100):\n",
" return HTML(\"\"\"\n",
" \n",
" \"\"\".format(value=value, max=max))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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": 6,
"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": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"youtube-dl \"https://twitter.com/silentmoviegifs/status/1092793719173115905\" -o \"video/source/video5.mp4\"\n",
"\n",
"\n",
"[twitter] 1092793719173115905: Downloading webpage\n",
"[twitter:card] 1092793719173115905: Downloading webpage\n",
"[twitter:card] 1092793719173115905: Downloading guest token\n",
"[twitter:card] 1092793719173115905: Downloading JSON metadata\n",
"[download] Destination: video/source/video5.mp4\n",
"\n",
"\u001b[K[download] 0.4% of 252.07KiB at 567.03KiB/s ETA 00:00\n",
"\u001b[K[download] 1.2% of 252.07KiB at 1.55MiB/s ETA 00:00\n",
"\u001b[K[download] 2.8% of 252.07KiB at 3.37MiB/s ETA 00:00\n",
"\u001b[K[download] 6.0% of 252.07KiB at 6.52MiB/s ETA 00:00\n",
"\u001b[K[download] 12.3% of 252.07KiB at 5.96MiB/s ETA 00:00\n",
"\u001b[K[download] 25.0% of 252.07KiB at 3.37MiB/s ETA 00:00\n",
"\u001b[K[download] 50.4% of 252.07KiB at 3.60MiB/s ETA 00:00\n",
"\u001b[K[download] 100.0% of 252.07KiB at 5.21MiB/s ETA 00:00\n",
"\u001b[K[download] 100% of 252.07KiB in 00:00\n",
"\n"
]
}
],
"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": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(b'', None)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"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": 9,
"metadata": {},
"outputs": [],
"source": [
"framecount = len(os.listdir(str(bwframes_folder)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DeOldify / Colorize"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/media/jason/Projects/Deep Learning/DeOldifyV2/DeOldify/fastai/data_block.py:414: UserWarning: Your training set is empty. Is this is by design, pass `ignore_empty=True` to remove this warning.\n",
" warn(\"Your training set is empty. Is this is by design, pass `ignore_empty=True` to remove this warning.\")\n",
"/media/jason/Projects/Deep Learning/DeOldifyV2/DeOldify/fastai/data_block.py:417: UserWarning: Your validation set is empty. Is this is by design, use `no_split()` \n",
" or pass `ignore_empty=True` when labelling to remove this warning.\n",
" or pass `ignore_empty=True` when labelling to remove this warning.\"\"\")\n"
]
}
],
"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": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"71/7\n",
"10\n"
]
},
{
"data": {
"text/plain": [
"(b'', None)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"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
}